双亲委托
某个类加载时,首先委托给parent加载,依次递归,如果parent可以拿到对象则返回,否则由子类获取
优点
- 避免重复加载
- 安全考虑,防止核心API被修改
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
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;
}
热修复
类加载是有先后顺序,
首先获取到当前应用PathClassLoader,通过反射获取到DexPathList的pathList;修改pathList中的dexElements,
- 把补丁包patch.dex转化为Element[] patch
- 获取到dexElement对象属性
-
patch+Dexelement合并,并反射赋值给dexElements
AndFix
反射 类加载