Search in sources :

Example 1 with DexFile

use of com.taobao.android.dx.dex.file.DexFile in project atlas by alibaba.

the class Main method runMonoDex.

private int runMonoDex() throws IOException {
    File incrementalOutFile = null;
    if (args.incremental) {
        if (args.outName == null) {
            System.err.println("error: no incremental output name specified");
            return -1;
        }
        incrementalOutFile = new File(args.outName);
        if (incrementalOutFile.exists()) {
            minimumFileAge = incrementalOutFile.lastModified();
        }
    }
    if (!processAllFiles()) {
        return 1;
    }
    if (args.incremental && !anyFilesProcessed) {
        // this was a no-op incremental build
        return 0;
    }
    // this array is null if no classes were defined
    byte[] outArray = null;
    if (!outputDex.isEmpty() || (args.humanOutName != null)) {
        outArray = writeDex(outputDex);
        if (outArray == null) {
            return 2;
        }
    }
    if (args.incremental) {
        outArray = mergeIncremental(outArray, incrementalOutFile);
    }
    outArray = mergeLibraryDexBuffers(outArray);
    if (args.jarOutput) {
        // Effectively free up the (often massive) DexFile memory.
        outputDex = null;
        if (outArray != null) {
            outputResources.put(DexFormat.DEX_IN_JAR_NAME, outArray);
        }
        if (!createJar(args.outName)) {
            return 3;
        }
    } else if (outArray != null && args.outName != null) {
        OutputStream out = openOutput(args.outName);
        out.write(outArray);
        closeOutput(out);
    }
    return 0;
}
Also used : JarOutputStream(java.util.jar.JarOutputStream) DexFile(com.taobao.android.dx.dex.file.DexFile) DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile)

Example 2 with DexFile

use of com.taobao.android.dx.dex.file.DexFile in project atlas by alibaba.

the class Main method runMultiDex.

private int runMultiDex() throws IOException {
    assert !args.incremental;
    if (args.mainDexListFile != null) {
        classesInMainDex = new HashSet<String>();
        readPathsFromFile(args.mainDexListFile, classesInMainDex);
    }
    dexOutPool = Executors.newFixedThreadPool(args.numThreads);
    if (!processAllFiles()) {
        return 1;
    }
    if (!libraryDexBuffers.isEmpty()) {
        throw new DexException("Library dex files are not supported in multi-dex mode");
    }
    if (outputDex != null) {
        // this array is null if no classes were defined
        dexOutputFutures.add(dexOutPool.submit(new DexWriter(outputDex)));
        // Effectively free up the (often massive) DexFile memory.
        outputDex = null;
    }
    try {
        dexOutPool.shutdown();
        if (!dexOutPool.awaitTermination(600L, TimeUnit.SECONDS)) {
            throw new RuntimeException("Timed out waiting for dex writer threads.");
        }
        for (Future<byte[]> f : dexOutputFutures) {
            dexOutputArrays.add(f.get());
        }
    } catch (InterruptedException ex) {
        dexOutPool.shutdownNow();
        throw new RuntimeException("A dex writer thread has been interrupted.");
    } catch (Exception e) {
        dexOutPool.shutdownNow();
        throw new RuntimeException("Unexpected exception in dex writer thread");
    }
    if (args.jarOutput) {
        for (int i = 0; i < dexOutputArrays.size(); i++) {
            outputResources.put(getDexFileName(i), dexOutputArrays.get(i));
        }
        if (!createJar(args.outName)) {
            return 3;
        }
    } else if (args.outName != null) {
        File outDir = new File(args.outName);
        assert outDir.isDirectory();
        for (int i = 0; i < dexOutputArrays.size(); i++) {
            OutputStream out = new FileOutputStream(new File(outDir, getDexFileName(i)));
            try {
                out.write(dexOutputArrays.get(i));
            } finally {
                closeOutput(out);
            }
        }
    }
    return 0;
}
Also used : DexException(com.taobao.android.dex.DexException) JarOutputStream(java.util.jar.JarOutputStream) CstString(com.taobao.android.dx.rop.cst.CstString) DexFile(com.taobao.android.dx.dex.file.DexFile) DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) DexException(com.taobao.android.dex.DexException) UsageException(com.taobao.android.dx.command.UsageException) ParseException(com.taobao.android.dx.cf.iface.ParseException) SimException(com.taobao.android.dx.cf.code.SimException)

Example 3 with DexFile

use of com.taobao.android.dx.dex.file.DexFile in project atlas by alibaba.

the class Main method processClass.

/**
     * Processes one classfile.
     *
     * @param name {@code non-null;} name of the file, clipped such that it
     * <i>should</i> correspond to the name of the class it contains
     * @param bytes {@code non-null;} contents of the file
     * @return whether processing was successful
     */
private boolean processClass(String name, byte[] bytes) {
    if (!args.coreLibrary) {
        checkClassName(name);
    }
    DirectClassFile cf = new DirectClassFile(bytes, name, args.cfOptions.strictNameCheck);
    cf.setAttributeFactory(StdAttributeFactory.THE_ONE);
    cf.getMagic();
    int numMethodIds = outputDex.getMethodIds().items().size();
    int numFieldIds = outputDex.getFieldIds().items().size();
    int constantPoolSize = cf.getConstantPool().size();
    int maxMethodIdsInDex = numMethodIds + constantPoolSize + cf.getMethods().size() + MAX_METHOD_ADDED_DURING_DEX_CREATION;
    int maxFieldIdsInDex = numFieldIds + constantPoolSize + cf.getFields().size() + MAX_FIELD_ADDED_DURING_DEX_CREATION;
    if (args.multiDex && // Never switch to the next dex if current dex is already empty
    (outputDex.getClassDefs().items().size() > 0) && ((maxMethodIdsInDex > args.maxNumberOfIdxPerDex) || (maxFieldIdsInDex > args.maxNumberOfIdxPerDex))) {
        DexFile completeDex = outputDex;
        createDexFile();
        assert (completeDex.getMethodIds().items().size() <= numMethodIds + MAX_METHOD_ADDED_DURING_DEX_CREATION) && (completeDex.getFieldIds().items().size() <= numFieldIds + MAX_FIELD_ADDED_DURING_DEX_CREATION);
    }
    try {
        ClassDefItem clazz = CfTranslator.translate(cf, bytes, args.cfOptions, args.dexOptions, args.optimizerOptions, outputDex);
        synchronized (outputDex) {
            outputDex.add(clazz);
        }
        return true;
    } catch (ParseException ex) {
        dxConsole.err.println("\ntrouble processing:");
        if (args.debug) {
            ex.printStackTrace(dxConsole.err);
        } else {
            ex.printContext(dxConsole.err);
        }
    }
    errors.incrementAndGet();
    return false;
}
Also used : DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) ClassDefItem(com.taobao.android.dx.dex.file.ClassDefItem) ParseException(com.taobao.android.dx.cf.iface.ParseException) DexFile(com.taobao.android.dx.dex.file.DexFile)

Aggregations

DirectClassFile (com.taobao.android.dx.cf.direct.DirectClassFile)3 DexFile (com.taobao.android.dx.dex.file.DexFile)3 ParseException (com.taobao.android.dx.cf.iface.ParseException)2 JarOutputStream (java.util.jar.JarOutputStream)2 DexException (com.taobao.android.dex.DexException)1 SimException (com.taobao.android.dx.cf.code.SimException)1 UsageException (com.taobao.android.dx.command.UsageException)1 ClassDefItem (com.taobao.android.dx.dex.file.ClassDefItem)1 CstString (com.taobao.android.dx.rop.cst.CstString)1