use of dalvik.system.DexClassLoader in project Auto.js by hyb1996.
the class AndroidClassLoader method loadDex.
public DexClassLoader loadDex(File file) throws FileNotFoundException {
Log.d(LOG_TAG, "loadDex: file = " + file);
if (!file.exists()) {
throw new FileNotFoundException(file.getPath());
}
DexClassLoader loader = new DexClassLoader(file.getPath(), mCacheDir.getPath(), null, parent);
mDexClassLoaders.add(loader);
return loader;
}
use of dalvik.system.DexClassLoader in project blog_resource by Guolei1130.
the class MainActivity method loadExtApk.
private void loadExtApk() {
String apkPath = new File("/sdcard/plugin_1.apk").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();
ClassLoader classLoader = null;
if (Constants.isDalvik()) {
classLoader = new DexClassLoader(apkPath, dexOptOutDr, null, ClassLoader.getSystemClassLoader());
} else {
classLoader = new PathClassLoader(apkPath, ClassLoader.getSystemClassLoader());
}
try {
Class userClz = classLoader.loadClass("com.guolei.plugin_1.People");
Object user = userClz.getConstructor(String.class, int.class).newInstance("guolei", 24);
Method method = userClz.getDeclaredMethod("toString");
method.setAccessible(true);
String msg = (String) method.invoke(user);
Log.e(TAG, "loadExtApk: " + msg);
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
use of dalvik.system.DexClassLoader in project FastAndroid by jerryliuinfo.
the class HotFixManager method loadDex.
public static void loadDex(Context context) {
if (context == null) {
return;
}
// 获取当前应用的上下文的私有路径,也就是dex目录
File filesDir = context.getDir("odex", Context.MODE_PRIVATE);
FrameworkLogUtil.d("loadDex filesDir path : " + filesDir.getAbsolutePath());
File[] files = filesDir.listFiles();
for (File file : files) {
FrameworkLogUtil.d("loadDex file path : " + file.getAbsolutePath() + ", name:" + file.getName());
if (file.getName().contains("classes") || file.getName().endsWith(".dex")) {
loadedDex.add(file);
}
}
// 创建一个目录,用来存放解压的文件
String optmizeDir = filesDir.getAbsolutePath() + File.separator + "opt_dex";
FrameworkLogUtil.d("loadDex optmizeDir +" + optmizeDir);
File optimizeDir = new File(optmizeDir);
if (!optimizeDir.exists()) {
optimizeDir.mkdirs();
}
FrameworkLogUtil.d("loadDex loadedDex : " + loadedDex);
for (File dex : loadedDex) {
DexClassLoader myDexClassLoader = new DexClassLoader(dex.getAbsolutePath(), optimizeDir.getAbsolutePath(), null, context.getClassLoader());
// 实现一个类加载器对象
PathClassLoader pathClassLoader = (PathClassLoader) context.getClassLoader();
try {
/**
************获取系统的未修复的dexElements开始***************
*/
// 获取到DexClassLoader类加载器的父类
Class<?> baseDexCkazzLoader = Class.forName("dalvik.system.BaseDexClassLoader");
Field pathListField = baseDexCkazzLoader.getDeclaredField("pathList");
// 设置成员变量允许被访问,用于反射
pathListField.setAccessible(true);
// 执行pathList的get方法
Object pathListObject = pathListField.get(pathClassLoader);
// 获取到PathList类对象
Class<?> systemPathListClass = pathListObject.getClass();
// 通过这类对象获取到它里面的一个叫做dexElement的成员表变量
Field dexElements = systemPathListClass.getDeclaredField("dexElements");
dexElements.setAccessible(true);
// 获取到dexElements成员变量的值
Object systemElements = dexElements.get(pathListObject);
/**
************获取系统的未修复的dexElements结束***************
*/
/**
************获取已修复的dexElements开始***************
*/
// 通过反射获取类加载器的父类
Class<?> myDexClazzLoader = Class.forName("dalvik.system.BaseDexClassLoader");
// 从父类的对象中去获取pathList的成员变量
Field myPathListField = baseDexCkazzLoader.getDeclaredField("pathList");
myPathListField.setAccessible(true);
Object myPathListObject = myPathListField.get(myDexClassLoader);
Field myDexElements1Field = myPathListObject.getClass().getDeclaredField("dexElements");
myDexElements1Field.setAccessible(true);
Object myElements = myDexElements1Field.get(myPathListObject);
/**
************获取已修复的dexElements结束***************
*/
/**
************dex文件的融合***************
*/
// 获取到系统的(未修复)systemElements的类对象
Class<?> componentType = systemElements.getClass().getComponentType();
// 得到系统的的dexElements的长度
int systemLength = Array.getLength(systemElements);
// 获取到修复的dexElements长度
int myLength = Array.getLength(myElements);
// 创建一个新的长度
int newLength = systemLength + myLength;
Object newElementArray = Array.newInstance(componentType, newLength);
for (int i = 0; i < newLength; i++) {
if (i < myLength) {
// 将补丁的myElements的第i位放到newElementArray 的第i位置
Array.set(newElementArray, i, Array.get(myElements, i));
} else {
Array.set(newElementArray, i, Array.get(systemElements, i - myLength));
}
}
// 将系统的数组和我们定义的数组融合之后,再放入到系统的数组中
Field dexElementsField = pathListObject.getClass().getDeclaredField("dexElements");
dexElementsField.setAccessible(true);
dexElementsField.set(pathListObject, newElementArray);
} catch (Exception e) {
e.printStackTrace();
}
}
}
use of dalvik.system.DexClassLoader in project XCFramework by wydzxc21.
the class XCHotfixUtil method doDexInject.
/**
* @author ZhangXuanChen
* @date 2021/6/29
* @description doDexInject
*/
private static boolean doDexInject(Context context, File dexDir) {
if (context == null || dexDir == null) {
return false;
}
try {
HashSet<File> dexList = getDexList(dexDir);
if (dexList != null && !dexList.isEmpty()) {
File optimizedFile = new File(getOptimizedPath(context));
if (!optimizedFile.exists()) {
optimizedFile.mkdirs();
}
//
PathClassLoader pathLoader = (PathClassLoader) context.getClassLoader();
for (File dexFile : dexList) {
DexClassLoader dexLoader = new DexClassLoader(dexFile.getAbsolutePath(), optimizedFile.getAbsolutePath(), null, pathLoader);
//
Object dexPathList = getPathList(dexLoader);
Object pathPathList = getPathList(pathLoader);
//
Object leftDexElements = getDexElements(dexPathList);
Object rightDexElements = getDexElements(pathPathList);
//
Object dexElements = combineArray(leftDexElements, rightDexElements);
//
Object pathList = getPathList(pathLoader);
setField(pathList, pathList.getClass(), "dexElements", dexElements);
}
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
use of dalvik.system.DexClassLoader in project Shadow by Tencent.
the class ImplLoader method loadWhiteList.
public String[] loadWhiteList(InstalledApk installedApk, String whiteListClassName, String whiteListFieldName) {
DexClassLoader dexClassLoader = new DexClassLoader(installedApk.apkFilePath, installedApk.oDexPath, installedApk.libraryPath, getClass().getClassLoader());
String[] whiteList = null;
try {
Class<?> whiteListClass = dexClassLoader.loadClass(whiteListClassName);
Field whiteListField = whiteListClass.getDeclaredField(whiteListFieldName);
Object o = whiteListField.get(null);
whiteList = (String[]) o;
} catch (ClassNotFoundException ignored) {
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
String[] interfaces;
if (whiteList != null) {
interfaces = concatenate(getCustomWhiteList(), whiteList);
} else {
interfaces = getCustomWhiteList();
}
return interfaces;
}
Aggregations