Search in sources :

Example 6 with Dex

use of com.android.dex.Dex in project buck by facebook.

the class DexMergeTest method mergeAndLoad.

public ClassLoader mergeAndLoad(String dexAResource, String dexBResource) throws Exception {
    Dex dexA = resourceToDexBuffer(dexAResource);
    Dex dexB = resourceToDexBuffer(dexBResource);
    Dex merged = new DexMerger(dexA, dexB, CollisionPolicy.KEEP_FIRST).merge();
    File mergedDex = File.createTempFile("DexMergeTest", ".classes.dex");
    merged.writeTo(mergedDex);
    File mergedJar = dexToJar(mergedDex);
    // simplify the javac classpath by not depending directly on 'dalvik.system' classes
    return (ClassLoader) Class.forName("dalvik.system.PathClassLoader").getConstructor(String.class, ClassLoader.class).newInstance(mergedJar.getPath(), getClass().getClassLoader());
}
Also used : Dex(com.android.dex.Dex) File(java.io.File)

Example 7 with Dex

use of com.android.dex.Dex in project buck by facebook.

the class MergeTest method main.

public static void main(String[] args) throws Throwable {
    for (int i = 0; i < NUMBER_OF_TRIES; i++) {
        String fileName1 = args[(int) (Math.random() * args.length)];
        String fileName2 = args[(int) (Math.random() * args.length)];
        try {
            Dex toMerge = new Dex(new File(fileName1));
            Dex toMerge2 = new Dex(new File(fileName2));
            new DexMerger(toMerge, toMerge2, CollisionPolicy.KEEP_FIRST).merge();
        } catch (DexIndexOverflowException e) {
        // ignore index overflow
        } catch (Throwable t) {
            System.err.println("Problem merging those 2 dexes: \"" + fileName1 + "\" and \"" + fileName2 + "\"");
            throw t;
        }
    }
}
Also used : DexIndexOverflowException(com.android.dex.DexIndexOverflowException) Dex(com.android.dex.Dex) File(java.io.File)

Example 8 with Dex

use of com.android.dex.Dex in project buck by facebook.

the class DexMerger method mergeClassDefs.

private void mergeClassDefs() {
    SortableType[] types = getSortedTypes();
    contentsOut.classDefs.off = idsDefsOut.getPosition();
    contentsOut.classDefs.size = types.length;
    for (SortableType type : types) {
        Dex in = type.getDex();
        transformClassDef(in, type.getClassDef(), type.getIndexMap());
    }
}
Also used : Dex(com.android.dex.Dex)

Example 9 with Dex

use of com.android.dex.Dex in project jadx by skylot.

the class InputFile method loadFromZip.

private boolean loadFromZip(String ext) throws IOException, DecodeException {
    ZipFile zf = new ZipFile(file);
    int index = 0;
    while (true) {
        String entryName = "classes" + (index == 0 ? "" : index) + ext;
        ZipEntry entry = zf.getEntry(entryName);
        if (entry == null) {
            break;
        }
        InputStream inputStream = zf.getInputStream(entry);
        try {
            if (ext.equals(".dex")) {
                addDexFile(entryName, new Dex(inputStream));
            } else if (ext.equals(".jar")) {
                File jarFile = FileUtils.createTempFile(entryName);
                FileOutputStream fos = new FileOutputStream(jarFile);
                try {
                    IOUtils.copy(inputStream, fos);
                } finally {
                    close(fos);
                }
                addDexFile(entryName, loadFromJar(jarFile));
            } else {
                throw new JadxRuntimeException("Unexpected extension in zip: " + ext);
            }
        } finally {
            close(inputStream);
        }
        index++;
        if (index == 1) {
            index = 2;
        }
    }
    zf.close();
    return index > 0;
}
Also used : ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) Dex(com.android.dex.Dex) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 10 with Dex

use of com.android.dex.Dex in project J2ME-Loader by nikita36078.

the class Main method mergeLibraryDexBuffers.

/**
 * Merges the dex files in library jars. If multiple dex files define the
 * same type, this fails with an exception.
 */
private byte[] mergeLibraryDexBuffers(byte[] outArray) throws IOException {
    ArrayList<Dex> dexes = new ArrayList<Dex>();
    if (outArray != null) {
        dexes.add(new Dex(outArray));
    }
    for (byte[] libraryDex : libraryDexBuffers) {
        dexes.add(new Dex(libraryDex));
    }
    if (dexes.isEmpty()) {
        return null;
    }
    Dex merged = new DexMerger(dexes.toArray(new Dex[dexes.size()]), CollisionPolicy.FAIL, context).merge();
    return merged.getBytes();
}
Also used : Dex(com.android.dex.Dex) ArrayList(java.util.ArrayList) DexMerger(com.android.dx.merge.DexMerger)

Aggregations

Dex (com.android.dex.Dex)22 File (java.io.File)7 DexMerger (com.android.dx.merge.DexMerger)5 DxContext (com.android.dx.command.dexer.DxContext)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 ZipEntry (java.util.zip.ZipEntry)2 DexException (com.android.dex.DexException)1 DexIndexOverflowException (com.android.dex.DexIndexOverflowException)1 StaticValuesParser (jadx.core.dex.nodes.parser.StaticValuesParser)1 DecodeException (jadx.core.utils.exceptions.DecodeException)1 JadxException (jadx.core.utils.exceptions.JadxException)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1 FileOutputStream (java.io.FileOutputStream)1 BufferOverflowException (java.nio.BufferOverflowException)1 ZipFile (java.util.zip.ZipFile)1