首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓蜂巢上的DexClassLoader

安卓蜂巢上的DexClassLoader
EN

Stack Overflow用户
提问于 2011-02-24 20:44:06
回答 2查看 3.1K关注 0票数 9

我正在做一个尝试通过DexClassLoader加载外部库的项目。这在2.3中运行得相当好:

代码语言:javascript
复制
    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抛出了一个异常:

代码语言:javascript
复制
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/files“。

你有什么办法解决这个问题吗?还是说这是一种蜂窝虫?

谢谢,

罗兰

EN

回答 2

Stack Overflow用户

发布于 2012-06-25 17:39:17

我知道这是一个老帖子,但我最近需要一个答案,而不是升级到Android 3.1,所以我想我应该分享我的解决方案。

我使用了"DexFile“类而不是"DexClassLoader",因为它允许我传递输出文件,从而解决了输出目录被忽略的问题。

下面是我的代码:

代码语言:javascript
复制
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);
}

希望这对某些人有帮助。

票数 3
EN

Stack Overflow用户

发布于 2012-03-30 01:11:35

查看更改历史,这应该会在Android3.1中得到修复。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5104823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档