Search in sources :

Example 1 with DexException

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

the class DecodedInstruction method decodeAll.

/**
     * Decodes an array of instructions. The result has non-null
     * elements at each offset that represents the start of an
     * instruction.
     */
public static DecodedInstruction[] decodeAll(short[] encodedInstructions) {
    int size = encodedInstructions.length;
    DecodedInstruction[] decoded = new DecodedInstruction[size];
    ShortArrayCodeInput in = new ShortArrayCodeInput(encodedInstructions);
    try {
        while (in.hasMore()) {
            decoded[in.cursor()] = DecodedInstruction.decode(in);
        }
    } catch (EOFException ex) {
        throw new DexException(ex);
    }
    return decoded;
}
Also used : DexException(com.android.dex.DexException) EOFException(java.io.EOFException)

Example 2 with DexException

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

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.android.dex.DexException) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CstString(com.android.dx.rop.cst.CstString) DirectClassFile(com.android.dx.cf.direct.DirectClassFile) DexFile(com.android.dx.dex.file.DexFile) File(java.io.File) SimException(com.android.dx.cf.code.SimException) UsageException(com.android.dx.command.UsageException) ParseException(com.android.dx.cf.iface.ParseException) DexException(com.android.dex.DexException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with DexException

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

the class DexMerger method readSortableTypes.

/**
     * Reads just enough data on each class so that we can sort it and then find
     * it later.
     */
private void readSortableTypes(SortableType[] sortableTypes, Dex buffer, IndexMap indexMap) {
    for (ClassDef classDef : buffer.classDefs()) {
        SortableType sortableType = indexMap.adjust(new SortableType(buffer, indexMap, classDef));
        int t = sortableType.getTypeIndex();
        if (sortableTypes[t] == null) {
            sortableTypes[t] = sortableType;
        } else if (collisionPolicy != CollisionPolicy.KEEP_FIRST) {
            throw new DexException("Multiple dex files define " + buffer.typeNames().get(classDef.getTypeIndex()));
        }
    }
}
Also used : DexException(com.android.dex.DexException) ClassDef(com.android.dex.ClassDef)

Example 4 with DexException

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

the class Main method processAllFiles.

/**
     * Constructs the output {@link DexFile}, fill it in with all the
     * specified classes, and populate the resources map if required.
     *
     * @return whether processing was successful
     */
private boolean processAllFiles() {
    createDexFile();
    if (args.jarOutput) {
        outputResources = new TreeMap<String, byte[]>();
    }
    anyFilesProcessed = false;
    String[] fileNames = args.fileNames;
    Arrays.sort(fileNames);
    // translate classes in parallel
    classTranslatorPool = new ThreadPoolExecutor(args.numThreads, args.numThreads, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2 * args.numThreads, true), new ThreadPoolExecutor.CallerRunsPolicy());
    // collect translated and write to dex in order
    classDefItemConsumer = Executors.newSingleThreadExecutor();
    try {
        if (args.mainDexListFile != null) {
            // with --main-dex-list
            FileNameFilter mainPassFilter = args.strictNameCheck ? new MainDexListFilter() : new BestEffortMainDexListFilter();
            // forced in main dex
            for (int i = 0; i < fileNames.length; i++) {
                processOne(fileNames[i], mainPassFilter);
            }
            if (dexOutputFutures.size() > 0) {
                throw new DexException("Too many classes in " + Arguments.MAIN_DEX_LIST_OPTION + ", main dex capacity exceeded");
            }
            if (args.minimalMainDex) {
                // Wait for classes in progress to complete
                synchronized (dexRotationLock) {
                    while (maxMethodIdsInProcess > 0 || maxFieldIdsInProcess > 0) {
                        try {
                            dexRotationLock.wait();
                        } catch (InterruptedException ex) {
                        /* ignore */
                        }
                    }
                }
                rotateDexFile();
            }
            // remaining files
            for (int i = 0; i < fileNames.length; i++) {
                processOne(fileNames[i], new NotFilter(mainPassFilter));
            }
        } else {
            // without --main-dex-list
            for (int i = 0; i < fileNames.length; i++) {
                processOne(fileNames[i], ClassPathOpener.acceptAll);
            }
        }
    } catch (StopProcessing ex) {
    /*
             * Ignore it and just let the error reporting do
             * their things.
             */
    }
    try {
        classTranslatorPool.shutdown();
        classTranslatorPool.awaitTermination(600L, TimeUnit.SECONDS);
        classDefItemConsumer.shutdown();
        classDefItemConsumer.awaitTermination(600L, TimeUnit.SECONDS);
        for (Future<Boolean> f : addToDexFutures) {
            try {
                f.get();
            } catch (ExecutionException ex) {
                // Catch any previously uncaught exceptions from
                // class translation and adding to dex.
                int count = errors.incrementAndGet();
                if (count < 10) {
                    if (args.debug) {
                        context.err.println("Uncaught translation error:");
                        ex.getCause().printStackTrace(context.err);
                    } else {
                        context.err.println("Uncaught translation error: " + ex.getCause());
                    }
                } else {
                    throw new InterruptedException("Too many errors");
                }
            }
        }
    } catch (InterruptedException ie) {
        classTranslatorPool.shutdownNow();
        classDefItemConsumer.shutdownNow();
        throw new RuntimeException("Translation has been interrupted", ie);
    } catch (Exception e) {
        classTranslatorPool.shutdownNow();
        classDefItemConsumer.shutdownNow();
        e.printStackTrace(out);
        throw new RuntimeException("Unexpected exception in translator thread.", e);
    }
    int errorNum = errors.get();
    if (errorNum != 0) {
        context.err.println(errorNum + " error" + ((errorNum == 1) ? "" : "s") + "; aborting");
        return false;
    }
    if (args.incremental && !anyFilesProcessed) {
        return true;
    }
    if (!(anyFilesProcessed || args.emptyOk)) {
        context.err.println("no classfiles specified");
        return false;
    }
    if (args.optimize && args.statistics) {
        context.codeStatistics.dumpStatistics(context.out);
    }
    return true;
}
Also used : DexException(com.android.dex.DexException) CstString(com.android.dx.rop.cst.CstString) SimException(com.android.dx.cf.code.SimException) UsageException(com.android.dx.command.UsageException) ParseException(com.android.dx.cf.iface.ParseException) DexException(com.android.dex.DexException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ExecutionException(java.util.concurrent.ExecutionException) FileNameFilter(com.android.dx.cf.direct.ClassPathOpener.FileNameFilter)

Example 5 with DexException

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

the class MergeConflictTest method testMergeConflict.

public void testMergeConflict() throws IOException {
    Dex a = resourceToDexBuffer("/testdata/A.dex");
    Dex b = resourceToDexBuffer("/testdata/B.dex");
    // a and b don't overlap; this should succeed
    Dex ab = new DexMerger(a, b, CollisionPolicy.FAIL).merge();
    // a and ab overlap; this should fail
    DexMerger dexMerger = new DexMerger(a, ab, CollisionPolicy.FAIL);
    try {
        dexMerger.merge();
        fail();
    } catch (DexException expected) {
        assertEquals("Multiple dex files define Ltestdata/A;", expected.getMessage());
    }
}
Also used : DexException(com.android.dex.DexException) Dex(com.android.dex.Dex)

Aggregations

DexException (com.android.dex.DexException)5 SimException (com.android.dx.cf.code.SimException)2 ParseException (com.android.dx.cf.iface.ParseException)2 UsageException (com.android.dx.command.UsageException)2 CstString (com.android.dx.rop.cst.CstString)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 ClassDef (com.android.dex.ClassDef)1 Dex (com.android.dex.Dex)1 FileNameFilter (com.android.dx.cf.direct.ClassPathOpener.FileNameFilter)1 DirectClassFile (com.android.dx.cf.direct.DirectClassFile)1 DexFile (com.android.dx.dex.file.DexFile)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 EOFException (java.io.EOFException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 JarOutputStream (java.util.jar.JarOutputStream)1