InputStream is = null;
try {
fos = new FileOutputStream(hackFile);
is = context.getAssets().open(“hack.dex”);
int len;
byte[] buffer = new byte[2048];
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return hackFile;
}
/**
-
1、获取程序的PathClassLoader对象
-
2、反射获得PathClassLoader父类BaseDexClassLoader的pathList对象
-
3、反射获取pathList的dexElements对象 (oldElement)
-
4、把补丁包变成Element数组:patchElement(反射执行makePathElements)
-
5、合并patchElement+oldElement = newElement (Array.newInstance)
-
6、反射把oldElement赋值成newElement
-
@param application
-
@param patch
*/
public static void installPatch(Application application, File patch) {
File hackDex = initHack(application);
List patchs = new ArrayList<>();
patchs.add(hackDex);
if (patch.exists()) {
patchs.add(patch);
}
//1、获取程序的PathClassLoader对象
ClassLoader classLoader = application.getClassLoader();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
ClassLoaderInjector.inject(application, classLoader, patchs);
} catch (Throwable throwable) {<