Android Honeycomb上的DexClassLoader

9

我正在开发一个项目,尝试通过DexClassLoader加载外部库。在2.3版本中,这个操作非常顺利:

    public class FormularDisplayLoader {
public final static String PATH ="/data/data/at.mSystem.client/files/mSystem_Client_FormularLibrary.jar";
        private DexClassLoader classLoader;

            public FormularDisplayLoader(Context context){
                    this.context = context;
                    this.classLoader = new DexClassLoader("/data/data/at.mSystem.client/
    files/mSystem_Client_FormularLibrary.jar",
                        context.getFilesDir().getAbsolutePath(),
                        null,
                        FormularDisplayLoader.class.getClassLoader());
            }

            public View getDisplay(String className) throws ErrorCodeException{
                    try {
                            Class c = classLoader.loadClass(className);
                            Method m = c.getMethod("getDisplay", Context.class);
                            View ret = (View) m.invoke(c.newInstance(), context);
                            return ret;
                    } catch (Exception e) {
                            e.printStackTrace();
                            throw new
    ErrorCodeException(FormularErrorCode.NO_DISPLAY_AVAILABLE_FOR_FORMULAR);
                    }
            }

    }

很遗憾,在尝试将此应用程序移植到蜂巢版(因为该应用程序的实际目标是平板电脑)时,DexClassLoader抛出了异常:

02-23 09:30:58.221: ERROR/dalvikvm(8022): Can't open dex cache '/data/
dalvik-cache/
data@d...@at.mSystem.client@files@mSystem_Client_FormularLibrary....@classes.dex':
No such file or directory
02-23 09:30:58.221: INFO/dalvikvm(8022): Unable to open or create
cache for /data/data/at.mSystem.client/files/
mSystem_Client_FormularLibrary.jar (/data/dalvik-cache/
data@d...@at.mSystem.client@files@mSystem_Client_FormularLibrary....@classes.dex)
02-23 09:30:58.231: WARN/System.err(8022):
java.lang.ClassNotFoundException:
at.mSystem.client.formular.contract.ContractListFormularDisplay in
loader dalvik.system.DexClassLoader@40630308
02-23 09:30:58.241: WARN/System.err(8022):     at
dalvik.system.DexClassLoader.findClass(DexClassLoader.java:240)
02-23 09:30:58.241: WARN/System.err(8022):     at
java.lang.ClassLoader.loadClass(ClassLoader.java:548)
02-23 09:30:58.261: WARN/System.err(8022):     at
java.lang.ClassLoader.loadClass(ClassLoader.java:508)
02-23 09:30:58.261: WARN/System.err(8022):     at
at.mSystem.client.system.formularmodule.formular.FormularDisplayLoader.getDisplay(FormularDisplayLoader.java:
35)

似乎DexClassLoader忽略了第二个参数(dexOutputDir),因为在我的示例中,context.getFilesDir().getAbsolutePath()的值为“/data/data/at.mSystem.client/files”。
你有任何解决方法吗?或者这是一种蜂巢错误吗?
谢谢,
Roland

我没有答案,但是想让你知道我也遇到了同样的问题。 - gotosleep
我在Android问题跟踪器上开了一个工单:http://code.google.com/p/android/issues/detail?id=15893 - gotosleep
在您提交后的同一天,有谷歌的一位工作人员表示:“是的,这是内部错误3439372。已安排在即将推出的蜂巢维护版本中修复。” - James Moore
2个回答

3

我知道这是一篇旧文章,但我最近需要一个解决方案,而不升级到Android 3.1版本,所以我想分享我的解决方案。

我使用了“DexFile”类代替“DexClassLoader”,因为它允许我传递输出文件,从而绕过忽略输出目录的问题。

以下是我的代码:

final File dexClasses = new File("/sdcard/dexcontainer.zip");
DexFile dexFile = DexFile.loadDex(dexClasses.getAbsolutePath(), getFilesDir().getAbsolutePath() + "/outputdexcontainer.dex", 0);

Enumeration<String> classFileNames = dexFile.entries();
while (classFileNames.hasMoreElements())
{
  String className = classFileNames.nextElement();
  dexFile.loadClass(className, classLoader);
}

希望这能帮助到某些人。

2

从变更历史来看,这个问题应该在Android 3.1中得到解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接