最近看了阿里的AndFix热修复框架,但是好像不能支持所有设备,所以,自己手动来实现一遍安卓的热修复,究其是如何实现的。
Java的虚拟机JVM运行代码时,加载的是.class字节码文件,而Android的Dalvik/ART虚拟机加载的是Dex文件,不过他们的工作机制是一样的,都经过ClassLoader这个类加载器,只不过,Android重新定义了两个类DexClassLoader和PathClassLoader去解析类,他们是继承BaseDexClassLoader类的,关于这两个类的介绍:
1)、PathClassLoader:官方文档解析 Android uses this class for its system class loader and for its application class loader(s),使用该类作为系统类和应用类的加载器,也即只能加载已经安装到Android系统的apk文件
2)、DexClassLoader:可加载jar、apk和dex文件,可以从存储外部加载。
1、首先写一个测试修改效果的类Bugs ,并在activity中使用,
public class Bugs {
public static String getBugs(){
return "有bug!";
}
}
public class TestHotfixActivity extends Activity {
TextView tv_result;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_result=(TextView)findViewById(R.id.tv_result);
}
public void onPost(View view){
tv_result.setText(Bugs.getBugs());
}
}
2、新建application和HotFixEngine类
public class BaseApplication extends Application {
public static final String FIX_DEX_PATH = "fix_dex";//fixDex存储的路径
@Override
public void onCreate() {
super.onCreate();
copyDexFileToAppAndFix(this,"patch.dex");
loadAllDex();
}
/**
* 用下载的dex文件,修复app
*/
public void loadAllDex() {
File dexFilePath = getDir(FIX_DEX_PATH, Context.MODE_PRIVATE);
for (File dexFile:dexFilePath.listFiles()){
Log.d("======>",dexFile.getAbsolutePath());
if (dexFile.getAbsolutePath().endsWith("dex") ) {
new HotFixEngine().loadDex(this, dexFile);
}
}
}
/**
* 复制SD卡中的补丁文件到dex目录,或者从服务器下载补丁文件代码
*/
public static void copyDexFileToAppAndFix(Context context, String dexFileName) {
File path = new File(Environment.getExternalStorageDirectory(), dexFileName);
if (!path.exists()) {
Toast.makeText(context, "没有找到补丁文件", Toast.LENGTH_SHORT).show();
return;
}
if (!path.getAbsolutePath().endsWith("dex")){
Toast.makeText(context, "补丁文件格式不正确", Toast.LENGTH_SHORT).show();
return;
}
File dexFilePath = context.getDir(FIX_DEX_PATH, Context.MODE_PRIVATE);
File dexFile = new File(dexFilePath, dexFileName);
if (dexFile.exists()) {
dexFile.delete();
}
//copy
InputStream is = null;
FileOutputStream os = null;
try {
is = new FileInputStream(path);
os = new FileOutputStream(dexFile);
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
path.delete();//删除sdcard中的补丁文件,或者你可以直接下载到app的路径中
is.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
Log.d("======>",e.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
Log.d("======>",e.getMessage());
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
Log.d("======>",e.getMessage());
}
}
}
}
}
package com.lemon.testhotfix;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import dalvik.system.DexClassLoader;
import dalvik.system.PathClassLoader;
public class HotFixEngine {
public static final String DEX_OPT_DIR = "optimize_dex";//dex的优化路径
public static final String DEX_BASECLASSLOADER_CLASS_NAME = "dalvik.system.BaseDexClassLoader";
public static final String DEX_ELEMENTS_FIELD = "dexElements";//pathList中的dexElements字段
public static final String DEX_PATHLIST_FIELD = "pathList";//BaseClassLoader中的pathList字段
public static final String FIX_DEX_PATH = "fix_dex";//fixDex存储的路径
/**
* 获得pathList中的dexElements
*
* @param obj
* @return
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public Object getDexElements(Object obj) throws NoSuchFieldException, IllegalAccessException {
return getField(obj, obj.getClass(), DEX_ELEMENTS_FIELD);
}
/**
* fix
*
* @param context
*/
public void loadDex(Context context, File dexFile) {
if (context == null) {
Log.d("======>","context==null");
return;
}
File fixDir = context.getDir(FIX_DEX_PATH, Context.MODE_PRIVATE);
//mrege and fix
mergeDex(context, fixDir,dexFile);
}
/**
* 获取指定classloader 中的pathList字段的值(DexPathList)
*
* @param classLoader
* @return
*/
public Object getDexPathListField(Object classLoader) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
return getField(classLoader, Class.forName(DEX_BASECLASSLOADER_CLASS_NAME), DEX_PATHLIST_FIELD);
}
/**
* 获取一个字段的值
*
* @return
*/
public Object getField(Object obj, Class clz, String fieldName) throws NoSuchFieldException, IllegalAccessException {
Field field = clz.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(obj);
}
/**
* 为指定对象中的字段重新赋值
*
* @param obj
* @param claz
* @param filed
* @param value
*/
public void setFiledValue(Object obj, Class claz, String filed, Object value) throws NoSuchFieldException, IllegalAccessException {
Field field = claz.getDeclaredField(filed);
field.setAccessible(true);
field.set(obj, value);
}
/**
* 合并dex
*
* @param context
* @param fixDexPath
*/
public void mergeDex(Context context, File fixDexPath, File dexFile) {
try {
//创建dex的optimize路径
File optimizeDir = new File(fixDexPath.getAbsolutePath(), DEX_OPT_DIR);
if (!optimizeDir.exists()) {
optimizeDir.mkdir();
}
//加载自身Apk的dex,通过PathClassLoader
PathClassLoader pathClassLoader = (PathClassLoader) context.getClassLoader();
//找到dex并通过DexClassLoader去加载
//dex文件路径,优化输出路径,null,父加载器
DexClassLoader dexClassLoader = new DexClassLoader(dexFile.getAbsolutePath(), optimizeDir.getAbsolutePath(), null, pathClassLoader);
//获取app自身的BaseDexClassLoader中的pathList字段
Object appDexPathList = getDexPathListField(pathClassLoader);
//获取补丁的BaseDexClassLoader中的pathList字段
Object fixDexPathList = getDexPathListField(dexClassLoader);
Object appDexElements = getDexElements(appDexPathList);
Object fixDexElements = getDexElements(fixDexPathList);
//合并两个elements的数据,将修复的dex插入到数组最前面
Object finalElements = combineArray(fixDexElements, appDexElements);
//给app 中的dex pathList 中的dexElements 重新赋值
setFiledValue(appDexPathList, appDexPathList.getClass(), DEX_ELEMENTS_FIELD, finalElements);
Log.d("======>","修复成功!");
} catch (Exception e) {
e.printStackTrace();
Log.d("======>",e.getMessage());
}
}
/**
* 两个数组合并
*
* @param arrayLhs
* @param arrayRhs
* @return
*/
private static Object combineArray(Object arrayLhs, Object arrayRhs) {
Class localClass = arrayLhs.getClass().getComponentType();
int i = Array.getLength(arrayLhs);
int j = i + Array.getLength(arrayRhs);
Object result = Array.newInstance(localClass, j);
for (int k = 0; k < j; ++k) {
if (k < i) {
Array.set(result, k, Array.get(arrayLhs, k));
} else {
Array.set(result, k, Array.get(arrayRhs, k - i));
}
}
return result;
}
}
3、打包或者直接跑在手机或者模拟器上,然后改动Bugs的方法:Bugs.getBugs(){ return "已修复!";},重新编译。
4、找到Bugs.class,用sdk/build-tools/dx.bat工具转成dex文件。
dx --dex --output=patch.dex 包名/Bugs.class
5、将patch.dex发送到手机的根目录。重启应用即可。
资源https://download.csdn.net/download/qq_35022307/10608280