本文介绍了DexClassLoader在Android蜂窝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图通过加载外部库项目DexClassLoader。这2.3工作pretty的还有:

I'm working on a project that tries to load an external library viaDexClassLoader. This worked pretty well in 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引发例外:

Unfortunately, when trying to port this app to Honeycomb (because theactual target for this app are tablets) the DexClassLoader throws anexception:

02-23 09:30:58.221: ERROR/dalvikvm(8022): Can't open dex cache '/data/
dalvik-cache/
data@[email protected]@files@[email protected]':
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@[email protected]@files@[email protected])
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()在我的例子是/数据/数据​​/at.mSystem.client /文件。

It seems like the DexClassLoader ignores the 2th parameter(dexOutputDir), as the value ofcontext.getFilesDir().getAbsolutePath() in my example is "/data/data/at.mSystem.client/files".

你有什么想法如何解决呢?或者,这是某种形式的蜂窝错误?

Do you have any ideas how to solve that? Or is this some kind ofhoneycomb bug?

谢谢

罗兰

推荐答案

我知道这是一个古老的职位,但我最近需要一个答案,这一点没有升级到Android 3.1,所以我想我会分享我的解决方案。

I know this is an old post, but I recently needed an answer to this without upgrading to Android 3.1, so I thought I would share my solution.

我用,而不是DexClassLoader的DexFile类,因为它让我通过输出文件,从而绕开问题被忽略的输出目录。

I used the "DexFile" class instead of the "DexClassLoader", since it allowed me to pass the output file, thus getting around the issue with the output directory being ignored.

下面是我的code:

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);
}

希望这可以帮助别人。

Hope this helps someone.

这篇关于DexClassLoader在Android蜂窝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:59