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

    }

残念ながら、このアプリを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)

私の例のcontext.getFilesDir()。getAbsolutePath()の値は「/data/data/at.mSystem.client/files」であるため、DexClassLoaderは2番目のパラメーター(dexOutputDir)を無視しているようです。

それを解決する方法はありますか?それとも、これはある種のハニカムバグですか?

ありがとう、

ローランド

4

2 に答える 2

3

これは古い投稿ですが、最近Android 3.1にアップグレードせずにこれに対する回答が必要だったので、解決策を共有したいと思いました。

「DexClassLoader」の代わりに「DexFile」クラスを使用しました。これにより、出力ファイルを渡すことができ、出力ディレクトリが無視されるという問題を回避できました。

これが私のコードです:

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

これが誰かを助けることを願っています。

于 2012-06-25T09:39:16.807 に答える
2

変更履歴を見ると、これは Android 3.1 で修正されているはずです。

于 2012-03-29T17:11:35.447 に答える