ClassNotFoundException when turning on the library

In my Android project, I added the library to the libs folder.
This jar contains three different packages:

  • google GSON (com.google.gson)
  • jboss netty (org.jboss.netty)
  • A client application that I developed myself that uses these two other libraries. (Com.example.core)

The box is compiled / archived using ant.

There are no errors in eclipse, but when I try to run the Android application on the device, I get the following:

 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 this is definitely my jar that causes this, because if I remove the links to it from the classes from MainActivity, then the problem will be solved and the application will start on the device.

Using apktool , I checked what happens in apk and I found that everything is added to the smali directory, that is, everything except for my package (com.example.core).
So, all the classes of applications for Android (com.example.project), gson (com.google.gson) and netty (org.jboss.netty) from my bank are, but not com.example.core.

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

Devices I tested for:
Samsung Galaxy S2
LG nexus 4

Any ideas? Thanks.

+6
source share
4 answers

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

I use java 7 on my machine, and the ant constructor uses this version by default, unfortunately, however, Android does not like java 7, and therefore 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" 

We hope that this will save time for those who are faced with this problem.

+7
source

You get a NoClassDefFoundError because your jar file is not available at runtime.

To make it available at runtime, you will need to check the boxes in your jar file in the java build path as follows:

enter image description here

+10
source

(java build / export and export path) move your library on top and check

+3
source

For IntelliJ, this setup finally made a mistake for me:

enter image description here

enter image description here

+1
source

All Articles