Approach for fixing NoClassDefFoundError?

DJC picture DJC · Mar 16, 2010 · Viewed 11.7k times · Source

Possible Duplicate:
NoClassDefFoundError - Eclipse and Android

I'm seeing this question is getting asked a lot in many different contexts. Perhaps we can set some strategies for locating and fixing it? I'm noobish myself so all I can contribute are horror stories and questions, sorry...

It seems this is thrown when a class is visible at compile time but not at run time... how can this happen?

In my case I am developing an app that uses the Google APIs, in Eclipse, for the Android platform. I've configured the Project Properties / Java Build Path / Libraries to include the gdata .jars and all is well. When I execute in the emulator I get a force close and the logcat shows a NoClassDefFoundError on a simple new ContactsService("myApp"); I've also tried a new CalendarService("myApp") with the same results.

Is it possible or desirable to statically bind at compile time to avoid the problem?

How could dynamic binding of an add-on library work in the mobile environment anyway? Either it has to be bound into my .apk or else I need to "install" it? ... hmmm.

Advice much appreciated.

Answer

CommonsWare picture CommonsWare · Mar 16, 2010

It seems this is thrown when a class is visible at compile time but not at run time... how can this happen?

The build classpath may include JARs that are not being packaged into the APK.

Is it possible or desirable to statically bind at compile time to avoid the problem?

It is possible, desirable, and necessary.

Outside of Eclipse, you just put the JARs you need in libs/ in your project, compile with Ant, and you are done.

Inside of Eclipse, one pattern I have had students use with success is to put the JARs you need in libs/ in your project, add them as JARs to the build path (note: not external JARs), and they get packaged as part of the APK. Note, though, that I do not personally use Eclipse, and so my experience with it is limited.