6

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.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299

4 Answers4

10

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

Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • I don't have the jar in the `Order and Export` tab, only the src and gen folders and the Android Dependencies and Android 4.2. But as I wrote in my question, some content of my jar is being added to the apk, but not all of it. So it's not true that my jar is not available at runtime. – Nitzan Tomer Jan 31 '13 at 12:19
7

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.

RoelF
  • 7,483
  • 5
  • 44
  • 67
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
3

(java build path / order and export) move your lib on top and check in

Anand
  • 2,875
  • 1
  • 18
  • 16
  • I don't have the jar in the Order and Export tab, only the src and gen folders and the Android Dependencies and Android 4.2. – Nitzan Tomer Jan 31 '13 at 12:21
  • (java build path / libraries) click on add jar and add you gson jar file from libs folder – Anand Jan 31 '13 at 12:26
  • From what I've read, in the newer versions of ADT you don't need to do that, in fact you should not do that. It's supposed to be enough to add the needed libraries to the `libs` directory of the project. – Nitzan Tomer Jan 31 '13 at 12:28
  • I haven't read about that. AFAIK you still need to add the jar to your build path. – Ahmad Jan 31 '13 at 13:22
1

For IntelliJ this setup finally made the error go away for me:

enter image description here

enter image description here

JoachimR
  • 5,150
  • 7
  • 45
  • 50