javaandroiddexdex2jar

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


I'm trying to use dex2jar to read a dex file (an Android APK) in a plain Java app.

I have a file object to the APK and then simply call new DexFileReader(dex)

However this throws the following exception "not support magic":

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)

The source code shows the following block:

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.");
}

My assumption is I've missed a step and that there needs to be a conversion from APK to a base dex format, but it's not shown in the example 'Want to read dex file using dex2jar' section of the wiki (linked above).

How do I successfully read an APK? What have I missed?


Solution

  • The apk is basically a zip-file. It contains the dex - but is much more than it. So please extract the dex from there first and then run dex2jar.