Summary: Android Xiaobai's Growth Path_Knowledge System Summary【Continuously updated...】
Table of contents
What is ClassLoader
ClassLoader
It is the class loader. The virtual machine cannot directly load the class bytecode file. It needs to process the bytecode file through a series of processes to finally form an object ClassLoader
that the virtual machine can directly read.Class
Class loading process
The class loading process can be simply summarized as:
- Loading: Load bytecode files
ClassLoader
from various sources into memory through differentClass
- Verification: verify whether the loaded byte stream conforms to the specification of the virtual machine, and prevent non-standard byte streams generated by special processing from being loaded into memory
- File format verification: Verify that the byte stream conforms to
Class
the specification of the file format and can be processed by the current version of the virtual machine. - Metadata verification and bytecode verification: semantic analysis of bytecode and analysis of data flow to ensure correct code logic
- Symbol reference verification: whether the class described in the form of a string exists, and whether the definition of the class conforms to the specification
- File format verification: Verify that the byte stream conforms to
- Preparation: Allocate memory and assign initial values to class variables (modified
static
variables). The default initial value here is not the initial value written in our code, but the initial value assigned by the java virtual machine according to the type, for example, the initial value of int is 0 - Analysis: Replace the symbolic references in the constant pool with direct references, and the virtual machine replaces all symbolic references such as class names, method names, and field names with specific memory addresses or offsets
- Initialization: Initialize class variables, mainly perform initialization of static variables, including assignment of static variables and execution of static initialization blocks
Parental Delegation Mechanism
- For two classes, if and only if both classes come from the same Class file and are loaded by the same class loader, the virtual machine considers them to be the same class
- Parent delegation mechanism:
- Currently,
ClassLoader
first query whether this class has been loaded from its own loading cache, and return this class directly if it has been loaded - If it has not been loaded at present
ClassLoader
, entrust the parent class loader to load, and the parent class loader also adopts the same strategy, first check its own cache, if not, continue to entrust the parent class loader to load, until the highest level class loader - When all parent class loaders are not loaded, the current class loader will load it and put it in its own cache
- Currently,
- The parent class loader does not refer to the current
ClassLoader
parent class, but refers to the class corresponding to the current fieldClassLoader
.parent
- The role of the parent delegation mechanism:
- Prevent repeated loading of the same Class: the loaded Class will be returned directly
- Ensure that the Class of the core library is not tampered with: the Class of the core library is loaded by the high-level parent class loader. Even if we change a class of a core library and use the class loader to load it, it will be loaded by the parent class loader. If the parent class loader has already loaded this class, it will directly return the loaded one to avoid tampering with the core library
- The method of destroying the parental delegation mechanism: customize the ClassLoader and rewrite
loadClass
the method, and implement your own logic without following the parental delegation mechanism
ClassLoader in Java
BootstrapClassLoader
: The top-level loading class, which mainly loads the core class library, usuallyjre/lib
the class library in the JDK directoryExtensionClassLoader
: Its parent class loader is , it mainly loads the class library in the directoryBootstrapClassLoader
under the JDK directory , and then replaces it withjre/lib/ext
JAVA 9
PlatformClassLoader
AppClassLoader
: Application class loader, its parent class loader is responsible for loading all files and files under theExtensionClassLoader
application directoryCLASSPATH
jar
Class
ClassLoader in Android
- Since the Class file is no longer loaded in Android, but
Dex
the file, there is no ExtensionClassLoader and AppClassLoader in Android BootClassLoader
: Created when the system starts, passed in when the App starts, used to load someFramework
classes required by the system levelDexClassLoader
AndPathClassLoader
: APK, DEX and JAR can be loaded from the directory. The difference between the two is that PathClassLoader does not set itoptimizedDirectory
toNull
, that is, it does not set the optimized storage path. In fact, the default path when optimizedDirectory is null is/data/dalvik-cache
the directory. PathClassLoader is a class used to load Android system classes and applications. And DexClassLoader can specify optimizedDirectory, but after it arrivesAndroid 8.0之
, this field is deprecated, which means that DexClassLoader specifying optimizedDirectory will not work
Class loading application scenarios
- hot load
- encrypted protection