// DexClassLoader.java
public class DexClassLoader extends BaseDexClassLoader {
public DexClassLoader(String dexPath, String optimizedDirectory,
String libraryPath, ClassLoader parent) {
super(dexPath, new File(optimizedDirectory), libraryPath, parent);
}
}
// PathClassLoader.java
public class PathClassLoader extends BaseDexClassLoader {
public PathClassLoader(String dexPath, ClassLoader parent) {
super(dexPath, null, null, parent);
}
public PathClassLoader(String dexPath, String libraryPath,
ClassLoader parent) {
super(dexPath, null, libraryPath, parent);
}
}
public static boolean injectDexAtFirst(String dexPath, String dexOptPath) {
// 获取系统的dexElements
Object baseDexElements = getDexElements(getPathList(getPathClassLoader()));
// 获取patch的dexElements
DexClassLoader patchDexClassLoader = new DexClassLoader(dexPath, dexOptPath, dexPath, getPathClassLoader());
Object patchDexElements = getDexElements(getPathList(patchDexClassLoader));
// 组合最新的dexElements
Object allDexElements = combineArray(patchDexElements, baseDexElements);
// 将最新的dexElements添加到系统的classLoader中
Object pathList = getPathList(getPathClassLoader());
FieldUtils.writeField(pathList, "dexElements", allDexElements);
}
public static ClassLoader getPathClassLoader() {
return DexUtils.class.getClassLoader();
}
/**
* 反射调用getPathList方法,获取数据
* @param classLoader
* @return
* @throws ClassNotFoundException
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public static Object getPathList(ClassLoader classLoader) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
return FieldUtils.readField(classLoader, "pathList");
}
/**
* 反射调用pathList对象的dexElements数据
* @param pathList
* @return
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public static Object getDexElements(Object pathList) throws NoSuchFieldException, IllegalAccessException {
LogUtils.d("Reflect To Get DexElements");
return FieldUtils.readField(pathList, "dexElements");
}
/**
* 拼接dexElements,将patch的dex插入到原来dex的头部
* @param firstElement
* @param secondElement
* @return
*/
public static Object combineArray(Object firstElement, Object secondElement) {
LogUtils.d("Combine DexElements");
// 取得一个数组的Class对象, 如果对象是数组,getClass只能返回数组类型,而getComponentType可以返回数组的实际类型
Class objTypeClass = firstElement.getClass().getComponentType();
int firstArrayLen = Array.getLength(firstElement);
int secondArrayLen = Array.getLength(secondElement);
int allArrayLen = firstArrayLen + secondArrayLen;
Object allObject = Array.newInstance(objTypeClass, allArrayLen);
for (int i = 0; i < allArrayLen; i++) {
if (i < firstArrayLen) {
Array.set(allObject, i, Array.get(firstElement, i));
} else {
Array.set(allObject, i, Array.get(secondElement, i - firstArrayLen));
}
}
return allObject;
}
public static void loadPatchResources(Context context, String apkPath) throws Exception {
AssetManager newAssetManager = AssetManager.class.newInstance();
invokeMethod(newAssetManager, "addAssetPath", apkPath);
invokeMethod(newAssetManager, "ensureStringBlocks");
replaceAssetManager(context, newAssetManager);
}
private static void replaceAssetManager(Context context, AssetManager newAssetManager)
throws Exception {
Collection<WeakReference<Resources>> references;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Class<?> resourcesManagerClass = Class.forName("android.app.ResourcesManager");
Object resourcesManager = invokeStaticMethod(resourcesManagerClass, "getInstance");
if (getField(resourcesManagerClass, "mActiveResources") != null) {
ArrayMap<?, WeakReference<Resources>> arrayMap =
(ArrayMap) readField(resourcesManager, "mActiveResources", true);
references = arrayMap.values();
} else {
references = (Collection) readField(resourcesManager, "mResourceReferences", true);
}
} else {
HashMap<?, WeakReference<Resources>> map =
(HashMap) readField(ActivityThreadCompat.instance(), "mActiveResources", true);
references = map.values();
}
AssetManager assetManager = context != null ? context.getAssets() : null;
for (WeakReference<Resources> wr : references) {
Resources resources = wr.get();
if (resources == null) continue;
try {
writeField(resources, "mAssets", newAssetManager);
originalAssetManager = assetManager;
} catch (Throwable ignore) {
Object resourceImpl = readField(resources, "mResourcesImpl", true);
writeField(resourceImpl, "mAssets", newAssetManager);
}
resources.updateConfiguration(resources.getConfiguration(),
resources.getDisplayMetrics());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
for (WeakReference<Resources> wr : references) {
Resources resources = wr.get();
if (resources == null) continue;
// android.util.Pools$SynchronizedPool<TypedArray>
Object typedArrayPool = readField(resources, "mTypedArrayPool", true);
// Clear all the pools
while (invokeMethod(typedArrayPool, "acquire") != null) ;
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有