use of dalvik.system.DexClassLoader in project BestPracticeApp by pop1234o.
the class DexInsertHotFix method loadFix.
public static void loadFix(Context context) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
File directory = Environment.getExternalStorageDirectory();
File dexFile = new File(directory, "classes.dex");
if (!dexFile.exists()) {
Log.i("test", "补丁不存在");
return;
}
File optimizedDirectory = new File(context.getFilesDir(), "optimizedDirectory");
if (!optimizedDirectory.exists()) {
optimizedDirectory.mkdirs();
}
// 系统的类加载器
PathClassLoader pathClassLoader = (PathClassLoader) context.getClassLoader();
// 创建我们补丁的类加载器
DexClassLoader dexClassLoader = new DexClassLoader(dexFile.getAbsolutePath(), optimizedDirectory.getAbsolutePath(), null, pathClassLoader);
Field pathList = Class.forName("dalvik.system.BaseDexClassLoader").getDeclaredField("pathList");
pathList.setAccessible(true);
// 获取两个classLoader的pathList的成员变量(DexPathList.java)
Object pathPathList = pathList.get(pathClassLoader);
Object dexPathList = pathList.get(dexClassLoader);
Field dexElements = Class.forName("dalvik.system.DexPathList").getDeclaredField("dexElements");
dexElements.setAccessible(true);
// (DexPathList.java中的Element[]数组,这个是个内部类)
Object leftDexElements = dexElements.get(dexPathList);
Object rightDexElements = dexElements.get(pathPathList);
Class<?> componentType = leftDexElements.getClass().getComponentType();
// 得到左数组长度(补丁数组)
int i = Array.getLength(leftDexElements);
// 得到原dex数组长度
int j = Array.getLength(rightDexElements);
// 得到总数组长度(补丁数组+原dex数组)
int k = i + j;
// 创建一个类型为componentType,长度为k的新数组
Object result = Array.newInstance(componentType, k);
System.arraycopy(leftDexElements, 0, result, 0, i);
System.arraycopy(rightDexElements, 0, result, i, j);
// 将新的 Element[] 赋值到 PathDexClassLoader 对象中的pathList,中的 Element[] dexElements;
// 获取 (DexPathList)对象
Object pathPathList_1 = pathList.get(pathClassLoader);
// 将新的Element[] 赋值到 PathDexClassLoader->DexPathList->Element[]中
dexElements.set(pathPathList_1, result);
Log.i("test", "合并完成");
}
use of dalvik.system.DexClassLoader in project MagiskManager by topjohnwu.
the class CheckSafetyNet method dyload.
private void dyload() throws Exception {
DexClassLoader loader = new DexClassLoader(dexPath.getPath(), dexPath.getParent(), null, ISafetyNetHelper.class.getClassLoader());
Class<?> clazz = loader.loadClass("com.topjohnwu.snet.SafetyNetHelper");
helper = (ISafetyNetHelper) clazz.getConstructors()[0].newInstance(getActivity(), (ISafetyNetHelper.Callback) code -> MagiskManager.get().safetyNetDone.publish(false, code));
if (helper.getVersion() != Const.SNET_VER) {
throw new Exception();
}
}
use of dalvik.system.DexClassLoader in project AndroidLife by CaMnter.
the class SmartApplication method loadDex.
private void loadDex() {
try {
String dir = null;
final File cacheDir = this.getExternalCacheDir();
final File filesDir = this.getExternalFilesDir("");
if (cacheDir != null) {
dir = cacheDir.getAbsolutePath();
} else {
if (filesDir != null) {
dir = filesDir.getAbsolutePath();
}
}
if (TextUtils.isEmpty(dir))
return;
// assets 的 load-plugin-resource-plugin.apk 拷贝到 /storage/sdcard0/Android/data/[package name]/cache
// 或者 /storage/sdcard0/Android/data/[package name]/files
final File dexPath = new File(dir + File.separator + "load-plugin-resource-plugin.apk");
final String dexAbsolutePath = dexPath.getAbsolutePath();
AssetsUtils.copyAssets(this, "load-plugin-resource-plugin.apk", dexAbsolutePath);
// /data/data/[package name]/app_load-plugin-resource-plugin
final File optimizedDirectory = this.getDir("load-plugin-resource-plugin", MODE_PRIVATE);
this.dexClassLoader = new DexClassLoader(dexPath.getAbsolutePath(), optimizedDirectory.getAbsolutePath(), null, this.getClassLoader());
this.dexAbsolutePath = dexAbsolutePath;
} catch (Exception e) {
e.printStackTrace();
}
}
use of dalvik.system.DexClassLoader in project AndroidLife by CaMnter.
the class SmartApplication method loadDex.
private void loadDex() {
try {
String dir = null;
final File cacheDir = this.getExternalCacheDir();
final File filesDir = this.getExternalFilesDir("");
if (cacheDir != null) {
dir = cacheDir.getAbsolutePath();
} else {
if (filesDir != null) {
dir = filesDir.getAbsolutePath();
}
}
if (TextUtils.isEmpty(dir))
return;
// assets 的 load-service-plugin.apk 拷贝到 /storage/sdcard0/Android/data/[package name]/cache
// 或者 /storage/sdcard0/Android/data/[package name]/files
final File dexPath = new File(dir + File.separator + "load-service-plugin.apk");
final String dexAbsolutePath = dexPath.getAbsolutePath();
AssetsUtils.copyAssets(this, "load-service-plugin.apk", dexAbsolutePath);
// /data/data/[package name]/app_single-resources-plugin
final File optimizedDirectory = this.getDir("load-service-plugin", MODE_PRIVATE);
this.dexClassLoader = new DexClassLoader(dexPath.getAbsolutePath(), optimizedDirectory.getAbsolutePath(), null, this.getClassLoader());
} catch (Exception e) {
e.printStackTrace();
}
}
use of dalvik.system.DexClassLoader in project AndroidLife by CaMnter.
the class LoadSimplePluginActivity method initViews.
/**
* Initialize the view in the layout
*
* @param savedInstanceState savedInstanceState
*/
@Override
protected void initViews(Bundle savedInstanceState) {
try {
String dir = null;
final File cacheDir = this.getExternalCacheDir();
final File filesDir = this.getExternalFilesDir("");
if (cacheDir != null) {
dir = cacheDir.getAbsolutePath();
} else {
if (filesDir != null) {
dir = filesDir.getAbsolutePath();
}
}
if (TextUtils.isEmpty(dir))
return;
// assets 的 simple-plugin.dex 拷贝到 /storage/sdcard0/Android/data/[package name]/cache
// 或者 /storage/sdcard0/Android/data/[package name]/files
final File dexPath = new File(dir + File.separator + "simple-plugin.dex");
AssetsUtils.copyAssets(this, "simple-plugin.dex", dexPath.getAbsolutePath());
// /data/data/[package name]/app_simple-plugin
final File optimizedDirectory = this.getDir("simple-plugin", MODE_PRIVATE);
final DexClassLoader dexClassLoader = new DexClassLoader(dexPath.getAbsolutePath(), optimizedDirectory.getAbsolutePath(), null, this.getClassLoader());
final Class clazz = dexClassLoader.loadClass("com.camnter.load.simple.plugin.LoadSimplePlugin");
final LoadSimplePluginInterface pluginInterface = (LoadSimplePluginInterface) clazz.newInstance();
final TextView textView = this.findView(R.id.text);
textView.setText(pluginInterface.getInfo());
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations