7

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.

Community
  • 1
  • 1
DJC
  • 3,233
  • 3
  • 25
  • 31

4 Answers4

9

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.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • With the Eclipse plugin for Android development any jars included in the project's build path are packaged as part of the APK (as long as you are using the plugin to build the APK). The jars do not have to be within the project, or in a certain directory or anything. – Mark B Mar 16 '10 at 17:40
  • There's been a rash of complaints recently about this, though. Again, I'm not an Eclipse user. I've seen that there is "Add JAR" and "Add External JAR" on the one build path dialog tab -- is there maybe some impact if you choose "Add External JAR"? – CommonsWare Mar 16 '10 at 17:46
  • I have been using "Add External JAR", yes. I created a libs directory and moved the gdata installs directly into it. Then I used "Add JAR" and the jars show up as "Referenced Libraries" and the compiler is happy. Then I did a Project Clean, restarted Eclipse and the emulator, and ran. Same problem. Grrr. This thing is kickin me .. :) Looks like a dev environment bug? – DJC Mar 16 '10 at 20:06
  • Found on code.google.com gdata-java-client thread: vbarathan 2/10/09 This client library is not intended for mobile platforms primarily for the memory footprint related to parsing large xml responses... there may be other opensource efforts in the near future to come with a mobile compatible library. Similar comments add evidence that the Google API's other than maps are not available at all on Android. That said, a 2.0 beta was posted 4 days ago with this comment: "Much better experience on Android: much smaller library, and much more efficient" ... – DJC Mar 17 '10 at 06:05
  • Following up... Rewritten from scratch, the version 2 client library is an independent incompatible client library from version 1. Version 2 supports most (but not yet all) major features of version 1, plus: Support for Android (minimum 1.5 SDK), featuring a much smaller library... – DJC Jul 17 '10 at 17:20
2

For those having problem I was having the same error with my app. what I did to solve that was create a new project and copy my resource and source folders along with my manifest file into the new project (I deleted in advance those within the new project created) and voila.

Taryn
  • 224,125
  • 52
  • 341
  • 389
1

When I got this, the problem was actually deeper in the queue; Dalvik converter had failed to convert some of the referenced libraries and still Eclipse allowed me to launch the project.

Check the Android SDK console to see if there are any errors reported.

Zds
  • 4,151
  • 2
  • 22
  • 28
1

In my case, I'm using my own library (MyLib) shared between 2 apps. App A was closed when I added a new class to the library.

When I opened App A to work on it, Eclipse recognised the new class, and I was able to reference it. However on running I got the error.

It turned out that the imported library folder in App A (named something like MyLib_src) didn't reflect the changes made to my library project (MyLib).

  • To solve this I refreshed App A, the changes reflected, and Android could build my project correctly.

I have found no reference to this version of the problem, so thought I would add it to this list.

Richard Le Mesurier
  • 27,993
  • 19
  • 127
  • 242