记录学习路程
长路漫漫,上下求索
什么是热修复
热修复的优势
- 无需重新发布新版本,省时省力
- 用户无感知修复,也不需下载最新应用代价小
- 修复成功率高,把损失降到最低
热修复能完成哪些修复
- 代码修复
- 资源修复
- .so 库修复
修复技术
热修复分类:
热修复插桩原理
代码实现
大致的思路:
/**
* 热修复
* @param classLoader 自有的类加载器,加载了修复包的DexClassLoader
* @param context 上下文
*/
private static void hotfix(DexClassLoader classLoader, Context context) {
// 获取系统PathClassLoader类加载器
PathClassLoader pathLoader = (PathClassLoader) context.getClassLoader();
try {
// 获取自有的dexElements数组对象
Object myDexElements = ReflectUtils.getDexElements(ReflectUtils.getPathList(classLoader));
// 获取系统的dexElements数组对象
Object systemDexElements = ReflectUtils.getDexElements(ReflectUtils.getPathList(pathLoader));
// 合并成新的dexElements数组对象
Object dexElements = ArrayUtils.combineArray(myDexElements, systemDexElements);
// 通过反射再去获取 系统的pathList对象
Object systemPathList = ReflectUtils.getPathList(pathLoader);
// 重新赋值给系统的pathList属性 --- 修改了pathList中的dexElements数组对象
ReflectUtils.setField(systemPathList, systemPathList.getClass(), dexElements);
} catch (Exception e) {
e.printStackTrace();
}
}
https://github.com/aixiaozi/AndroidTec