com.android.dx.merge.CollisionPolicy Java Examples

The following examples show how to use com.android.dx.merge.CollisionPolicy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: rhino-android   Author: F43nd1r   File: BaseAndroidClassLoader.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Class<?> defineClass(String name, byte[] data) {
    try {
        DexOptions dexOptions = new DexOptions();
        DexFile dexFile = new DexFile(dexOptions);
        DirectClassFile classFile = new DirectClassFile(data, name.replace('.', '/') + ".class", true);
        classFile.setAttributeFactory(StdAttributeFactory.THE_ONE);
        classFile.getMagic();
        DxContext context = new DxContext();
        dexFile.add(CfTranslator.translate(context, classFile, null, new CfOptions(), dexOptions, dexFile));
        Dex dex = new Dex(dexFile.toDex(null, false));
        Dex oldDex = getLastDex();
        if (oldDex != null) {
            dex = new DexMerger(new Dex[]{dex, oldDex}, CollisionPolicy.KEEP_FIRST, context).merge();
        }
        return loadClass(dex, name);
    } catch (IOException | ClassNotFoundException e) {
        throw new FatalLoadingException(e);
    }
}
 
Example #2
Source Project: buck   Author: facebook   File: Main.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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[0]),
        CollisionPolicy.FAIL,
        context);
    Dex merged = dexMerger.merge();
    return merged.getBytes();
}
 
Example #3
Source Project: Box   Author: lulululbj   File: Main.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #4
Source Project: Box   Author: lulululbj   File: Main.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #5
Source Project: atlas   Author: alibaba   File: DexMerger.java    License: Apache License 2.0 4 votes vote down vote up
private void mergeDex(File outDexFolder, List<Dex> tmpList, int index, Dex[] mergedList) throws IOException {

        com.android.dx.merge.DexMerger dexMerger = new com.android.dx.merge.DexMerger(
            tmpList.toArray(new Dex[0]),
            CollisionPolicy.KEEP_FIRST,new DxContext());

        dexMerger.setCompactWasteThreshold(1024);

        Dex dex = dexMerger.merge();

        mergedList[index] = dex;

        if (dexList.isEmpty()) {
            logger.warn("dexCount of {}, methods {} , fields {}", index, dex.getTableOfContents().methodIds.size,
                        dex.getTableOfContents().fieldIds.size);

            String name = "classes" + (0 == index ? "" : String.valueOf(index + 1)) + ".dex";

            File dexFile = new File(outDexFolder, name);

            dex.writeTo(dexFile);
        }
    }
 
Example #6
Source Project: atlas   Author: alibaba   File: FastMultiDexer.java    License: Apache License 2.0 3 votes vote down vote up
private void mergeDex(File outDexFolder, List<Dex> tmpList, int index, Dex[] mergedList) throws IOException {

        DexMerger dexMerger = new DexMerger(tmpList.toArray(new Dex[0]), CollisionPolicy.KEEP_FIRST,new DxContext());
        Dex dex = dexMerger.merge();

        mergedList[index] = dex;

        String name = "classes" + (0 == index ? "" : String.valueOf(index + 1)) + ".dex";

        File dexFile = new File(outDexFolder, name);

        dex.writeTo(dexFile);
    }