不廢話,直接上步驟,跟着一步步操作就行!
點擊下載《熱修復demo》
- 以下兩個類文件是核心代碼,可直接複製
import android.content.Context;
import android.os.Build;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class FixUtil {
private static final String TAG = "FixUtil";
public static void installPatch(Context context, String patchPath) {
File patchFile = new File(patchPath);
if (!patchFile.exists()) {
Log.e(TAG, "installPatch: " + patchPath + "無文件,無需修復");
return;
}
File optDir = context.getCacheDir();
try {
// 1.得到當前程序的classLoader
ClassLoader classLoader = context.getClassLoader();
// 2.反射獲取類加載器的pathList屬性
Class<? extends ClassLoader> pathClassLoaderCls = classLoader.getClass();
Field pathListField = ReflectUtil.getField(pathClassLoaderCls, "pathList");
Object pathList = pathListField.get(classLoader);
// 3.反射獲取DexPathList中的dexElements
Field dexElementsField = ReflectUtil.getField(pathList.getClass(), "dexElements");
Object[] dexElements = (Object[]) dexElementsField.get(pathList);
// 4.加載自己的dex,並轉化爲Element[]
// 通過反射DexPathList 中的makeDexElements() 方法來實現
// 4.1 反射獲取makeDexElements方法
Object[] patchElements = null;
// 版本適配
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// Android 7.0+ 使用makeDexElements(List,File,List,ClassLoader)
Method makeDexElementsMethod = ReflectUtil.getMethod(pathList.getClass(), "makeDexElements", List.class, File.class, List.class, ClassLoader.class);
List<File> dexFileList = new ArrayList<>();
dexFileList.add(patchFile);
ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
patchElements = (Object[]) makeDexElementsMethod.invoke(null, dexFileList, optDir, suppressedExceptions, classLoader);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Android 6.0-7.0 使用makePathElements(List,File,List)
Method makePathElements = ReflectUtil.getMethod(pathList.getClass(), "makePathElements", List.class, File.class, List.class);
List<File> dexFileList = new ArrayList<>();
dexFileList.add(patchFile);
ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
patchElements = (Object[]) makePathElements.invoke(null, dexFileList, optDir, suppressedExceptions);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Android 5.0-6.0 使用makeDexElements(ArrayList,File,ArrayList) 注意這裏參數編程ArrayList類型
Method makeDexElementsMethod = ReflectUtil.getMethod(pathList.getClass(), "makeDexElements", ArrayList.class, File.class, ArrayList.class);
List<File> dexFileList = new ArrayList<>();
dexFileList.add(patchFile);
ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
patchElements = (Object[]) makeDexElementsMethod.invoke(null, dexFileList, optDir, suppressedExceptions);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Android 4.4-5.0 使用makeDexElements(ArrayList,File,ArrayList) 注意這裏參數編程ArrayList類型
// 和5.0一樣的方法,注意4.4 會報錯 Class ref in pre-verified class resolved to unexpected implementation
Log.e(TAG, "installPatch: 當前版本:" + Build.VERSION.SDK_INT + "此版本暫未解決 Class ref in pre-verified class resolved to unexpected implementation 的問題,敬請期待!");
return;
} else {
Log.e(TAG, "installPatch: 當前版本:" + Build.VERSION.SDK_INT + "不支持熱更新");
return;
}
// 5.合併兩個Element[] patchElements要放在前面
Object[] newElements = (Object[]) Array.newInstance(dexElements.getClass().getComponentType(), dexElements.length + patchElements.length);
System.arraycopy(patchElements, 0, newElements, 0, patchElements.length);
System.arraycopy(dexElements, 0, newElements, patchElements.length, dexElements.length);
// 6.反射替換原來的dexElements
dexElementsField.set(pathList, newElements);
Log.e(TAG, "installPatch: 修復成功");
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "installPatch: 修復失敗!!" + e.getMessage());
}
}
}
import android.util.Log;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
反射工具
*/
public class ReflectUtil {
private static final String TAG = "ReflectUtil";
/**
* 反射獲取屬性 會從所有父類中找,不區分private
*/
public static Field getField(Class cls, String fieldName) {
// 如果自己類中找不到 就去父類中遞歸查找
Field declaredField = null;
while (cls != null) {
Log.e(TAG, "getField: 當前類" + cls.getName());
try {
declaredField = cls.getDeclaredField(fieldName);
if (declaredField != null) {
declaredField.setAccessible(true);
return declaredField;
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
cls = cls.getSuperclass();
}
// 沒有找到
return null;
}
/**
* 反射獲取方法 會從所有父類中找,不區分private 注意方法需要傳入參數
*/
public static Method getMethod(Class cls, String methodName, Class<?>... paramTypes) {
// 如果自己類中找不到 就去父類中遞歸查找
Method declaredMethod = null;
while (cls != null) {
try {
declaredMethod = cls.getDeclaredMethod(methodName, paramTypes);
if (declaredMethod != null) {
declaredMethod.setAccessible(true);
return declaredMethod;
}
cls = cls.getSuperclass();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
// 沒有找到
return null;
}
}
- 創建完以上兩個類後即可開始熱修復;
- 創建一個activity和測試類
BugTest.java
public class BugTest {
public static String getMessage() {
return "我是渣渣輝";
}
}
HotAcitivty.java
public class HotAcitivty extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activtiy_hot);
Toast.makeText(this, BugTest.getMessage(), Toast.LENGTH_LONG).show();
}
}
- 將此BugTest.java轉換爲dex文件並命名爲patch.dex,具體方法:java轉dex
- 在程序入口文件中添加如下代碼:
public class HotFixApp extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
FixUtil.installPatch(this, getFilesDir() + File.separator + "patch.dex");
}
}
- 修改BugTest.java 爲以下代碼並運行,彈出提示"我是沽添樂"
public class BugTest {
public static String getMessage() {
return "我是沽添樂";
}
}
-
將patch.dex文件移動到app的私有路徑:具體操作
“adb push E:\patch.dex /data/user/0/com.oh.mygod/files”.
(ps:私有路徑可通過log.d(getFilesDir())獲取) -
再次運行app,則提示"我是渣渣輝";
-
至此,熱修復成功!