NoClassDefFoundError for code in an Java library on Android NoClassDefFoundError for code in an Java library on Android android android

NoClassDefFoundError for code in an Java library on Android


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.


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.


Try going to Project -> Properties -> Java Build Path -> Order & Export and ensure Android Private Libraries are checked for your project and for all other library projects you are using. i got the solution by following below link NoClassDefFoundError Android Project?