NoClassDefFoundError - Eclipse and Android

115,510

Solution 1

I had this problem after updating ADT.

I was storing all of my JAR files in a folder called "lib" and adding the jars to the build path the normal Eclipse way. This worked fine until my update.

After my update, I was getting the NoClassDefFoundError for a class that I could clearly see was included in the jar (checking out the ReferencedLibraries classes).

The solution was to remove my jars from the build path and rename my "lib" folder to "libs". This is an ant convention, and seems to be the way the new ADT finds and includes libraries in an .apk file. Once I did this, everything worked fine.

Solution 2

I didn't have to put the jar-library in assets or lib(s), but only tick the box for this jar in Properties -> Java Build Path -> "Order and Export" (it was listed before, but not selected)

Solution 3

By adding the external jar into your build path just adds the jar to your package, but it will not be available during runtime.

In order for the jar to be available at runtime, you need to:

  • Put the jar under your assets folder
  • Include this copy of the jar in your build path
  • Go to the export tab on the same popup window
  • Check the box against the newly added jar

Solution 4

I had this for MapActivity. Builds in Eclipse gets NoClassDefFound in debugger.

Forgot to add library to manifest, inside <Application>...</Application> element

<uses-library android:name="com.google.android.maps" />

Solution 5

I have changed the order of included projects (Eclipse / Configure Build Path / Order and Export). I have moved my two dependent projects to the top of the "Order and Export" list. It solved the problem "NoClassDefFoundError".

It is strange for me. I didn't heard about the importance of the order of included libraries and projects. Android + Eclipse is fun :)

Share:
115,510

Related videos on Youtube

Tom R
Author by

Tom R

Good times with C, Java, Python on web and embedded devices.

Updated on October 19, 2020

