BasicDexFileReader的解析方法

时间:2014-02-11 09:02:40

标签: bytearray dalvik dex

我想知道我是否可以使用BasicDexFileReader的parse方法加载一个被解密为字节数组的dexfile?

public void parse(byte[] dexBytes) throws IllegalArgumentException, IOException/*,
    RefNotFoundException */ {
            // Get a DalvikValueReader on the input stream.
            reader = new DalvikValueReader(dexBytes, FILE_SIZE_OFFSET);
            readHeader();
            readStrings();
            readTypes();
    }

如果有人可以解释解析方法的目的究竟是什么,我会很高兴,并且可以按我要求的方式使用。

由于

1 个答案:

答案 0 :(得分:1)

看一下DexMaker.java,它需要解决这个问题才能生成代码而不是解密它。

以下是相关样本:

    byte[] dex = ...;

    /*
     * This implementation currently dumps the dex to the filesystem. It
     * jars the emitted .dex for the benefit of Gingerbread and earlier
     * devices, which can't load .dex files directly.
     *
     * TODO: load the dex from memory where supported.
     */
    File result = File.createTempFile("Generated", ".jar", dexCache);
    result.deleteOnExit();
    JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(result));
    jarOut.putNextEntry(new JarEntry(DexFormat.DEX_IN_JAR_NAME));
    jarOut.write(dex);
    jarOut.closeEntry();
    jarOut.close();
    try {
        return (ClassLoader) Class.forName("dalvik.system.DexClassLoader")
                .getConstructor(String.class, String.class, String.class, ClassLoader.class)
                .newInstance(result.getPath(), dexCache.getAbsolutePath(), null, parent);
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException("load() requires a Dalvik VM", e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e.getCause());
    } catch (InstantiationException e) {
        throw new AssertionError();
    } catch (NoSuchMethodException e) {
        throw new AssertionError();
    } catch (IllegalAccessException e) {
        throw new AssertionError();
    }