When a Windows program executes, the CPU loads the program's instructions and data into memory execution. Similarly, when the Android program executes, the program's instructions and data are loaded into the runtime environment of the virtual machine.
android apk program is essentially a compressed package, It contains the Classes.dex executable file, androidmanifest.xml configuration files and resource files, etc., will be any hello.apk modified to Hello.zip decompression can be seen. This is similar to modifying the. doc Word document to. zip decompression under Windows.
Dalvik virtual machine class loading mechanism, its input is the Dex file in the APK, the output is a runtime environment in the classobject structure, Class loading is the association of resource data for classes in Dex with pointers to individual member variables under the classobject structure.
Here's a look at the workflow of the class loading mechanism.
The purpose of the verification is to verify the security and legality of the class data in the Dex file, and to guarantee the safe and stable operation of the virtual machine. The purpose of optimization is to optimize the Dex file and output the optimized Odex file according to the specific platform features, and improve the running efficiency of the program.
The purpose is to create a dedicated data structure in memory to describe the Odex file, so that the virtual machine's class data for each part of the Odex file is reachable and ready for subsequent loading of a class. Note that this time just completed parsing the Odex file and saved it to memory, but at this point the Odex file has not been loaded into the Dalvik virtual runtime environment.
Extracts the binary Dalvik bytecode from the parsed Odex file and encapsulates it into a runtime data structure for the interpreter to execute, depending on the Dalvik virtual machine execution needs. The run-time data structure is a classobject struct object, also known as a class object.
Notably, Dex's checksum optimizations are designed as standalone function modules in Android Dalvik virtual machines, and the Android system creates a new virtual machine dedicated to Dex file checksum optimization and frees up virtual machine resources at the end of the feature. This design is very much in line with Unix's design philosophy of doing only one thing and doing it well.
As can be seen from the Odex file structure, it is 3 parts more than the Dex file, the most important work is reflected in the library information and class index information two parts, through the two parts of the data, Dalvik virtual machine can find more quickly rely on the library and class information in Dex, improve program execution efficiency.
When the virtual machine finishes loading the class, the interpreter checks the loaded code to verify compliance with the virtual machine's execution specification, and finally calls the Dvminterpret function to initialize the interpreter and begin executing the bytecode. The Dvminterpret function has a key code.
/* Initialize interpreter working Environment */
self->interpsave.method = method;
self-> Interpsave.curframe = (u4*) self->interpsave.curframe;
This execution process self first obtains the method pointer that will be executed, and then assigns the first address of the Method->insns executable code area to the self-> of the PC pointer executing the process in the 3rd line of code. INTERPSAVE.PC, the interpreter's PC pointer is then read into the instruction and executes the relevant byte code.
Dalvik class loading mechanism