Comments

  • Tom R
    Tom R over 3 years

    I'm having a problem trying to run an Android app which, up until adding a second external library to its build path, was working fine. Since having added the scoreninja jar, I now get a NoClassDefFoundError when I try to run the app.

    Here's the message:

    02-11 21:45:26.154: ERROR/AndroidRuntime(3654): java.lang.NoClassDefFoundError: com.scoreninja.adapter.ScoreNinjaAdapter
    

    As all of the build scripts are generated by the Android tools (?), I'm not sure what else I can do other than cleaning and rebuilding or restarting eclipse (I have already tried all three). Does anyone know how I can amend this?

    • Teja Kantamneni
      Teja Kantamneni about 14 years
      How did you added the jar file? Did u added using build path -> Libraries -> External jars??
    • Tom R
      Tom R about 14 years
      Right-clicking project > Build Path > Add External Archives... as per instructions here: scoreninja.appspot.com
    • Atul Goyal
      Atul Goyal about 12 years
      If you updated ADT recently, then look at John's answer below (and upvote it!) stackoverflow.com/a/9916751/489852
    • Carlos P
      Carlos P almost 12 years
      If you're having this issue, before you jump right into John's answer, @Till 's answer below is solving it for a lot of people too, myself included.
    • Ashish Rawat
      Ashish Rawat over 8 years
      Did you try multidex ?
  • Tom R
    Tom R about 14 years
    ScoreNinjaAdapter is someone else's code, but I would be very surprised if it contained such an error as it is in common usage. The error happens at a point in my own code.
  • Tom R
    Tom R about 14 years
    The error literally just happens here: sna = new ScoreNinjaAdapter(this, SN_APP_ID, SN_APP_KEY);
  • Anthony Forloney
    Anthony Forloney about 14 years
    Do you need an import statement for external jar files? I have never used them.
  • Anthony Forloney
    Anthony Forloney about 14 years
    Have you tried with the removing the .jar file from the assests folder approach?
  • Tom R
    Tom R about 14 years
    Yeah. In the end, I got fed up with all this and just downloaded the source and copied the source files into my project. Not the nicest solution, but it works now. Thanks for all your help.
  • The Student
    The Student almost 14 years
    @Anthony so I can't add another project to mine? I mean, because I can't add the whole project to the assets folder..
  • Hong
    Hong about 12 years
    Thanks a lot! I had neither a lib or a libs folder before yesterday's ADT update, and everything worked fine. After the update, all external jars had this problem. They have been fixed thanks to your answer. I have to combine yours with Brian's to make it work.
  • Michaeldcooney
    Michaeldcooney about 12 years
    Thanks John! I will add that after changing folder to libs I had to restart eclipse for it to work.
  • navicore
    navicore almost 12 years
    thx for this. placing my jar in assets/libs rather than just libs got the runtime working.
  • Travis
    Travis almost 12 years
    I ran into the same problem after updating ADT, but I was keeping my jars in a directory outside my project and including them as a User Library in my build path. The solution was to remove the user library from my build path and instead include jars using "Add External Jars", and then make them exported by checking the checkboxes on the "Order and Export" tab.
  • Luis
    Luis almost 12 years
    Thanks so much! I had been struggling with this error for a long time and all I needed to do was check the darn box for the jar in the "Order and Export" screen. Funny thing is that I only had to do this so that it would work on a Nook Tablet emulator. Did not have a problem on devices or other emulators.
  • Snowwire
    Snowwire almost 12 years
    I was referencing android-support-v13.jar as an external library at C:\android-sdk-windows\extras\android\compatibility\v13. I moved that JAR into the project libs folder and removed the reference from Java Build Path -> Libraries. A clean build and I was going again.
  • Lou Morda
    Lou Morda almost 12 years
    Does anyone know why you have to do both, add the library and also select the checkbox in Order and Export? Is there a reason why I would want to add an external JAR but not include it in the Order and Export?
  • charles young
    charles young almost 12 years
    I had this problem after updating ADT and this solved it for me.
  • Jono
    Jono over 11 years
    Hi. i tried our approach by adding the external jar libs inside /libs and now i recieve this error: Conversion to Dalvik format failed with error 1
  • NioShobu
    NioShobu over 11 years
    Hey, @Travis, I followed your solution, as I was referencing a custom version of the android.jar file (which had an extra class we are planning to use for coverage purposes). However, that custom class is giving me the NoClassDefFoundError, and that solution didn't seem to help. Also, because it is an Android Test project, there doesn't appear to be any lib or libs folders...
  • John O'Connor
    John O'Connor over 11 years
  • altumano
    altumano over 11 years
    It helped me, but only after I did Project -> Clean
  • Timmmm
    Timmmm over 11 years
    Yeah there seems to be a bug in Eclipse/ADT/dex/whatever that basically misses out classes in the build. It can also miss out the Application class which leads to a different error.
  • Rynardt
    Rynardt almost 11 years
    Thanks!! Saved me. Had to do a clean and restart after installing Eclipse Juno.
  • arberg
    arberg almost 11 years
    This is also the solution mentioned by the second usercomment here in the bugruport: code.google.com/p/android/issues/detail?id=55304
  • Hendy Irawan
    Hendy Irawan over 10 years
    THANK YOUUUUUUUUUUUUUUU!!!!!!!!!!!!!!!!!!! I've been frustrated by this problem and you solved it! It's a bit ironic that even the latest Jelly Bean 4.3 SDK (API 18) still cannot read 1.7 JAR/classes. And worst of all it doesn't complain at all :(
  • jmrodrigg
    jmrodrigg over 10 years
    It also worked for me. I included the library but forgot to check it in "Order and export". Thank you!
  • Pranav Mahajan
    Pranav Mahajan almost 10 years
    If your JARs are already in "libs" folder then most probably @Anthony Forloney's answer will work for you.
  • Pedro Náñez
    Pedro Náñez over 9 years
    I was having the same problem. The worst of all is that I thinked in this option and then I changed my -source 1.7 with -source 1.5 (to be sure) and the problem wasn't solved. But, after read your comment, I tried again with 1.6 and then, it worked like a charm.
  • Marcel Verwey
    Marcel Verwey over 9 years
    After checking the box I also had to move the jar file to the top in the "Order and Export" screen. But the strange thing is, after moving it to the bottom again it still works!