Search in sources :

Example 1 with DexMerger

use of com.android.dx.merge.DexMerger in project bazel by bazelbuild.

the class DexFileAggregator method merge.

private Dex merge(Dex... dexes) throws IOException {
    switch(dexes.length) {
        case 0:
            return new Dex(0);
        case 1:
            return dexes[0];
        default:
            try {
                DexMerger dexMerger = new DexMerger(dexes, CollisionPolicy.FAIL);
                dexMerger.setCompactWasteThreshold(wasteThresholdPerDex);
                return dexMerger.merge();
            } catch (BufferOverflowException e) {
                // Bug in dx can cause this for ~1500 or more classes
                Dex[] left = Arrays.copyOf(dexes, dexes.length / 2);
                Dex[] right = Arrays.copyOfRange(dexes, left.length, dexes.length);
                System.err.printf("Couldn't merge %d classes, trying %d%n", dexes.length, left.length);
                try {
                    return merge(merge(left), merge(right));
                } catch (RuntimeException e2) {
                    e2.addSuppressed(e);
                    throw e2;
                }
            }
    }
}
Also used : Dex(com.android.dex.Dex) DexMerger(com.android.dx.merge.DexMerger) BufferOverflowException(java.nio.BufferOverflowException)

Example 2 with DexMerger

use of com.android.dx.merge.DexMerger in project buck by facebook.

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;
    }
    DexMerger dexMerger = new DexMerger(dexes.toArray(new Dex[dexes.size()]), CollisionPolicy.FAIL, context);
    Dex merged = dexMerger.merge();
    return merged.getBytes();
}
Also used : Dex(com.android.dex.Dex) ArrayList(java.util.ArrayList) DexMerger(com.android.dx.merge.DexMerger)

Example 3 with DexMerger

use of com.android.dx.merge.DexMerger in project buck by facebook.

the class Main method mergeIncremental.

/**
     * Merges the dex files {@code update} and {@code base}, preferring
     * {@code update}'s definition for types defined in both dex files.
     *
     * @param base a file to find the previous dex file. May be a .dex file, a
     *     jar file possibly containing a .dex file, or null.
     * @return the bytes of the merged dex file, or null if both the update
     *     and the base dex do not exist.
     */
private byte[] mergeIncremental(byte[] update, File base) throws IOException {
    Dex dexA = null;
    Dex dexB = null;
    if (update != null) {
        dexA = new Dex(update);
    }
    if (base.exists()) {
        dexB = new Dex(base);
    }
    Dex result;
    if (dexA == null && dexB == null) {
        return null;
    } else if (dexA == null) {
        result = dexB;
    } else if (dexB == null) {
        result = dexA;
    } else {
        DexMerger dexMerger = new DexMerger(new Dex[] { dexA, dexB }, CollisionPolicy.KEEP_FIRST, context);
        result = dexMerger.merge();
    }
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    result.writeTo(bytesOut);
    return bytesOut.toByteArray();
}
Also used : Dex(com.android.dex.Dex) DexMerger(com.android.dx.merge.DexMerger) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with DexMerger

use of com.android.dx.merge.DexMerger 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)

Example 5 with DexMerger

use of com.android.dx.merge.DexMerger in project J2ME-Loader by nikita36078.

the class Main method mergeIncremental.

/**
 * Merges the dex files {@code update} and {@code base}, preferring
 * {@code update}'s definition for types defined in both dex files.
 *
 * @param base a file to find the previous dex file. May be a .dex file, a
 *     jar file possibly containing a .dex file, or null.
 * @return the bytes of the merged dex file, or null if both the update
 *     and the base dex do not exist.
 */
private byte[] mergeIncremental(byte[] update, File base) throws IOException {
    Dex dexA = null;
    Dex dexB = null;
    if (update != null) {
        dexA = new Dex(update);
    }
    if (base.exists()) {
        dexB = new Dex(base);
    }
    Dex result;
    if (dexA == null && dexB == null) {
        return null;
    } else if (dexA == null) {
        result = dexB;
    } else if (dexB == null) {
        result = dexA;
    } else {
        result = new DexMerger(new Dex[] { dexA, dexB }, CollisionPolicy.KEEP_FIRST, context).merge();
    }
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    result.writeTo(bytesOut);
    return bytesOut.toByteArray();
}
Also used : Dex(com.android.dex.Dex) DexMerger(com.android.dx.merge.DexMerger) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

DexMerger (com.android.dx.merge.DexMerger)9 Dex (com.android.dex.Dex)8 DxContext (com.android.dx.command.dexer.DxContext)3 File (java.io.File)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 GradleException (org.gradle.api.GradleException)2 TaskContainerAdaptor (com.android.build.gradle.internal.TaskContainerAdaptor)1 PackageApplication (com.android.build.gradle.tasks.PackageApplication)1 DexException (com.android.dex.DexException)1 DirectClassFile (com.android.dx.cf.direct.DirectClassFile)1 DexOptions (com.android.dx.dex.DexOptions)1 CfOptions (com.android.dx.dex.cf.CfOptions)1 AtlasDependencyTree (com.taobao.android.builder.dependency.AtlasDependencyTree)1 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)1 FirstApkAction (com.taobao.android.builder.tasks.app.bundle.actions.FirstApkAction)1 LastApkAction (com.taobao.android.builder.tasks.app.bundle.actions.LastApkAction)1 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)1 ExecutorServicesHelper (com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper)1 FileLogger (com.taobao.android.builder.tools.log.FileLogger)1