Java Code Examples for com.android.dx.dex.file.DexFile#toDex()

The following examples show how to use com.android.dx.dex.file.DexFile#toDex() . 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: AndroidRhinoContext.java    From Mindustry with GNU General Public License v3.0 6 votes vote down vote up
@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 File: BaseAndroidClassLoader.java    From rhino-android with 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 3
Source File: Main.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Converts {@link #outputDex} into a {@code byte[]} and do whatever
 * human-oriented dumping is required.
 *
 * @return {@code null-ok;} the converted {@code byte[]} or {@code null}
 * if there was a problem
 */
private byte[] writeDex(DexFile outputDex) {
    byte[] outArray = null;

    try {
        try {
            if (args.methodToDump != null) {
                /*
                 * Simply dump the requested method. Note: The call
                 * to toDex() is required just to get the underlying
                 * structures ready.
                 */
                outputDex.toDex(null, false);
                dumpMethod(outputDex, args.methodToDump, humanOutWriter);
            } else {
                /*
                 * This is the usual case: Create an output .dex file,
                 * and write it, dump it, etc.
                 */
                outArray = outputDex.toDex(humanOutWriter, args.verboseDump);
            }

            if (args.statistics) {
                context.out.println(outputDex.getStatistics().toHuman());
            }
        } finally {
            if (humanOutWriter != null) {
                humanOutWriter.flush();
            }
        }
    } catch (Exception ex) {
        if (args.debug) {
            context.err.println("\ntrouble writing output:");
            ex.printStackTrace(context.err);
        } else {
            context.err.println("\ntrouble writing output: " +
                               ex.getMessage());
        }
        return null;
    }
    return outArray;
}
 
Example 4
Source File: Main.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Converts {@link #outputDex} into a {@code byte[]} and do whatever
 * human-oriented dumping is required.
 *
 * @return {@code null-ok;} the converted {@code byte[]} or {@code null}
 * if there was a problem
 */
private byte[] writeDex(DexFile outputDex) {
    byte[] outArray = null;

    try {
        try {
            if (args.methodToDump != null) {
                /*
                 * Simply dump the requested method. Note: The call
                 * to toDex() is required just to get the underlying
                 * structures ready.
                 */
                outputDex.toDex(null, false);
                dumpMethod(outputDex, args.methodToDump, humanOutWriter);
            } else {
                /*
                 * This is the usual case: Create an output .dex file,
                 * and write it, dump it, etc.
                 */
                outArray = outputDex.toDex(humanOutWriter, args.verboseDump);
            }

            if (args.statistics) {
                context.out.println(outputDex.getStatistics().toHuman());
            }
        } finally {
            if (humanOutWriter != null) {
                humanOutWriter.flush();
            }
        }
    } catch (Exception ex) {
        if (args.debug) {
            context.err.println("\ntrouble writing output:");
            ex.printStackTrace(context.err);
        } else {
            context.err.println("\ntrouble writing output: " +
                               ex.getMessage());
        }
        return null;
    }
    return outArray;
}
 
Example 5
Source File: Main.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/**
 * Converts {@link #outputDex} into a {@code byte[]} and do whatever
 * human-oriented dumping is required.
 *
 * @return {@code null-ok;} the converted {@code byte[]} or {@code null}
 * if there was a problem
 */
private byte[] writeDex(DexFile outputDex) {
    byte[] outArray = null;

    try {
        try {
            if (args.methodToDump != null) {
                /*
                 * Simply dump the requested method. Note: The call
                 * to toDex() is required just to get the underlying
                 * structures ready.
                 */
                outputDex.toDex(null, false);
                dumpMethod(outputDex, args.methodToDump, humanOutWriter);
            } else {
                /*
                 * This is the usual case: Create an output .dex file,
                 * and write it, dump it, etc.
                 */
                outArray = outputDex.toDex(humanOutWriter, args.verboseDump);
            }

            if (args.statistics) {
                context.out.println(outputDex.getStatistics().toHuman());
            }
        } finally {
            if (humanOutWriter != null) {
                humanOutWriter.flush();
            }
        }
    } catch (Exception ex) {
        if (args.debug) {
            context.err.println("\ntrouble writing output:");
            ex.printStackTrace(context.err);
        } else {
            context.err.println("\ntrouble writing output: " +
                               ex.getMessage());
        }
        return null;
    }
    return outArray;
}
 
Example 6
Source File: DexFiles.java    From bazel with Apache License 2.0 4 votes vote down vote up
/**
 * Serializes the given {@link DexFile} into {@code .dex}'s file format.
 */
static byte[] encode(DexFile dex) throws IOException {
  return dex.toDex(null, false);
}
 
Example 7
Source File: Main.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Converts {@link #outputDex} into a {@code byte[]} and do whatever
 * human-oriented dumping is required.
 *
 * @return {@code null-ok;} the converted {@code byte[]} or {@code null}
 * if there was a problem
 */
private byte[] writeDex(DexFile outputDex) {
    byte[] outArray = null;

    try {
        try {
            if (args.methodToDump != null) {
                /*
                 * Simply dump the requested method. Note: The call
                 * to toDex() is required just to get the underlying
                 * structures ready.
                 */
                outputDex.toDex(null, false);
                dumpMethod(outputDex, args.methodToDump, humanOutWriter);
            } else {
                /*
                 * This is the usual case: Create an output .dex file,
                 * and write it, dump it, etc.
                 */
                outArray = outputDex.toDex(humanOutWriter, args.verboseDump);
            }

            if (args.statistics) {
                context.out.println(outputDex.getStatistics().toHuman());
            }
        } finally {
            if (humanOutWriter != null) {
                humanOutWriter.flush();
            }
        }
    } catch (Exception ex) {
        if (args.debug) {
            context.err.println("\ntrouble writing output:");
            ex.printStackTrace(context.err);
        } else {
            context.err.println("\ntrouble writing output: " +
                ex.getMessage());
        }
        return null;
    }
    return outArray;
}