文章目录
类加载机制
类加载器
在类加载阶段,在虚拟机外部。通过一个类的全限定名来捕获描述此类的二进制流的动作 的代码模块。
双亲委派机制:
//ClassLoader.java
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
// First, check if the class has already been loaded
//1.步骤1
Class<?> c = findLoadedClass(name);
if (c == null) {
try {
if (parent != null) {
//2.步骤2
c = parent.loadClass(name, false);
} else {
//3.返回null
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.
//3.步骤3
c = findClass(name);
}
}
return c;
}
类加载器加载步骤
- findLoadedClass(): 从类加载器的缓存中寻找是否有加载过得类(类加载器第一次加载完,会缓存起来)
- parent.loadClass(): 如果缓存中没有 该类,且有父类加载器的话,调用父类的 loadClass() 方法
- findClass: 如果没有父类的话,调用自己的findClass 方法
Android 中的类加载器
PathClassLoader 和 DexClassLoader 对比
//PathClassLoader
public class PathClassLoader extends BaseDexClassLoader {
public PathClassLoader(String dexPath, ClassLoader parent) {
super(dexPath, null, null, parent);
}
public PathClassLoader(String dexPath, String librarySearchPath, ClassLoader parent) {
super(dexPath, null, librarySearchPath, parent);
}
}
// DexClassLoader
public class DexClassLoader extends BaseDexClassLoader {
/**
*@param optimizedDirectory this parameter is deprecated and has no effect since API level 26.
*/
public DexClassLoader(String dexPath, String optimizedDirectory,
String librarySearchPath, ClassLoader parent) {
super(dexPath, null, librarySearchPath, p arent);
}
}
- PathClassLoader 是Android 项目默认的类加载器
- DexClassLoader 一般是加载 项目以外的dex 文件(没有编译到项目中的)。因为为了考虑到项目安全,必须为项目以外的二进制文件设置一个缓存目录 optimizedDirectory,不过api 26 以后,已经允许不设置该值。
Android 中的类加载分析
public abstract class ClassLoader {
//获取默认类加载器
static private class SystemClassLoader {
public static ClassLoader loader = ClassLoader.createSystemClassLoader(); }
private static ClassLoader createSystemClassLoader() {
String classPath = System.getProperty("java.class.path", ".");
String librarySearchPath = System.getProperty("java.library.path", "");
return new PathClassLoader(classPath, librarySearchPath, BootClassLoader.getInstance());
}
}
默认使用时,由上可知 PathClassLoader 的 父类加载器 BootClassLoader(引导类加载器)。
BootClassLoader 引导类加载器:手机启动后,加载操作系统中的预加载类。
PatchClassLoader :
由于pathClassLoader 的父加载器不加载应用的dex 类,最终会导致 pathClassLoader 自己去加载应用的类。
加载时序图
热修复切入点
-
自定义类加载器加载应用外部的 dex 文件。
-
将自定义类加载器中的 dexElements , 加到 应用默认加载器 的dexElements 前面。这样可以保证应用加载类的时候,可以先加载到 外部的 dex 二进制文件,而不会加载应用中的dex 二进制文件。
-
加载好的类,会放入类加载器的缓存中。
-
下次应用加载相同全限定名的类时,直接从缓存中取。
解决方案
反射+hook 修改应用默认类加载器的dexElements 。