问题
I'm interested in doing some tinkering on compiled Class files before they're converted to dex files by dx. I've looked a bit at the official Dalvik documentation and also at comparisons between the DEX format and Class format. I can't find much information regarding the actual conversion process, class->dex. Does dx first verify the Class files before the conversion? Does it simply go field by field and method by method, merging groups of instructions into more compact groupings? Any insight would be appreciated.
Thanks.
回答1:
I'm not as familiar with dx itself and the conversion process as with dalvik bytecode, but I don't recall seeing any verification of the original java bytecode, although obviously it has to be well-formed enough to be parsed/understood by dx.
There is no documentation on the conversion process that I am aware of. It involves converting the bytecode into a couple of intermediate formats (ROP, SSA), and includes some logic for efficient register allocation and some optimizations on the intermediate forms (I think).
For more information on the conversion process, your best bet is to look at the dx source itself (/dalvik/dx)
回答2:
The way that dx
is run, it doesn't typically have sufficient information to do all possible verification, nor is it written to do so. In particular, part of verification has to do with how the code in one class refers to code in other classes, and when dx
is run, the code for the "other classes" in question might not actually be available. For example, you could compile some code against Android API level 6, producing a .dex
file. Later, when a device running API level 29 comes out, you could try to run that .dex
file. It's only when the file is on a system and getting ready to run that the system has all the info needed to perform verification. At that point, it can inspect the references in the .dex
file with what's available on the system and either accept (pass verification of) or reject (fail verification of) that file.
As a brief example, maybe the .dex
file refers to a class or method that existed in API level 6 but was removed as of API level 29.
But to be clear, as @JesusFreke said, dx
needs to be able to parse .class
files enough to be able to do its job of translation. If it runs into a problem at that layer, it will report that as a failure to translate, which, in context, is about equivalent to a verification error, though it's not generally phrased as such.
Even disregarding the possibility of evolution of the API, it is possible to take a .class
that wouldn't verify, succeed in translating it into a (part of a) .dex
file, and then observe that the .dex
file fails to verify.
I hope this helps!
来源:https://stackoverflow.com/questions/10315354/does-dx-conversion-to-dex-include-verification-of-original-class-files