使用 dex2jar 读取 APK 需要什么魔法?

What magic do I need to read an APK with dex2jar?

我正在尝试使用 dex2jar 在普通 Java 应用程序中读取 dex 文件(Android APK)。

我有一个 APK 的文件对象,然后只需调用 new DexFileReader(dex)

但是这会引发以下异常“不支持魔法”:

com.googlecode.d2j.DexException: not support magic.
at com.googlecode.d2j.reader.DexFileReader.<init>(DexFileReader.java:160)
at com.googlecode.d2j.reader.DexFileReader.<init>(DexFileReader.java:258)
at com.googlecode.d2j.reader.DexFileReader.<init>(DexFileReader.java:276)

源代码显示以下块:

in = in.asReadOnlyBuffer().order(ByteOrder.BIG_ENDIAN);
int magic = in.getInt() & 0xFFFFFF00;

final int MAGIC_DEX = 0x6465780A & 0xFFFFFF00;// hex for 'dex ', ignore the 0A
final int MAGIC_ODEX = 0x6465790A & 0xFFFFFF00;// hex for 'dey ', ignore the 0A

if (magic == MAGIC_DEX) {
    ...
} else if (magic == MAGIC_ODEX) {
    ...
} else {
    throw new DexException("not support magic.");
}

我的假设是我错过了一个步骤,需要从 APK 转换为基本 dex 格式,但它没有显示在 wiki 的示例 'Want to read dex file using dex2jar' 部分(上面链接) .

如何成功读取APK?我错过了什么?

apk 基本上是一个 zip 文件。它包含 dex - 但远不止于此。所以请先从那里提取 dex 然后 运行 dex2jar.