Search in sources :

Example 6 with BaksmaliOptions

use of org.jf.baksmali.BaksmaliOptions in project atlas by alibaba.

the class SmaliUtils method disassembleDexFile.

/**
 * 将dex文件转换为smali文件
 * @param dex
 * @param outputDir
 * @param includeClasses 需要做过滤的文件
 */
public static boolean disassembleDexFile(File dex, File outputDir, final Set<String> includeClasses) throws IOException {
    final BaksmaliOptions options = createBaksmaliOptions();
    if (!outputDir.exists()) {
        outputDir.mkdirs();
    }
    DexFile dexFile = DexFileFactory.loadDexFile(dex, Opcodes.getDefault());
    List<? extends ClassDef> classDefs = Ordering.natural().sortedCopy(dexFile.getClasses());
    final ClassFileNameHandler fileNameHandler = new ClassFileNameHandler(outputDir, ".smali");
    ExecutorService executor = Executors.newFixedThreadPool(4);
    List<Future<Boolean>> tasks = Lists.newArrayList();
    Set<String> classSet = null;
    if (includeClasses != null) {
        classSet = new HashSet<String>(includeClasses);
    }
    for (final ClassDef classDef : classDefs) {
        String className = getDalvikClassName(classDef.getType());
        if (classSet != null && !classSet.contains(className)) {
            continue;
        }
        tasks.add(executor.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return BakSmali.disassembleClass(classDef, fileNameHandler, options);
            }
        }));
    }
    boolean errorOccurred = false;
    try {
        for (Future<Boolean> task : tasks) {
            while (true) {
                try {
                    if (!task.get()) {
                        errorOccurred = true;
                    }
                } catch (InterruptedException ex) {
                    continue;
                } catch (ExecutionException ex) {
                    throw new RuntimeException(ex);
                }
                break;
            }
        }
    } finally {
        executor.shutdown();
    }
    return !errorOccurred;
}
Also used : ClassFileNameHandler(org.jf.util.ClassFileNameHandler) DexFile(org.jf.dexlib2.iface.DexFile) ClassDef(org.jf.dexlib2.iface.ClassDef) BaksmaliOptions(org.jf.baksmali.BaksmaliOptions)

Aggregations

BaksmaliOptions (org.jf.baksmali.BaksmaliOptions)4 SyntheticAccessorResolver (org.jf.dexlib2.util.SyntheticAccessorResolver)3 org.jf.baksmali.baksmaliOptions (org.jf.baksmali.baksmaliOptions)2 ClassDef (org.jf.dexlib2.iface.ClassDef)2 AndrolibException (brut.androlib.AndrolibException)1 IOException (java.io.IOException)1 ClassDefinition (org.jf.baksmali.Adaptors.ClassDefinition)1 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)1 DexBackedOdexFile (org.jf.dexlib2.dexbacked.DexBackedOdexFile)1 DexFile (org.jf.dexlib2.iface.DexFile)1 ClassFileNameHandler (org.jf.util.ClassFileNameHandler)1 IndentingWriter (org.jf.util.IndentingWriter)1