Search in sources :

Example 1 with BasePool

use of org.jf.dexlib2.writer.pool.BasePool in project atlas by alibaba.

the class PatchDexTool method writeDex.

public void writeDex(File outDexFolder, Set<ClassDef> classDefs) throws IOException {
    int i = 0;
    File outDexFile = getDexFile(outDexFolder, i);
    if (!outDexFile.getParentFile().exists()) {
        outDexFile.getParentFile().mkdirs();
    }
    if (mainBundle) {
        List<ClassDef> sortClassDefs = sort(classDefs);
        DexPool dexPool = new DexPool(Opcodes.getDefault());
        Iterator<ClassDef> iterator = sortClassDefs.iterator();
        while (iterator.hasNext()) {
            ClassDef classDef = iterator.next();
            dexPool.internClass(classDef);
            if (((BasePool) dexPool.methodSection).getItemCount() > MAX_COUNT || ((BasePool) dexPool.fieldSection).getItemCount() > MAX_COUNT) {
                dexPool.writeTo(new FileDataStore(outDexFile));
                outDexFile = getDexFile(outDexFolder, ++i);
                dexPool = new DexPool(Opcodes.getDefault());
            }
        }
        dexPool.writeTo(new FileDataStore(outDexFile));
    } else {
        DexFileFactory.writeDexFile(outDexFile.getAbsolutePath(), new DexFile() {

            @Nonnull
            @Override
            public Set<? extends ClassDef> getClasses() {
                return new AbstractSet<ClassDef>() {

                    @Nonnull
                    @Override
                    public Iterator<ClassDef> iterator() {
                        return classDefs.iterator();
                    }

                    @Override
                    public int size() {
                        return classDefs.size();
                    }
                };
            }

            @Nonnull
            @Override
            public Opcodes getOpcodes() {
                return Opcodes.getDefault();
            }
        });
    }
}
Also used : Nonnull(javax.annotation.Nonnull) DexFile(org.jf.dexlib2.iface.DexFile) DexPool(org.jf.dexlib2.writer.pool.DexPool) BasePool(org.jf.dexlib2.writer.pool.BasePool) ClassDef(org.jf.dexlib2.iface.ClassDef) Opcodes(org.jf.dexlib2.Opcodes) File(java.io.File) DexFile(org.jf.dexlib2.iface.DexFile) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore)

Aggregations

File (java.io.File)1 Nonnull (javax.annotation.Nonnull)1 Opcodes (org.jf.dexlib2.Opcodes)1 ClassDef (org.jf.dexlib2.iface.ClassDef)1 DexFile (org.jf.dexlib2.iface.DexFile)1 FileDataStore (org.jf.dexlib2.writer.io.FileDataStore)1 BasePool (org.jf.dexlib2.writer.pool.BasePool)1 DexPool (org.jf.dexlib2.writer.pool.DexPool)1