dexBuilder.writeTo(new FileDataStore(new File(options.outputDexFile)));
/** * Writes all built dex files to the given folder. * * @param folder * the output folder * @return File handles to all written dex files * @throws IOException * when failed to create {@link FileDataStore} */ public List<File> writeTo(String folder) throws IOException { final List<File> result = new ArrayList<>(dexPools.size()); for (DexPool dexPool : dexPools) { int count = result.size(); // name dex files: classes.dex, classes2.dex, classes3.dex, etc. File file = new File(folder, "classes" + (count == 0 ? "" : count + 1) + ".dex"); result.add(file); FileDataStore fds = new FileDataStore(file); dexPool.writeTo(fds); fds.close(); } return result; } }
private void build() throws AndrolibException { try { DexBuilder dexBuilder; if (mApiLevel > 0) { dexBuilder = new DexBuilder(Opcodes.forApi(mApiLevel)); } else { dexBuilder = new DexBuilder(Opcodes.getDefault()); } for (String fileName : mSmaliDir.getDirectory().getFiles(true)) { buildFile(fileName, dexBuilder); } dexBuilder.writeTo(new FileDataStore( new File(mDexFile.getAbsolutePath()))); } catch (IOException | DirectoryException ex) { throw new AndrolibException(ex); } }
final FileDataStore fileDataStore = new FileDataStore(dest); dexBuilder.writeTo(fileDataStore); final String md5 = MD5.getMD5(dest);
public void run(String[] args) throws IOException, UnhandledVirtualException { opts = getOptions(args); setLogLevel(opts); log.info("Options:\n{}", opts.toString()); RuntimeStats stats = new RuntimeStats(); stats.begin(); vm = vmFactory.build( opts.getInFile(), opts.getOutputAPILevel(), opts.getMaxAddressVisits(), opts.getMaxCallDepth(), opts.getMaxMethodVisits(), opts.getMaxExecutionTime() ); ClassManager classManager = vm.getClassManager(); Map<String, Collection<VirtualMethod>> targetClassNameToMethods = collectTargetClassNameToMethods(classManager, opts); stats.startClasses(targetClassNameToMethods.keySet()); for (Map.Entry<String, Collection<VirtualMethod>> entry : targetClassNameToMethods.entrySet()) { stats.incrementCurrentClassIndex(); String className = entry.getKey(); Collection<VirtualMethod> methods = entry.getValue(); System.out.println("[" + stats.getCurrentClassIndex() + " / " + stats.getTotalClasses() + "] Processing top level class " + className); executeMethods(methods, classManager, stats); } stats.end(); System.out.println("Simplification complete:\n" + stats.getStats()); System.out.println(Optimizer.getTotalOptimizationCounts()); System.out.println("Writing output to " + opts.getOutFile()); classManager.getDexBuilder().writeTo(new FileDataStore(opts.getOutDexFile())); if (opts.isZip()) { Files.copy(opts.getInFile().toPath(), opts.getOutFile().toPath(), StandardCopyOption.REPLACE_EXISTING); updateZip(opts.getOutFile(), opts.getOutDexFile(), "classes.dex"); } }
@Test public void emptyingATryBlockWithTwoHandlersWhichCreatesNullStartAndEndLocationsIsRemovedWithoutIncident() throws IOException { manipulator = OptimizerTester.getGraphManipulator(CLASS_NAME, "tryBlockWithTwoCatches()V"); assertEquals(2, manipulator.getTryBlocks().size()); manipulator.removeInstruction(0); assertEquals(0, manipulator.getTryBlocks().size()); // Exception is thrown when saving. Make sure doesn't happen. ClassManager classManager = manipulator.getVM().getClassManager(); File out = File.createTempFile("test", "simplify"); classManager.getDexBuilder().writeTo(new FileDataStore(out)); out.delete(); }
public void writeTo(@Nonnull final File outFolder) { try { if (!outFolder.mkdirs()) { System.err.println("No output folder " + outFolder); return; } writeTo(dexNum -> new FileDataStore(new File(outFolder, getDexFileName(dexNum)))); } catch (IOException ex) { ex.printStackTrace(); } }
public static void writeTo(@Nonnull String path, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException { DexPool dexPool = makeDexPool(); for (ClassDef classDef: input.getClasses()) { ((ClassPool)dexPool.classSection).intern(classDef); } dexPool.writeTo(new FileDataStore(new File(path))); }
public static void writeTo(@Nonnull String path, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException { DexPool dexPool = new DexPool(input.getOpcodes()); for (ClassDef classDef: input.getClasses()) { dexPool.internClass(classDef); } dexPool.writeTo(new FileDataStore(new File(path))); }
public static void writeTo(@Nonnull String path, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException { DexPool dexPool = new DexPool(input.getOpcodes()); for (ClassDef classDef: input.getClasses()) { dexPool.internClass(classDef); } dexPool.writeTo(new FileDataStore(new File(path))); }
/** * 将smali文件夹转换为dex文件 * @param smaliFolder * @param outDexFile * @return */ public static boolean assembleSmaliFile(File smaliFolder,File outDexFile) throws IOException, RecognitionException { Collection<File> smaliFiles = FileUtils.listFiles(smaliFolder, new String[]{"smali"}, true); if(null!= smaliFiles && smaliFiles.size() > 0){ DexBuilder dexBuilder = DexBuilder.makeDexBuilder(); for(File smaliFile:smaliFiles){ SmaliMod.assembleSmaliFile(smaliFile, dexBuilder, true, true); } dexBuilder.writeTo(new FileDataStore(outDexFile)); return true; }else{ return false; } }
dexBuilder.writeTo(new FileDataStore(new File(options.outputDexFile)));
dexBuilder.writeTo(new FileDataStore(new File(outputDexFile))); } catch (RuntimeException ex) { System.err.println("\nUNEXPECTED TOP-LEVEL EXCEPTION:");
dexBuilder.writeTo(new FileDataStore(new File(dexFileName))); Logger.log("build the dexfile ok"); return true;
public static Set<String> buildCode(File smaliDir, File dexFile, DexDiffInfo info) throws IOException, RecognitionException { Set<String> classes = new HashSet<String>(); Set<DexBackedClassDef> classDefs = new HashSet<DexBackedClassDef>(); classDefs.addAll(info.getModifiedClasses()); classDefs.addAll(info.getAddedClasses()); final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali"); final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali"); DexBuilder dexBuilder = DexBuilder.makeDexBuilder(); File smaliFile; String className; for (DexBackedClassDef classDef : classDefs) { ApkPatch.currentClassType = classDef.getType(); className = TypeGenUtil.newType(classDef.getType()); AfBakSmali.disassembleClass(classDef, outFileNameHandler, getBuildOption(classDefs, 19), false, false); smaliFile = inFileNameHandler.getUniqueFilenameForClass(className); classes.add(className.substring(1, className.length() - 1).replace('/', '.')); SmaliMod.assembleSmaliFile(smaliFile, dexBuilder, true, true); } dexBuilder.writeTo(new FileDataStore(dexFile)); return classes; }
FileDataStore fileDataStore = new FileDataStore(dest); dexBuilder.writeTo(fileDataStore);
@Test public void testBuilderCallSite() throws IOException { DexBuilder dexBuilder = new DexBuilder(Opcodes.forArtVersion(111)); BuilderCallSiteReference callSite = dexBuilder.internCallSite(new ImmutableCallSiteReference("call_site_1", new ImmutableMethodHandleReference( MethodHandleType.INVOKE_STATIC, new ImmutableMethodReference("Lcls1", "loader", ImmutableList.of("Ljava/lang/invoke/Lookup;", "Ljava/lang/String;", "Ljava/lang/invoke/MethodType;"), "Ljava/lang/invoke/CallSite;")), "someMethod", new ImmutableMethodProtoReference(ImmutableList.of(), "V"), ImmutableList.of())); MethodImplementationBuilder methodImplementationBuilder = new MethodImplementationBuilder(10); methodImplementationBuilder.addInstruction(new BuilderInstruction35c(Opcode.INVOKE_CUSTOM, 0, 0, 0, 0, 0, 0, callSite)); BuilderMethod method = dexBuilder.internMethod("Lcls1", "method1", null, "V", 0, ImmutableSet.of(), methodImplementationBuilder.getMethodImplementation()); dexBuilder.internClassDef("Lcls1;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, ImmutableSet.of(), null, ImmutableList.of(method)); File tempFile = File.createTempFile("dex", ".dex"); dexBuilder.writeTo(new FileDataStore(tempFile)); verifyDexFile(DexFileFactory.loadDexFile(tempFile, Opcodes.forArtVersion(111))); }