Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException when including a library jar

In my android project I added a library jar into the libs folder.
This jar contains three different packages:

  1. google GSON (com.google.gson)
  2. jboss netty (org.jboss.netty)
  3. A client application I developed myself which uses these two other libs. (com.example.core)

The jar is compiled/archived using ant.

There are no errors in eclipse, but when I try to run my android app on a device I get this:

E/AndroidRuntime(23807): FATAL EXCEPTION: main
E/AndroidRuntime(23807): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.project/com.example.project.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.project.MainActivity" on path: /data/app/com.example.project-1.apk
E/AndroidRuntime(23807):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
E/AndroidRuntime(23807):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(23807):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(23807):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(23807):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(23807):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(23807):    at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(23807):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(23807):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(23807):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(23807):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(23807):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(23807): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.project.MainActivity" on path: /data/app/com.example.project-1.apk
E/AndroidRuntime(23807):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
E/AndroidRuntime(23807):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(23807):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(23807):    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
E/AndroidRuntime(23807):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
E/AndroidRuntime(23807):    ... 11 more

And it's definitely my jar that is causing this, since if I remove references to it's classes from within the MainActivity then the problem is solved and the app runs on the device.

Using apktool I checked to see what goes into the apk, and I found that everything is being added to the smali directory, that is everything except for my package (com.example.core).
So all of the android app classes (com.example.project), the gson (com.google.gson) and netty (org.jboss.netty) from my jar are there but not com.example.core.

My environment:
Eclipse 4.2.1
ADT 21.0.1
Java 7 (oracle)

Devices I tested on:
Samsung galaxy S2
LG nexus 4

Any ideas? Thanks.

like image 209
Nitzan Tomer Avatar asked Jan 31 '13 12:01

Nitzan Tomer


2 Answers

You're getting a NoClassDefFoundError because your jar file is not available at runtime.

In order for it to be available at runtime you'll have to check the checkboxes on your jar file in your java build path like this:

enter image description here

like image 50
Ahmad Avatar answered Oct 21 '22 22:10

Ahmad


Ok, I finally managed to find the problem, it took me a while.

I am using java 7 on my machine and the ant builder uses this default version as well, unfortunately though, android does not like java 7, and so this fixed the problem for me:

<javac srcdir="." destdir="bin/classes" source="1.6" target="1.6">
    ...
</java>

Or you can use these steps in Eclipse:

Eclipse menu: Window/Preferences/Java/Compiler
Compiler Compliance level: 1.6 
tick "use default" 

Hope that this will save some time for others who are facing this problem.

like image 37
Nitzan Tomer Avatar answered Oct 21 '22 22:10

Nitzan Tomer