1

I have an Android application that requires the ability to load external Jar files. I know the locations of the Jars.

I have been playing with the PathClassLoader class provided by the Android API to no avail. I am using the following code from within an Activity:

String jarPath = "/sdcard/jars/testjar.jar";

PathClassLoader myClassLoader = new PathClassLoader(jarPath, ClassLoader.getSystemClassLoader());

        try
    {
        Class<?> clazz = Class.forName("com.test.Class1", true, myClassLoader);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }

Upon instantiating the PathClassLoader instance, Dalvik prints a message to LogCat telling me that it could not find the 'classes.dex' file. After that, 'Class.forName' throws a 'ClassNotFoundException'.

I'm trying to find a way to be able to load classes from the external Jar file without the need for the 'classes.dex' file. The Jar file does not contain this file, but when other applications are built with the Jar in the classpath, Dalvik can load the classes quite happily.

Simply put: How can one load classes from an external Jar file without the need for 'classes.dex'?

Many thanks, P

Update The message logged by Dalvik is

Zip is good, but no classes.dex inside, and no valid .odex file in the same directory

2 Answers 2

2

there is a tools called "dx", this tool can transfer Java Binary Code into Android Dalvik Binary code. In Android, Java Binary Code cannot be recognized.

Sign up to request clarification or add additional context in comments.

Comments

0

I discovered the solution. Perhaps I was too hasty when posting the question.

Of course the .dex files are required since that is exactly what Dalvik executes.

To solve my problem I have dex'd the external Jar files so they now include their own 'classes.dex'. Fortunately this is part of our continuous integration environment, so once we have checked out and built the Android libraries, an extra intermediary step through dx.exe generates 'classes.dex' and eventually allows me to load the classes from the Jar at run-time.

1 Comment

Hi @protectedmember, I'm in a similar situation. Could you please tell me the steps to do it. i have actually converted an apk into a jar file using dex2jar. now when i load this class using dexclassloader there is an error like Zip is good, but no classes.dex inside, and no valid .odex file in the same directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.