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 的 register-activity-plugin.apk 拷贝到 /storage/sdcard0/Android/data/[package name]/cache
// 或者 /storage/sdcard0/Android/data/[package name]/files
final File dexPath = new File(dir + File.separator + "register-activity-plugin.apk");
AssetsUtils.copyAssets(this, "register-activity-plugin.apk", dexPath.getAbsolutePath());
// /data/data/[package name]/app_register-activity-plugin
final File optimizedDirectory = this.getDir("register-activity-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 LoadedPlugin method createClassLoader.
/**
* 构造一个加载该 Apk 的 classloader
*
* 然后把这个 classloader 的 Element 插入宿主的 DexPathList # Element[] dexElements 中
* 宿主 classloader 也可以访问该 Apk 内的 class
*
* @param context context
* @param apk apk
* @param libsDir libsDir
* @param parent parent
* @return ClassLoader
*/
private static ClassLoader createClassLoader(Context context, File apk, File libsDir, ClassLoader parent) {
File dexOutputDir = context.getDir(Constants.OPTIMIZE_DIR, Context.MODE_PRIVATE);
String dexOutputPath = dexOutputDir.getAbsolutePath();
DexClassLoader loader = new DexClassLoader(apk.getAbsolutePath(), dexOutputPath, libsDir.getAbsolutePath(), parent);
if (Constants.COMBINE_CLASSLOADER) {
try {
DexUtil.insertDex(loader);
} catch (Exception e) {
e.printStackTrace();
}
}
return loader;
}
use of dalvik.system.DexClassLoader in project AndroidLife by CaMnter.
the class HotPatch method inject.
public static void inject(String path, Context context) {
File file = new File(path);
if (file.exists()) {
try {
// 获取 classes 的 dexElements
Class<?> cl = Class.forName("dalvik.system.BaseDexClassLoader");
Object pathList = ReflectionUtils.getField(cl, "pathList", context.getClassLoader());
Object baseElements = ReflectionUtils.getField(pathList.getClass(), "dexElements", pathList);
// 获取 patch_dex 的 dexElements(需要先加载dex)
String dexopt = context.getDir("dexopt", 0).getAbsolutePath();
DexClassLoader dexClassLoader = new DexClassLoader(path, dexopt, dexopt, context.getClassLoader());
Object obj = ReflectionUtils.getField(cl, "pathList", dexClassLoader);
Object dexElements = ReflectionUtils.getField(obj.getClass(), "dexElements", obj);
// 合并两个 Elements
Object combineElements = ReflectionUtils.combineArray(dexElements, baseElements);
// 将合并后的 Element 数组重新赋值给 app 的 classLoader
ReflectionUtils.setField(pathList.getClass(), "dexElements", pathList, combineElements);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.e("HotPatch", file.getAbsolutePath() + "does not exists");
}
}
use of dalvik.system.DexClassLoader in project AndroidLife by CaMnter.
the class HotFix method injectBelowApiLevel14.
/**
* 小于 API 14 的插桩策略
*
* @param context context
* @param str 插件 dex 的路径
* @param str2 插件 dex 中要修复的类 name
* @throws ClassNotFoundException
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
@TargetApi(14)
private static void injectBelowApiLevel14(Context context, String str, String str2) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
PathClassLoader obj = (PathClassLoader) context.getClassLoader();
DexClassLoader dexClassLoader = new DexClassLoader(str, context.getDir("dex", 0).getAbsolutePath(), str, context.getClassLoader());
dexClassLoader.loadClass(str2);
setField(obj, PathClassLoader.class, "mPaths", appendArray(getField(obj, PathClassLoader.class, "mPaths"), getField(dexClassLoader, DexClassLoader.class, "mRawDexPath")));
setField(obj, PathClassLoader.class, "mFiles", combineArray(getField(obj, PathClassLoader.class, "mFiles"), getField(dexClassLoader, DexClassLoader.class, "mFiles")));
setField(obj, PathClassLoader.class, "mZips", combineArray(getField(obj, PathClassLoader.class, "mZips"), getField(dexClassLoader, DexClassLoader.class, "mZips")));
setField(obj, PathClassLoader.class, "mDexs", combineArray(getField(obj, PathClassLoader.class, "mDexs"), getField(dexClassLoader, DexClassLoader.class, "mDexs")));
obj.loadClass(str2);
}
use of dalvik.system.DexClassLoader in project blog_resource by Guolei1130.
the class MainActivity method enableInstantRun.
private void enableInstantRun() {
// 为了简单,就不修改ClassLoader了,直接用一个新的ClassLoader去加载。
String dexPath = new File("/sdcard/instant_run.jar").getPath();
File dexOptOutDir = new File(getFilesDir(), "dexopt");
if (!dexOptOutDir.exists()) {
boolean result = dexOptOutDir.mkdir();
if (!result) {
Log.e(TAG, "loadExtJar: create out dir error");
}
}
String dexOptOutDr = dexOptOutDir.getPath();
DexClassLoader dexClassLoader = new DexClassLoader(dexPath, dexOptOutDr, null, ClassLoader.getSystemClassLoader());
try {
$change = dexClassLoader.loadClass("com.guolei.hotfixdemo.MainActivity$override").getConstructor(Object.class).newInstance(this);
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations