Search in sources :

Example 1 with FileDataStore

use of org.jf.dexlib2.writer.io.FileDataStore in project smali by JesusFreke.

the class Smali method assemble.

/**
     * Assemble the specified files, using the given options
     *
     * @param options a SmaliOptions object with the options to run smali with
     * @param input The files/directories to process
     * @return true if assembly completed with no errors, or false if errors were encountered
     */
public static boolean assemble(final SmaliOptions options, List<String> input) throws IOException {
    LinkedHashSet<File> filesToProcessSet = new LinkedHashSet<File>();
    for (String fileToProcess : input) {
        File argFile = new File(fileToProcess);
        if (!argFile.exists()) {
            throw new IllegalArgumentException("Cannot find file or directory \"" + fileToProcess + "\"");
        }
        if (argFile.isDirectory()) {
            getSmaliFilesInDir(argFile, filesToProcessSet);
        } else if (argFile.isFile()) {
            filesToProcessSet.add(argFile);
        }
    }
    boolean errors = false;
    final DexBuilder dexBuilder = new DexBuilder(Opcodes.forApi(options.apiLevel));
    ExecutorService executor = Executors.newFixedThreadPool(options.jobs);
    List<Future<Boolean>> tasks = Lists.newArrayList();
    for (final File file : filesToProcessSet) {
        tasks.add(executor.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return assembleSmaliFile(file, dexBuilder, options);
            }
        }));
    }
    for (Future<Boolean> task : tasks) {
        while (true) {
            try {
                try {
                    if (!task.get()) {
                        errors = true;
                    }
                } catch (ExecutionException ex) {
                    throw new RuntimeException(ex);
                }
            } catch (InterruptedException ex) {
                continue;
            }
            break;
        }
    }
    executor.shutdown();
    if (errors) {
        return false;
    }
    dexBuilder.writeTo(new FileDataStore(new File(options.outputDexFile)));
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder) File(java.io.File) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore)

Example 2 with FileDataStore

use of org.jf.dexlib2.writer.io.FileDataStore in project Apktool by iBotPeaches.

the class SmaliBuilder method build.

private void build() throws AndrolibException {
    try {
        DexBuilder dexBuilder;
        if (mApiLevel > 0) {
            dexBuilder = DexBuilder.makeDexBuilder(Opcodes.forApi(mApiLevel));
        } else {
            dexBuilder = DexBuilder.makeDexBuilder();
        }
        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);
    }
}
Also used : AndrolibException(brut.androlib.AndrolibException) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore) ExtFile(brut.directory.ExtFile) DirectoryException(brut.directory.DirectoryException) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder)

Example 3 with FileDataStore

use of org.jf.dexlib2.writer.io.FileDataStore in project atlas by alibaba.

the class SmaliUtils method assembleSmaliFile.

/**
     * 将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;
    }
}
Also used : File(java.io.File) DexFile(org.jf.dexlib2.iface.DexFile) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder)

Example 4 with FileDataStore

use of org.jf.dexlib2.writer.io.FileDataStore in project atlas by alibaba.

the class SmaliDiffUtils method buildCode.

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;
}
Also used : ClassFileNameHandler(org.jf.util.ClassFileNameHandler) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) File(java.io.File) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore) HashSet(java.util.HashSet) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder)

Example 5 with FileDataStore

use of org.jf.dexlib2.writer.io.FileDataStore in project smali by JesusFreke.

the class DexPool method writeTo.

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)));
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore) File(java.io.File)

Aggregations

FileDataStore (org.jf.dexlib2.writer.io.FileDataStore)5 File (java.io.File)4 DexBuilder (org.jf.dexlib2.writer.builder.DexBuilder)4 AndrolibException (brut.androlib.AndrolibException)1 DirectoryException (brut.directory.DirectoryException)1 ExtFile (brut.directory.ExtFile)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)1 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)1 ClassDef (org.jf.dexlib2.iface.ClassDef)1 DexFile (org.jf.dexlib2.iface.DexFile)1 ClassFileNameHandler (org.jf.util.ClassFileNameHandler)1