Android Honeycomb上的DexClassLoader

时间:2011-02-24 12:44:06

标签: android android-3.0-honeycomb

我正在开发一个试图通过加载外部库的项目 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);
                    }
            }

    }

不幸的是,当试图将此应用程序移植到Honeycomb时(因为 这个应用程序的实际目标是平板电脑)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忽略了第2个参数 (dexOutputDir),作为值 我的例子中的context.getFilesDir()。getAbsolutePath()是“/ data / data /” at.mSystem.client /文件”。

您有什么想法如何解决这个问题?或者这是某种 蜂窝虫?

谢谢,

罗兰

2 个答案:

答案 0 :(得分: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);
}

希望这有助于某人。

答案 1 :(得分:2)

查看更改历史记录,这应该在Android 3.1中修复。