Noclassdeffounderror For Code in an Java Library on 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.

NoClassDefFoundError on external library project for Android

It has been clearly stated in offcial API here:

A library project can include a JAR library

You can develop a library project that itself includes a JAR library, however you need to manually edit the dependent application project's build path and add a path to the JAR file

The jar lib must be manually added to the dependent application project's build path, not only the library project build path itself.

Update from SDK r17:

This is automatically handled by ADT now, check out new feature for ADT 17.0.0 release here:

Added feature to automatically setup JAR dependencies. Any .jar files in the /libs folder are added to the build configuration (similar to how the Ant build system works). Also, .jar files needed by library projects are also automatically added to projects that depend on those library projects. (more info)

android studio: NoClassDefFoundError exception on runtime with Java Library Module

This issue is resolved in Android Studio 0.4.4 :D (http://tools.android.com/recent/androidstudio044released)

NoClassDefFoundError for .jar library at runtime in Android Studio

New Findings

My GS4 (Android 4.3) cannot run this app, but HTC One M8 (Android 5.0.1) can run it

Root Cause

65K methods limit
Refer to this document: https://developer.android.com/tools/building/multidex.html
Basically there is a methods reference limit and the only solution is to use multidex configuration. However there are implementation for platforms prior 5.0 and after 5.0

Solution

The solution is described in both:
https://developer.android.com/tools/building/multidex.html
https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html
For my project, since it supports older version of Android, I go with the "multidex support library" solution.

  1. Set the build tool version of all library modules and app module to the latest 21.1.2
  2. For module that has Application class, add compile 'com.android.support:multidex:1.0.0' as dependency
  3. Refer to this link to modify your application class. https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html
  4. At the same gradle script, under android -> defaultConfig block, add "multiDexEnabled true"
  5. If your system gives "Java Heap Size" error, in the app module's gradle script, add the following