Android熱修復(fù)筆記
java--->class---jar--->dex
使用方法:1、運(yùn)行修復(fù)的程序,
2、在build-->intermediates-->javac-->debug--->classes 在這個文件夾打開終端窗口運(yùn)行 jar cvf fix.jar qzone/Fix.class
(javac -encoding UTF-8 qzone/Fix.java
或者
studio編譯之后在build-intermediates-javac-debug-classes文件中查找)
3、得到j(luò)ar之后,把classes的jar復(fù)制到sdk->build-tools->版本 中(dx)
使用 dx --dex --output=fix.dex fix.jar 命令 得到dex

image.png

image.png

image.png

image.png
FixUtil工具類
public class FixUtil {
static String TAG="hotfix";
public static void installPatch(Context context, String patchPath) {
PsyP();
File patchFile = new File(patchPath);
if (!patchFile.exists()) {
Log.e(TAG, "installPatch: " + patchPath + "無文件,無需修復(fù)");
return;
}
File optDir = context.getCacheDir();
try {
// 1.得到當(dāng)前程序的classLoader
ClassLoader classLoader = context.getClassLoader();
// 2.反射獲取類加載器的pathList屬性
Field pathListField = getField(Class.forName("dalvik.system.BaseDexClassLoader"), "pathList");
Object pathList = pathListField.get(classLoader);
// 3.反射獲取DexPathList中的dexElements
Field dexElementsField = getField(pathList.getClass(), "dexElements");
Object[] dexElements = (Object[]) dexElementsField.get(pathList);
// 4.加載自己的dex,并轉(zhuǎn)化為Element[]
// 通過反射DexPathList 中的makeDexElements() 方法來實現(xiàn)
// 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 = 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 = 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) 注意這里參數(shù)編程ArrayList類型
Method makeDexElementsMethod = 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) 注意這里參數(shù)編程ArrayList類型
// 和5.0一樣的方法,注意4.4 會報錯 Class ref in pre-verified class resolved to unexpected implementation
Log.e(TAG, "installPatch: 當(dāng)前版本:" + Build.VERSION.SDK_INT + "此版本暫未解決 Class ref in pre-verified class resolved to unexpected implementation 的問題");
return;
} else {
Log.e(TAG, "installPatch: 當(dāng)前版本:" + 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: 修復(fù)成功");
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "installPatch: 修復(fù)失敗!!" + e.getMessage());
}
}
/**
* 10.0以上反射調(diào)用隱蔽方法
*/
static void PsyP() {
if (SDK_INT < Build.VERSION_CODES.P) {
return;
}
try {
//設(shè)置豁免所有hide api https://developer.android.google.cn/about/versions/10/non-sdk-q
Method forName = Class.class.getDeclaredMethod("forName", String.class);
Method getDeclaredMethod = Class.class.getDeclaredMethod("getDeclaredMethod", String.class, Class[].class);
Class<?> vmRuntimeClass = (Class<?>) forName.invoke(null, "dalvik.system.VMRuntime");
Method getRuntime = (Method) getDeclaredMethod.invoke(vmRuntimeClass, "getRuntime", null);
Method setHiddenApiExemptions = (Method) getDeclaredMethod.invoke(vmRuntimeClass, "setHiddenApiExemptions", new Class[]{String[].class});
Object sVmRuntime = getRuntime.invoke(null);
setHiddenApiExemptions.invoke(sVmRuntime, new Object[]{new String[]{"L"}});
} catch (
Throwable e) {
Log.e("[error]", "reflect bootstrap failed:", e);
}
}
/**
* 反射獲取屬性 會從所有父類中找,不區(qū)分private
*/
public static Field getField(Class cls, String fieldName) {
// 如果自己類中找不到 就去父類中遞歸查找
Field declaredField = null;
while (cls != null) {
Log.e(TAG, "getField: 當(dāng)前類" + 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;
}
/**
* 反射獲取方法 會從所有父類中找,不區(qū)分private 注意方法需要傳入?yún)?shù)
*/
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;
}
}
Fix修復(fù)類(要修復(fù)的類)
public class Fix {
public static String getMessage() {
// return "程序出bug了,趕緊叫人來修補(bǔ)?。?!";
return "程序的問題已經(jīng)修復(fù)完成(這是修復(fù)之后的類文件及方法)";
}
}
AppLication
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Log.e("Application", "----------path:" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/fix.dex");
FixUtil.installPatch(this, Environment.getExternalStorageDirectory().getAbsolutePath() + "/fix.dex");
// FixUtil.installPatch(this, getFilesDir()+ File.separator+"patch.dex");
}
}