Search in sources :

Example 1 with DexPool

use of org.jf.dexlib2.writer.pool.DexPool 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)

Example 2 with DexPool

use of org.jf.dexlib2.writer.pool.DexPool in project smali by JesusFreke.

the class DexPool method writeTo.

public static void writeTo(@Nonnull DexDataStore dataStore, @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(dataStore);
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef)

Example 3 with DexPool

use of org.jf.dexlib2.writer.pool.DexPool in project smali by JesusFreke.

the class RollbackTest method testRollback.

@Test
public void testRollback() throws IOException {
    ClassDef class1 = new ImmutableClassDef("Lcls1;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation;", null)), Lists.<Field>newArrayList(new ImmutableField("Lcls1;", "field1", "I", AccessFlags.PUBLIC.getValue(), null, null, null)), Lists.<Method>newArrayList(new ImmutableMethod("Lcls1;", "method1", Lists.<MethodParameter>newArrayList(new ImmutableMethodParameter("I", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, null, null)));
    ClassDef class2 = new ImmutableClassDef("Lcls2;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation2;", null)), Lists.<Field>newArrayList(new ImmutableField("Lcls2;", "field2", "D", AccessFlags.PUBLIC.getValue(), null, null, null)), Lists.<Method>newArrayList(new ImmutableMethod("Lcls2;", "method2", Lists.<MethodParameter>newArrayList(new ImmutableMethodParameter("D", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, null, null)));
    DexBackedDexFile dexFile1;
    {
        MemoryDataStore dataStore = new MemoryDataStore();
        DexPool dexPool = new DexPool(Opcodes.getDefault());
        dexPool.internClass(class1);
        dexPool.mark();
        dexPool.internClass(class2);
        dexPool.reset();
        dexPool.writeTo(dataStore);
        dexFile1 = new DexBackedDexFile(Opcodes.getDefault(), dataStore.getBuffer());
    }
    DexBackedDexFile dexFile2;
    {
        MemoryDataStore dataStore = new MemoryDataStore();
        DexPool dexPool = new DexPool(Opcodes.getDefault());
        dexPool.internClass(class1);
        dexPool.writeTo(dataStore);
        dexFile2 = new DexBackedDexFile(Opcodes.getDefault(), dataStore.getBuffer());
    }
    List<MapItem> mapItems1 = dexFile1.getMapItems();
    List<MapItem> mapItems2 = dexFile2.getMapItems();
    for (int i = 0; i < mapItems1.size(); i++) {
        Assert.assertEquals(mapItems1.get(i).getType(), mapItems2.get(i).getType());
        Assert.assertEquals(mapItems1.get(i).getItemCount(), mapItems2.get(i).getItemCount());
    }
}
Also used : DexPool(org.jf.dexlib2.writer.pool.DexPool) ClassDef(org.jf.dexlib2.iface.ClassDef) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) MemoryDataStore(org.jf.dexlib2.writer.io.MemoryDataStore) MapItem(org.jf.dexlib2.dexbacked.raw.MapItem) Test(org.junit.Test)

Example 4 with DexPool

use of org.jf.dexlib2.writer.pool.DexPool in project soot by Sable.

the class MultiDexBuilder method newDexPool.

protected void newDexPool() {
    curPool = new DexPool(opcodes);
    dexPools.add(curPool);
}
Also used : DexPool(org.jf.dexlib2.writer.pool.DexPool)

Example 5 with DexPool

use of org.jf.dexlib2.writer.pool.DexPool in project soot by Sable.

the class MultiDexBuilder method writeTo.

/**
 * 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;
}
Also used : DexPool(org.jf.dexlib2.writer.pool.DexPool) ArrayList(java.util.ArrayList) File(java.io.File) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore)

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)4 DexPool (org.jf.dexlib2.writer.pool.DexPool)4 File (java.io.File)3 FileDataStore (org.jf.dexlib2.writer.io.FileDataStore)3 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1 Opcodes (org.jf.dexlib2.Opcodes)1 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)1 MapItem (org.jf.dexlib2.dexbacked.raw.MapItem)1 DexFile (org.jf.dexlib2.iface.DexFile)1 MemoryDataStore (org.jf.dexlib2.writer.io.MemoryDataStore)1 BasePool (org.jf.dexlib2.writer.pool.BasePool)1 Test (org.junit.Test)1