使用阿里热修复需要添加依赖
compile 'com.alipay.euler:andfix:0.5.0@aar'
热修复的关键代码
//初始化阿里热修复
mPatchManger = new PatchManager(this);
//获取当前应用版本
mPatchManger.init(AppUtils.getVersionName(this));
mPatchManger.loadPatch();
//以下代码可以写在项目的application中
//获取下载到的patch包
File patchFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"fix.apatch");
if (patchFile != null){
try {
mPatchManger.addPatch(patchFile.getAbsolutePath());
Toast.makeText(this,"修复成功",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this,"修复失败",Toast.LENGTH_SHORT).show();
}
}
接下来我们从这些方法开始看热修复是怎样实现的
mPatchManger = new PatchManager(this);
public PatchManager(Context context) {
mContext = context;
mAndFixManager = new AndFixManager(mContext);
//差分包存储的路径,下载之后拷贝到这里
mPatchDir = new File(mContext.getFilesDir(), DIR);
//线程安全的set和map
mPatchs = new ConcurrentSkipListSet<Patch>();
mLoaders = new ConcurrentHashMap<String, ClassLoader>();
}
mPatchManger.init(AppUtils.getVersionName(this));
public void init(String appVersion) {
if (!mPatchDir.exists() && !mPatchDir.mkdirs()) {// make directory fail
Log.e(TAG, "patch dir create error.");
return;
} else if (!mPatchDir.isDirectory()) {// not directory
mPatchDir.delete();
return;
}
SharedPreferences sp = mContext.getSharedPreferences(SP_NAME,
Context.MODE_PRIVATE);
String ver = sp.getString(SP_VERSION, null);
//版本校验
if (ver == null || !ver.equalsIgnoreCase(appVersion)) {
cleanPatch();
sp.edit().putString(SP_VERSION, appVersion).commit();
} else {
initPatchs();
}
}
private void initPatchs() {
File[] files = mPatchDir.listFiles();
//遍历这个文件中存储的所有的差分包,这些差分包是应用启动后下载到文件夹中去的
for (File file : files) {
addPatch(file);
}
}
addPatch
验证文件名,对符合要求的文件名进行封装,得到一个个Patch对象,在集合中保存
/**
* add patch file
*
* @param file
* @return patch
*/
private Patch addPatch(File file) {
Patch patch = null;
if (file.getName().endsWith(SUFFIX)) {
try {
patch = new Patch(file);
mPatchs.add(patch);
} catch (IOException e) {
Log.e(TAG, "addPatch", e);
}
}
return patch;
}
可见mPatchManger.init方法只是加载差分包然后进行存储
接下来看mPatchManger.loadPatch();
public void loadPatch() {
//保存类加载器
mLoaders.put("*", mContext.getClassLoader());// wildcard
Set<String> patchNames;
List<String> classes;
//遍历patch集合,得到每一个相关文件信息
for (Patch patch : mPatchs) {
patchNames = patch.getPatchNames();
for (String patchName : patchNames) {
classes = patch.getClasses(patchName);
mAndFixManager.fix(patch.getFile(), mContext.getClassLoader(),
classes);
}
}
}
进入mAndFixManager.fix(patch.getFile(),mContext.getClassLoader(),
classes);
//List<String> classes存储了每个patch文件的相关信息
public synchronized void fix(File file, ClassLoader classLoader,
List<String> classes) {
。。。。
//加载DexFile对象
final DexFile dexFile = DexFile.loadDex(file.getAbsolutePath(),
optfile.getAbsolutePath(), Context.MODE_PRIVATE);
......
ClassLoader patchClassLoader = new ClassLoader(classLoader) {
@Override
protected Class<?> findClass(String className)
throws ClassNotFoundException {
Class<?> clazz = dexFile.loadClass(className, this);
if (clazz == null
&& className.startsWith("com.alipay.euler.andfix")) {
return Class.forName(className);// annotation’s class
// not found
}
if (clazz == null) {
throw new ClassNotFoundException(className);
}
return clazz;
}
};
Enumeration<String> entrys = dexFile.entries();
Class<?> clazz = null;
while (entrys.hasMoreElements()) {
String entry = entrys.nextElement();
if (classes != null && !classes.contains(entry)) {
continue;// skip, not need fix
}
//通过classLoader加载得到差分包中的修复了的class
clazz = dexFile.loadClass(entry, patchClassLoader);
if (clazz != null) {
//得到了class之后才真正的开始修复
fixClass(clazz, classLoader);
}
}
} catch (IOException e) {
Log.e(TAG, "pacth", e);
}
}
fixClass
看到下边也许觉得奇怪,为什么去获取了一个叫MethodReplace的注解呢,这个注解是在哪里使用的,有什么作用,
private void fixClass(Class<?> clazz, ClassLoader classLoader) {
Method[] methods = clazz.getDeclaredMethods();
MethodReplace methodReplace;
String clz;
String meth;
for (Method method : methods) {
methodReplace = method.getAnnotation(MethodReplace.class);
if (methodReplace == null)
continue;
clz = methodReplace.clazz();
meth = methodReplace.method();
if (!isEmpty(clz) && !isEmpty(meth)) {
replaceMethod(classLoader, clz, meth, method);
}
}
}
解压差分包我们可以看到andFix在发生错误的位置做了标记,就是这个注解
在错误的方法上添加注解,看上边遍历这个class文件中所有的方法,找到这个注解调用replaceMethod,故名思意,就是将错误的方法替换为正确的方法,有可能是存在多个class存在多个问题,其实andFix修复的原理就是解压生成的PATCH.MF文件,这个文件中保存了有错误的类的信息,把这些类加入集合,遍历集合通过上边的方式获取到注解,定位到错误的位置
private void replaceMethod(ClassLoader classLoader, String clz,
String meth, Method method) {
try {
String key = clz + "@" + classLoader.toString();
Class<?> clazz = mFixedClass.get(key);
if (clazz == null) {// class not load
Class<?> clzz = classLoader.loadClass(clz);
// initialize target class
clazz = AndFix.initTargetClass(clzz);
}
if (clazz != null) {// initialize class OK
mFixedClass.put(key, clazz);
Method src = clazz.getDeclaredMethod(meth,
method.getParameterTypes());
AndFix.addReplaceMethod(src, method);
}
} catch (Exception e) {
Log.e(TAG, "replaceMethod", e);
}
}
public static void addReplaceMethod(Method src, Method dest) {
try {
replaceMethod(src, dest);
initFields(dest.getDeclaringClass());
} catch (Throwable e) {
Log.e(TAG, "addReplaceMethod", e);
}
}
最后可以看到这里已经是c层面的做法了
private static native void replaceMethod(Method dest, Method src);
可以看到其实native层是通过指针修改方法的指向实现修复问题的,指向正确的修复之后的方法,就是这样,OK了