[Android] Android class loading mechanism

Introduction to ClassLoader

Any Java program is composed of one or more class files. When the program is running, the class files need to be loaded into the JVM before they can be used. The Java class loading mechanism is responsible for loading these class files. The role of ClassLoader is simply to load class files and provide them for use when the program is running. There is a classLoader field inside each Class object to identify which ClassLoader it is loaded by.

Android's class loading mechanism is based on Java's class loading mechanism, but there are some special implementations on the Android platform.

key components

BootClassLoader:

This is the root class loader in the Android system, responsible for loading the core library of the Android system. It is a class loader implemented by native code, and its loading path is configured in advance at the operating system level. (for loading Android Framework layer class files)

PathClassLoader:

This is the application class loader, responsible for loading the application's code and resources. Each application has its own independent instance of PathClassLoader, which is used to load the classes in the application APK. (The specified dex can be loaded, as well as classes.dex in jar, zip, and apk)

DexClassLoader:

This is a special class loader for loading dex files that are not installed under the application directory. DexClassLoader can specify the path of the dex file and load the classes in it. (for loading the specified dex, and classes.dex in jar, zip, apk)

insert image description here
The common parent class of PathClassLoader and DexClassLoader is BaseDexClassLoader.

insert image description here
It can be seen that the only difference between the two is: to create DexClassLoader, you need to pass an optimizedDirectory parameter, and it will be created as a File object and passed to super, while PathClassLoader will be directly given to null. So both can load specified dex, and classes.dex in jar, zip, apk

PathClassLoader pathClassLoader = new PathClassLoader("/sdcard/xx.dex",getClassLoader());

File dexOutputDir = context.getCodeCacheDir();

DexClassLoader dexClassLoader = new DexClassLoader("/sdcard/xx.dex",dexOutputDir.getAbsolutePath(), null,getClassLoader());

In fact, the optimizedDirectory parameter is the output directory (odex) of dexopt. When PathClassLoader is created, this directory is null, which means no dexopt? No, the default path when optimizedDirectory is null is: /data/dalvik-cache.

insert image description here

Parental Delegation Mechanism

Java

The Parent Delegation Model is an implementation in the Java class loading mechanism. It is a way 层次化的类加载器结构to manage and organize the loading of classes through a hierarchical relationship.

Under parental delegation, there is one for each classloader 父类加载器(except the top-level startup classloader).当一个类加载器需要加载某个类时,它首先将加载请求委托给父类加载器。只有当父类加载器无法加载该类时,子类加载器才会尝试加载。

The specific execution process is as follows:

1. When a class loader receives a loading request, it first checks whether the class is already loaded. If already loaded, the loaded class is returned directly.

2. If the class has not been loaded, the loading request is delegated to its parent class loader.

3. The parent class loader continues to load according to the same rules: first check whether the class has been loaded, and then delegate to its parent class loader.

This delegation process continues until the topmost startup class loader is reached. If the startup class loader still cannot load the class, the subclass loader starts trying to load it.

The subclass loader attempts to load this class, and returns if successful, otherwise throws a ClassNotFoundException.

This level of parental delegation ensures class consistency and safety. Its core idea is 优先使用高层次的父类加载器去加载类that this can avoid repeated loading and class conflicts.如果一个类已经被父类加载器加载了,那么子类加载器就不需要再加载,直接使用父类加载器加载的类即可。

Class loaders in Java are usually delegated in the following order:
Bootstrap ClassLoader ->
Extension ClassLoader ->
Application ClassLoader.

Developers can also customize the class loader and override the loading behavior of the parent class loader as needed.

In general, the parent delegation mechanism ensures the loading order and consistency of classes through the hierarchical class loader structure, and prevents repeated loading and conflicts of classes. It is an important implementation of the Java class loading mechanism, which ensures the stability and security of Java programs.

Android

In Android, the parental delegation mechanism adopts a parental delegation model similar to that of Java, but the implementation of the top-level class loader is different. The Android class loader structure is as follows: (again)

BootClassLoader(引导类加载器):
This is the root class loader in the Android system and is responsible for loading core libraries (such as the core Java class library and the Android runtime library). It is a class loader implemented by native code, and it is solidified in the Android system, and application developers are not allowed to use it directly.

PathClassLoader(路径类加载器):
It is the primary implementation of the Android application classloader. Each application has its own independent instance of PathClassLoader, which is used to load classes and resources in the application APK. The loader searches the application's APK file path and loads the classes there.

DexClassLoader(DEX 类加载器):
This is a special class loader that can be used to load DEX files that are not installed under the application directory. By specifying the path of the DEX file, the classes in it can be loaded.

Android's class loader will delegate in the following order when loading classes:

1.PathClassLoader first tries to load the class. If it cannot find or load the requested class, it delegates the load request to its parent class loader, BootClassLoader.
2. BootClassLoader will continue to try to load classes. If it still cannot find or load the requested class, it throws a ClassNotFoundException.

In Android applications, PathClassLoader 是最常用的类加载器. It is responsible for loading the application's code and resources, including Java classes in APK files, standalone libraries (such as .so files), and other assets. PathClassLoader searches and loads classes along the class path, and if the class cannot be found, it delegates to the parent class loader for loading.

Through the parent delegation mechanism, Android's class loader ensures that 类的唯一性和一致性. If a class has already been loaded by the parent class loader, the child class loader will not load the class again, and directly use the loaded class instance. This is fine 避免重复加载和类的冲突.

It should be noted that in order to adapt to the Android operating environment, the Android class loader has done some special processing. For example, when loading resources, you can use the getResources() method provided by the Context class to load resources and return a resource object loaded by PathClassLoader.

To sum up, Android's parental delegation mechanism ensures the loading order and consistency of classes through the hierarchical relationship of class loaders. PathClassLoader is the main application class loader responsible for loading classes and resources in APK files. BootClassLoader is the root class loader responsible for loading the core library. This mechanism ensures the stability and security of Android applications.

Guess you like

Origin blog.csdn.net/qq_43358469/article/details/131480424