通过类DEX意外解决; [英] Class resolved by unexpected DEX;

查看:340
本文介绍了通过类DEX意外解决;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序,它会自动从外部apk文件外DEX装入类(外部APK文件存储在应用程序的内部存储)。外部apk文件都使用com.google.gson类。

I am developing a application which automatically load class from external dex from external apk file (external apk file is stored in internal storage of application). The external apk file have class using com.google.gson.

应用程序的源代码code

The source code of the application

// Internal storage where the DexClassLoader writes the optimized dex file to.
    final File optimizedDexOutputPath = mContext.getDir("optimize_offload", Context.MODE_PRIVATE);

    // Initialize the class loader with the s dex file.
    DexClassLoader dexClassLoader = new DexClassLoader(apkPath,  optimizedDexOutputPath.getAbsolutePath(),
            null,  mContext.getClassLoader());

    // Load the apk class from the class loader.
    Class<?> apkProviderClass = dexClassLoader.loadClass(className);        

    Object obj = apkProviderClass.newInstance();   

    // call method wrappers
    java.lang.reflect.Method method = apkProviderClass.getDeclaredMethod("R_" + methodName, String.class);
    System.out.println(method.getName());

    method.setAccessible(true); 

    Object result = method.invoke(obj, params); // Exception here

类外部APK文件有以下方法

Class in external apk file has following methods

public long R_catalanSum(String params) {

    Gson gson = new Gson();

    StartedSumLooperService_catalanSum param = 
            gson.fromJson(params, StartedSumLooperService_catalanSum.class);

    // Call local implementation
    return catalanSum(param.n); 
}

public long R_fiboSum(String params) {

    Gson gson = new Gson();

    StartedSumLooperService_fiboSum param = 
            gson.fromJson(params, StartedSumLooperService_fiboSum.class);

    // Call local implementation
    return fiboSum(param.n); 
}

但我面对如下问题,同时运行:

But I face a following problem while running:

03-05 17:10:59.479: W/dalvikvm(5433): Class resolved by unexpected DEX: Lorg/vkedco/android/startedsumlooperservice/StartedSumLooperService;(0x40f007d8):0x56f3a000 ref [Lcom/google/gson/Gson;] Lcom/google/gson/Gson;(0x40eabac0):0x56daa000

17 03 - 05:10:59.479:W / dalvikvm(5433):(Lorg / vkedco /安卓/ startedsumlooperservice / StartedSumLooperService;使用不同的LCOM /谷歌/ GSON / GSON; pre-验证期间)

03-05 17:10:59.479: W/dalvikvm(5433): (Lorg/vkedco/android/startedsumlooperservice/StartedSumLooperService; had used a different Lcom/google/gson/Gson; during pre-verification)

根据Android的生成过程中,与第三方库(如GSON这里)沿DEX文件被捆绑成DEX文件(classes.dex)。因此,我并不需要手动初始化时使用DexClassLoader类加载器指定所有第三届库。我是正确与否?

According to the Android build process, dex file along with 3rd party library (such as gson here) are bundled into dex files (classes.dex). Hence, I do not need to manual specify all 3rd library when initialize the class loader using DexClassLoader. Am I correct or not?

推荐答案

有关这个问题的答案是,DEX发现类加载器和加载类2个版本的GSON。
解决的办法是:只有在任一类加载器加载或使用类GSON

The answer for this problem is that DEX find 2 versions of gson in class loader and loaded class. The solution is: only use gson in either class loader or loaded class.

这篇关于通过类DEX意外解决;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