Table of contents
1. Problem discovery
When using the dex2jar tool to reverse compile, as soon as the command is input, the result reports com.googlecode.d2j.DexException: not support version error (as shown below)
Abnormal situation.png
2. Investigation reasons:
Google's introduction to dex files : Constant arrays/strings
DEX_FILE_MAGIC
are lists of bytes, and such bytes must appear at.dex
the beginning of the file in order for the system to recognize it as it is. The value intentionally contains a newline ("\n"
or0x0a
) and null byte ("\0"
or0x00
) to aid in the detection of certain forms of corruption. This value can also encode the format version number as 3 decimal digits; this value is expected to increase monotonically as the format evolves.Note : Support for the format was added in Android 9.0
039
, which introduces two new bytecodesconst-method-handle
andconst-method-type
. ( These bytecodes are described in the summary table of bytecode collections039
.) In Android 10, version extends the DEX file format to include hidden API information applicable only to DEX files on the startup classpath.Note
038
: Support for the format was added in the Android 8.0 release .038
New bytecode (invoke-polymorphic
andinvoke-custom
) and data for method handles were added in version.Note
037
: Support for the format was added in the Android 7.0 release . Most versions of Android prior to037
the version used035
the version format.035
The only difference between version and037
version is whether default methods are added and whether they are adjustedinvoke
.
Simply put: it is the system version corresponding to the minSdkVersion parameter configured in Android Studio, and the dex file header will be
For example: minSdkVersion is configured as 24 (corresponding to the 7.0 system), and the compiled dex header is version 037 (you can see it by opening the dex file with notepad++)
android {
......
defaultConfig {
......
minSdkVersion 24
......
}
Compiled dex file.png
3. Root cause investigation:
In fact, after reading the third survey , there is already a plan, which is to modify the version information in the dex file. But I don't know the root cause and I am not reconciled, so I continue to investigate.
According to the error message: com.googlecode.d2j.reader.DexFileReader.<init>(DexFileReader.java:151)
Guess it is reader.DexFileReader
an error reported in the construction method, open the dex2jar-2.0\lib directory, and throw dex-reader-2.0.jar
ordex-reader-api-2.0.jar
drop it into the jd-gui tool to see the source code as follows:
// 构造方法中找到报错点
public DexFileReader(ByteBuffer in){
......
int version = in.getInt() & 0xFFFFFF;
if ((version != 3486512) && (version != 3552048)) {
throw new DexException("not support version.");
}
skip(in, 32);
......
}
.....
}
// 支持的版本定义
private static final int MAGIC_035 = 3486512;
private static final int MAGIC_036 = 3552048;
It means that the dex2jar-2.0 tool only supports version 035 and 036 protocols! ! !
4. Solve the problem
Use the notepad++ file browsing tool (any tool that can open files, such as Notepad...) to open the dex file, modify the version information 037 to 036 or 035 , then save the file, and then use the reverse compilation command: d2j- dex2jar.bat classes.dex can be successfully reversed.
After modifying the dex version information.png
Get the jar package normally.png