NoClassDefFoundError for code in an Java library on Android

I am experiencing an error quite often among my users. The app crashes during startup. When the MainActivity is supposed to be loaded the VM apparently cannot find the class. I cannot figure out why. The architecture of the app is that there is a common project that both my free and pro version are using. Don't know if it is relevant. See the stack trace below. Any thoughts?

java.lang.NoClassDefFoundError: com.android.common.MainActivity
at com.mycompany.myapp.Splash.onCreate(Splash.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.android.common.MainActivity in loader     dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.android.pro-1.apk]

Edit: Thanks for the comment below, Richard. Now I have changed com.android.Splash to something else. It wasn't the real classname anyway. My bad...!


Solution 1:

I had the same issue, I did the following to fix the problem.

  1. Go to "Properties" of the project.
  2. Select "Java Build Path"
  3. Select "Order and Export" Tab
  4. You should see the selected project's "src" and "gen" paths and dependencies here.
  5. The order how they listed were first "src" and then "gen" path
  6. I switch them, so that "gen" folder is build before the "src"

gen - automated code in project (from dependencies and references)
src - source code in project

There was no need to restart the Eclipse. It just started working.

Honestly I have never tried "Android Tools > Fix Project Properties", sometimes it might be doing the same thing. I do not know, I just did above after seen the error message, thinking something is wrong with the build paths.


Edit


Later on it was not sufficient, I was getting the error again. Then I "checked" all the dependencies listed in that view. Now it works again. So far so good. I will keep this updated if it fails again.

FYI: in my last attempt, I tried "Android Tools > Fix Project Properties", but it didn't work out for me.

Solution 2:

I'm currently using SDK 20.0.3 and none of the previous solutions worked for me.

I was able to get things to work by

  • Right clicking the Android project
  • Then selecting Build Path -> Link Source to bring up the Link Source dialog.
  • Then I specified the 'src' folder of the Java project and gave it a name other than the default of 'src' (e.g. 'src2').
  • You can now right-click -> delete the new 'src2' folder.

This brought this created Java class files that were compiled for the Dalvik VM instead of the Java VM when the Android project was built. This allowed the Java class files in the Android Library jar file to go thru dexing when the Android project .apk was created. Now when the app was run the Java project classes were found instead of a Java.lang.NoClassDefFoundError being thrown.

If you want to use a jar file instead of linking the source, then you will need to create a Library Android project. (An Android Project with 'is library' checked in Properties -> Android.) You will then need to either link the source of the Java Project to the Android Library project as described above or copy the source files from the 'src' folder of the Java Project to the 'src' folder of the Android Library project. Build the Android Library project. Then you will be able copy the Android Project jar file that was created into the 'libs' folder of the Android folder because the class files in it were compiled for the Davlik VM instead of the Java VM when the Android project was built. This allows the Java class files in the Android Library jar file to go thru dexing when the Android project .apk is created. Now when the app is run the Java project classes will be found instead of a Java.lang.NoClassDefFoundError being thrown.