Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DexClassLoader on Android Honeycomb

I'm working on a project that tries to load an external library via DexClassLoader. 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);
                    }
            }

    }

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

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)

It seems like the DexClassLoader ignores the 2th parameter (dexOutputDir), as the value of context.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 of honeycomb bug?

Thanks,

Roland

like image 413
Roland Avatar asked Feb 24 '11 12:02

Roland


2 Answers

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.

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.

Here's my 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.

like image 197
dudette Avatar answered Sep 24 '22 12:09

dudette


Looking at the change history, this should be fixed in Android 3.1.

like image 39
Roman Nurik Avatar answered Sep 23 '22 12:09

Roman Nurik