本文主要是介绍多个dex合并遇到的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
合并使用到的技术
/**** @param mainclassLoader 合并到这里* @param pluginClassloader 被合并的dex*/public static void mergeDex(ClassLoader mainclassLoader,ClassLoader pluginClassloader){/* String cachePath = context.getCacheDir().getAbsolutePath();String apkPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/plugin.apk";DexClassLoader pluginClassloader = new DexClassLoader(apkPath, cachePath, cachePath, context.getClassLoader());*/// 第一步 找到 插件的Elements数组 dexPathlist ----?dexElementtry {
// Class myDexClazzLoader=pluginClassloader.getClass();Class myDexClazzLoader=Class.forName("dalvik.system.BaseDexClassLoader");Field myPathListFiled=myDexClazzLoader.getDeclaredField("pathList");myPathListFiled.setAccessible(true);Object myPathListObject =myPathListFiled.get(pluginClassloader);Class myPathClazz=myPathListObject.getClass();Field myElementsField = myPathClazz.getDeclaredField("dexElements");myElementsField.setAccessible(true);
// 自己插件的 dexElements[]Object myElements=myElementsField.get(myPathListObject);// 第二步 找到 main的Elements数组 dexElements
// ClassLoader pathClassLoader=mainclassLoader;Class baseDexClazzLoader =Class.forName("dalvik.system.BaseDexClassLoader");Field pathListFiled = baseDexClazzLoader.getDeclaredField("pathList");pathListFiled.setAccessible(true);Object pathListObject = pathListFiled.get(mainclassLoader);Class systemPathClazz = pathListObject.getClass();Field systemElementsField = systemPathClazz.getDeclaredField("dexElements");systemElementsField.setAccessible(true);//系统的 dexElements[]Object systemElements = systemElementsField.get(pathListObject);// 第三步 上面的dexElements 数组 合并成新的 dexElements 然后通过反射重新注入系统的Field (dexElements )变量中// 新的 Element[] 对象
// dalvik.system.Elementint systemLength = Array.getLength(systemElements);int myLength = Array.getLength(myElements);
// 找到 Element 的Class类型 数组 每一个成员的类型Class<?> sigleElementClazz = systemElements.getClass().getComponentType();int newSysteLength = myLength + systemLength;Object newElementsArray=Array.newInstance(sigleElementClazz, newSysteLength);
//融合for (int i = 0; i < newSysteLength; i++) {
// 先融合 插件的Elementsif (i < myLength) {Array.set(newElementsArray, i, Array.get(myElements, i));}else {Object value = Array.get(systemElements, i - myLength);Array.set(newElementsArray,i, value);}}Field elementsField=pathListObject.getClass().getDeclaredField("dexElements");;elementsField.setAccessible(true);
// 将新生成的EleMents数组对象重新放到系统中去elementsField.set( pathListObject,newElementsArray);} catch (Exception e) {e.printStackTrace();}}
问题汇总
Found duplicate classes, falling back to interpreter mode
11325-11325/cn.qssq666.pluginload W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
05-12 19:29:21.841 11325-11325/cn.qssq666.pluginload V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@a035b2a
05-12 19:29:21.948 11325-11325/cn.qssq666.pluginload W/MainActivity: CLASSLOADER length12
05-12 19:29:22.310 11325-11325/cn.qssq666.pluginload W/MainActivity: 融合file:/data/user/0/cn.qssq666.pluginload/app_robot_out_dex/classes.dex
05-12 19:29:22.397 11325-11325/cn.qssq666.pluginload W/MainActivity: 融合file:/data/user/0/cn.qssq666.pluginload/app_robot_out_dex/classes2.dex
05-12 19:29:22.408 11325-11325/cn.qssq666.pluginload W/art: Found duplicate classes, falling back to interpreter mode for /data/user/0/cn.qssq666.pluginload/app_robot_out_dex/classes2.dexFound duplicated class when checking oat files: 'Lcom/umeng/analytics/pro/m$1;' in /data/app/cn.qssq666.redpacket-2/base.apk and /data/user/0/cn.qssq666.pluginload/app_robot_out_dex/classes2.dexFailure to verify dex file '/data/user/0/cn.qssq666.pluginload/app_robot_out_dex/classes2.dex': Bad file size (1788928, expected 1787988d)
05-12 19:29:22.418 11325-11325/cn.qssq666.pluginload E/System: Unable to load dex file: /data/user/0/cn.qssq666.pluginload/app_robot_out_dex/classes2.dex
05-12 19:29:22.419 11325-11325/cn.qssq666.pluginload E/System: java.io.IOException: Failed to open dex files from /data/user/0/cn.qssq666.pluginload/app_robot_out_dex/classes2.dex because: Failure to verify dex file '/data/user/0/cn.qssq666.pluginload/app_robot_out_dex/classes2.dex': Bad file size (1788928, expected 1787988d)at dalvik.system.DexFile.openDexFileNative(Native Method)
真正的错误原因
W/art: Found duplicate classes, falling back to interpreter mode for /data/user/0/cn.qssq666.pluginload/app_robot_out
_dex/classes2.dexFound duplicated class when checking oat files:
'Lcom/umeng/analytics/pro/m$1;' in /data/app/cn.qssq666.redpacket-2/
base.apk and /data/user/0/cn.qssq666.pluginload/app_robot_out_dex/
classes2.dex
Failure to verify dex file
'/data/user/0/cn.qssq666.pluginload/app_robot_out_dex
这个问题是因为我的情迁红包插件apk,它给每一个apk都加入了友盟,里面使用到了友盟,结果也被加载进来了。那么这样的合并代码显然是有问题的,那么为什么xposed融合就没有问题呢?我直接不跟主classloader
融合了。
问题2 LinkageError:No superclass defined for class
No superclass defined for class java.lang.Object(declartion of ''java.lang.Object") appears in /data/user/0/xxxxx/apk_robot_out/class.dex
忘记注意classloader为空了。
DexClassLoader currentClassloader = new DexClassLoader(
file.getAbsolutePath(),
dataDir.getAbsolutePath(),
packageInfo.applicationInfo.nativeLibraryDir,
MainActivity.this.getClassLoader());
这篇关于多个dex合并遇到的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!