Java Code Examples for com.android.dex.Dex#writeTo()

The following examples show how to use com.android.dex.Dex#writeTo() . 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 File: DexMerger.java    From Box with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        printUsage();
        return;
    }

    Dex[] dexes = new Dex[args.length - 1];
    for (int i = 1; i < args.length; i++) {
        dexes[i - 1] = new Dex(new File(args[i]));
    }
    Dex merged = new DexMerger(dexes, CollisionPolicy.KEEP_FIRST, new DxContext()).merge();
    merged.writeTo(new File(args[0]));
}
 
Example 2
Source File: DexMerger.java    From Box with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        printUsage();
        return;
    }

    Dex[] dexes = new Dex[args.length - 1];
    for (int i = 1; i < args.length; i++) {
        dexes[i - 1] = new Dex(new File(args[i]));
    }
    Dex merged = new DexMerger(dexes, CollisionPolicy.KEEP_FIRST, new DxContext()).merge();
    merged.writeTo(new File(args[0]));
}
 
Example 3
Source File: FileAndroidClassLoader.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Override
protected Class<?> loadClass(@NonNull Dex dex, @NonNull String name) throws ClassNotFoundException {
    try {
        dex.writeTo(dexFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new PathClassLoader(dexFile.getPath(), getParent()).loadClass(name);
}
 
Example 4
Source File: DexFileArchive.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a {@code .dex} file with the given details.
 */
public DexFileArchive addFile(ZipEntry entry, Dex dex) throws IOException {
  checkState(inUse.compareAndSet(null, entry), "Already in use");
  entry.setSize(dex.getLength());
  out.putNextEntry(entry);
  dex.writeTo(out);
  out.closeEntry();
  checkState(inUse.compareAndSet(entry, null), "Swooped in: ", inUse.get());
  return this;
}
 
Example 5
Source File: DexMerger.java    From buck with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        printUsage();
        return;
    }

    Dex[] dexes = new Dex[args.length - 1];
    for (int i = 1; i < args.length; i++) {
        dexes[i - 1] = new Dex(new File(args[i]));
    }
    Dex merged = new DexMerger(dexes, CollisionPolicy.KEEP_FIRST, new DxContext()).merge();
    merged.writeTo(new File(args[0]));
}
 
Example 6
Source File: DexMergeTest.java    From buck with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 7
Source File: DexMerger.java    From atlas with 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 8
Source File: FastMultiDexer.java    From atlas with 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);
    }