ClassNotFoundException when using JAR within an Android Project

I stumbled upon this problem, and took a while to get around it…

The problem is related to embedding JAR libraries in an Android application. I had this code which worked fine before, and suddenly it did not anymore.

Everyone is talking about something similar, but usually all messages relate to the changes from “lib” to “libs”, which is the major cause of headaches.

In my case, however, that was not it… The error message is something like:

E/AndroidRuntime(  893): java.lang.RuntimeException: Unable to instantiate application com.android.test.TestApplication: java.lang.ClassNotFoundException: com.android.test.TestApplication in loader dalvik.system.PathClassLoader[/data/app/com.android.test-1.apk]
E/AndroidRuntime(  893):         at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:649)
E/AndroidRuntime(  893):         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4232)
E/AndroidRuntime(  893):         at android.app.ActivityThread.access$3000(ActivityThread.java:125)
E/AndroidRuntime(  893):         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
E/AndroidRuntime(  893):         at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  893):         at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  893):         at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  893):         at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  893):         at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  893):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  893):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  893):         at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  893): Caused by: java.lang.ClassNotFoundException: com.android.test.TestApplication in loader dalvik.system.PathClassLoader[/data/app/com.android.test-1.apk]
E/AndroidRuntime(  893):         at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
E/AndroidRuntime(  893):         at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
E/AndroidRuntime(  893):         at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
E/AndroidRuntime(  893):         at android.app.Instrumentation.newApplication(Instrumentation.java:942)
E/AndroidRuntime(  893):         at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:644)
E/AndroidRuntime(  893):         ... 11 more

It tried everything. Moving the JAR files around, renaming them, renaming the whole project packages, and nothing. Tried moving the JAR dependencies order, etc… Digging and digging into this, I found that the JAR code was simply not being added to the final APK when compiled.

So I found one lost soul here, who gave me a hint of my issue: the recent Ubuntu comes with Java 1.7 by default (or did I upgrade it myself to run Eclipse 4?), and the Android must run on 1.6 environment.

Recompiling the JAR libraries using Java 1.6 did solve my problem! (for help on 1.6 usage in Ubuntu, check this, but I would not change the default, instead just call it from its full path).

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>