Reputation: 359
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
Upvotes: 1
Views: 3406
Reputation: 45
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.
Upvotes: 2
Reputation: 359
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.
Upvotes: 0