The difference between JVM and Dalvik
Android applications run on the Dalvik/ART VIRTUAL machine, and each application pair should have a separate Dalvik VIRTUAL machine instance. The Dalvik virtual machine is actually a Java virtual machine, but instead of executing a class file, it executes a dex file.
The Dalvik VIRTUAL machine shares similar features with the Java Virtual machine, but the difference is that the instruction set executed by the Dalvik virtual machine is different. The instruction set of the Dalvik virtual machine is basic register, while the instruction set of the Java Virtual machine is stack-based.
2. Stack-based Virtual Machines ()
For stack-based virtual machines, there is a separate stack for each thread at run time. The stack records the history of method calls, and each time a method is called, a stack frame is added to the stack. The top frame is called the current frame and represents the currently executing method. Stack-based virtual machines perform all operations through the operand stack.
Register-based virtual machines
Register-based virtual machines have no operand stack, but many virtual registers. Just like the operand stack, these registers are stored in the runtime stack, essentially an array. Similar to the JVM, each thread in the Dalvik VM has its own PC and call stack, and the activity record of method calls is stored on the call stack in frames.
Compared with the JVM version, it can be found that the number of instructions in Dalvik version is significantly reduced, and the number of data movement is also significantly reduced.
Iv. Dalvik and ART
Dalvik
The Dalvik virtual machine executes dex bytecode and interprets execution. Starting with Android version 2.2, JIT compilation (Just In Time) is supported to select hot code (frequently executed code) for compilation or optimization while an application is running.
ART
ART (Android Runtime) is a developer option introduced in Android 4.4 and the default Android Runtime for Android 5.0 and later. The ART virtual machine executes local machine code. The replacement of the Android runtime from the Dalvik VIRTUAL machine to the ART virtual machine does not require developers to compile their applications directly to the target machine code, and APK is still a file containing dex bytecode.
dex2aot
In the process of Dalvik application installation, optimization will be performed to optimize the DEX bytecode and generate odex file. The best AOT time to translate the application’s DEX bytecode into local machine code under Art is when the application is installed. ART introduces a compilation mechanism (Ahead Of Time). At installation Time, ART uses the dex2OAT tool that comes with the device to compile the application, and the bytecode in dex will be compiled into the local machine code.
Android7.0 uses mixed editing
This is because art needs to compile the bytecode in dex into machine code when installing the application. This will slow down the application installation. So there’s remixing.
1. Install the application initially without any AOT compilation (installation is fast again), interpret execution during run, JIT frequently executed methods, jIT-compiled methods will be recorded in the Profile.
2. When the device is idle and charging, the compilation daemon runs, AOT compiling common code from the Profile file. Use it directly for the next run.
5. Android classloader
- BootClassLoader: Loads classes in the Android Framework.
- PathClassLoader: the program’s classLoader parent->BootClassLoader
Class loading code
protected Class<? > loadClass(String name, boolean resolve) throws ClassNotFoundException { // First, Check if the class has already been loaded. > c = findLoadedClass(name); // then double delegate model to find. if (c == null) { try { if (parent ! = null) { c = parent.loadClass(name, false); } else { c = findBootstrapClassOrNull(name); } } catch (ClassNotFoundException e) { // ClassNotFoundException thrown if class not found // from the non-null parent class loader } if (c == null) { // If still not found, then invoke findClass in order // to find the class. c = findClass(name); } } return c; }Copy the code
Parental delegation mechanism
role
- Avoid reloading classes. Once the parent class is loaded, the subclass doesn’t have to be loaded.
- Security: Prevents core code from being tampered with
Class loading process
-
The pathClassLoader calls the LoadClass method, implemented in the parent BaseClassLoader class.
-
BaseClassLoader calls the findClass method. FindClass () of the DexPathList call
-
DexPathList has an array of dexElements. Each Element has a DexFile.
-
LoadClassBinaryName is used to find the corresponding class.
Six, thermal repair
- Gets the application’s pathClassLoader
- Get the PathList variable (DexPathlist) in the pathClassLoader by reflection.
- Convert your patch pack into an array of Elements, which is generated by calling DexPathList’s makePathElements method.
- Merge the two Elements arrays together. Set back by reflection.
Method of generating dex file.
-
Set environment variables. Dx. Bata.
-
Compile the project and go to the Classes folder.
-
Executes instructions: dx, dex, output = path. Dex com/imi/myapplication/activity/ShowActivity class
-
On the application of XML configuration is required above Android 10.0 Android: requestLegacyExternalStorage = “true”
Location of environment variables: