194

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?

casperOne
  • 70,959
  • 17
  • 175
  • 239
Tom R
  • 5,825
  • 9
  • 33
  • 41

27 Answers27

235

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.

John O'Connor
  • 5,104
  • 2
  • 20
  • 29
  • 1
    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. – Hong Mar 31 '12 at 11:32
  • Thanks John! I will add that after changing folder to libs I had to restart eclipse for it to work. – Michaeldcooney Apr 11 '12 at 23:17
  • 5
    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. – Travis Jun 03 '12 at 01:17
  • 1
    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. – Snowwire Jul 05 '12 at 20:54
  • 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 – Jonathan Aug 24 '12 at 15:04
  • 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... – NioShobu Aug 24 '12 at 18:39
  • @jonney http://stackoverflow.com/questions/2680827/conversion-to-dalvik-format-failed-with-error-1-on-external-jar – John O'Connor Aug 28 '12 at 22:23
  • If your JARs are already in "libs" folder then most probably @Anthony Forloney's answer will work for you. – Pranav Mahajan Aug 04 '14 at 05:15
100

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)

Till
  • 3,980
  • 1
  • 26
  • 32
  • 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. – Luis Jun 12 '12 at 19:58
  • 5
    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? – Lou Morda Jul 24 '12 at 16:11
  • I had this problem after updating ADT and this solved it for me. – cdavidyoung Jul 28 '12 at 05:51
  • 5
    It helped me, but only after I did Project -> Clean – altumano Oct 05 '12 at 18:55
  • Thanks!! Saved me. Had to do a clean and restart after installing Eclipse Juno. – Rynardt May 22 '13 at 11:40
  • 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! – Marcel Verwey Jan 03 '15 at 11:35
68

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
Anthony Forloney
  • 84,001
  • 14
  • 111
  • 112
  • 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 Feb 11 '10 at 21:58
  • The error literally just happens here: sna = new ScoreNinjaAdapter(this, SN_APP_ID, SN_APP_KEY); – Tom R Feb 11 '10 at 22:03
  • Do you need an `import` statement for external jar files? I have never used them. – Anthony Forloney Feb 11 '10 at 22:09
  • Have you tried with the removing the .jar file from the `assests` folder approach? – Anthony Forloney Feb 11 '10 at 22:32
  • 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. – Tom R Feb 11 '10 at 22:34
  • @Anthony so I can't add another project to mine? I mean, because I can't add the whole project to the assets folder.. – The Student Jul 16 '10 at 19:13
  • thx for this. placing my jar in assets/libs rather than just libs got the runtime working. – navicore Jun 02 '12 at 17:24
48

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" />
abbood
  • 21,507
  • 9
  • 112
  • 218
brian.clear
  • 5,154
  • 2
  • 39
  • 60
27

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 :)

  • This is also the solution mentioned by the second usercomment here in the bugruport: https://code.google.com/p/android/issues/detail?id=55304 – arberg Jun 04 '13 at 13:15
15

I'm not sure if this is related, or if you're even still looking for an answer, but I came across this thread while trying to research the same error (but possibly for different reasons).

I couldn't find any solutions online, but an answer on a similar thread got me thinking and realized I probably just needed to rebuild (or clean) the project.

In Eclipse, go to Project => Clean. Select your project and Eclipse seemed to fix it itself. For me this solved the problem.

Hope this helps.

Brian
  • 756
  • 6
  • 10
  • 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. – Timmmm Nov 29 '12 at 16:28
13

I have encountered the same issue. The reason was that the library that I was trying to use had been compiled with a standard JDK 7.

I recompiled it with the -source 1.6 -target 1.6 options and it worked fine.

assylias
  • 297,541
  • 71
  • 621
  • 741
  • 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 :( – Hendy Irawan Sep 01 '13 at 13:29
  • 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. – Pedro Náñez Sep 26 '14 at 10:35
7

Same thing worked for me: Properties -> Java Build Path -> "Order and Export" Interestingly - why this is not done automatically? I guess some setting is missing. Also this happened for me after SDK upgrade.

vlad
  • 150
  • 1
  • 5
3

Right click your project folder, look for Properties in Java build path and select the jar files that you see. It has worked for me.

sathish_at_madison
  • 793
  • 10
  • 33
zacharia
  • 1,033
  • 9
  • 22
3

John O'Connor is right with the issue. The problem stays with installing ADT 17 and above. Found this link for fixing the error:

http://android.foxykeep.com/dev/how-to-fix-the-classdefnotfounderror-with-adt-17

Himanshu Jansari
  • 28,446
  • 26
  • 101
  • 128
user1555123
  • 377
  • 3
  • 4
2

I tried everything from this post (and some other posts), and it didn't worked for me, this is by far the most terrible ADT upgrade I have experienced, and I will never upgrade without a working ADT backup.

I managed to solve it by removing the project, and then adding it again using a source backup I had.

Eli
  • 697
  • 8
  • 16
2

I had this same error with ADT22. Resolved it checking "Android Private Libraries" in properties -> Java build path -> Order and export. If you are using any library projects, the same should be done for them as well.

prijupaul
  • 2,046
  • 1
  • 14
  • 17
2

All the existing answers don't work for me because my case is a little bit different. It took me a few hours to get it to work. I'm using Eclipse.

My android project includes another normal java 1.6 project, which needs a 3rd party jar file. The trick is:

  • include the jar in the normal java project for it to compile (only), don't check it in the "Order and Export" tab
  • also copy this jar file into the "libs" folder of the android project so it'll be available in runtime

Hope this help those who have similar scenarios like mine.

Golden Thumb
  • 2,331
  • 19
  • 19
1

sometimes you have to take the whole external project as library and not only the jar:

my problem solved by adding the whole project (in my case google-play-services_lib) as library and not only the jar. the steps to to it (from @style answer):

  1. File->New->Other
  2. Select Android Project
  3. Select "Create Project from existing source"
  4. Click "Browse..." button and navigate to the wanted project
  5. Finish (Now action bar project in your workspace)
  6. Right-click on your project -> Properties
  7. In Android->Library section click Add
  8. select recently added project -> Ok
Community
  • 1
  • 1
dvrm
  • 3,781
  • 4
  • 32
  • 44
1

Sometimes it will happen due to not including jar, which you have dependency, with "uses-libary" tag in your AndroidManifest.xml.

Also, make sure it should be inside "application" tag.

Regards,

Ravi

1

I had this problem and it was caused by not "exporting" the library.Issue was just because the .class files for some classes are not available while packaging the APK.Compile time it will work fine with out exporiting

In my case I was using "CusrsorAdapter" class and under "JavaBuildPath->Order and Export" I didn't check the support V4 jar.Once it is selected issue is gone.

To make sure you are getting noClassDefFound error because of above reason, please check your logacat, you will see unknown super classs error at run time.

Sojan P R
  • 456
  • 6
  • 8
1

Acutally you have reached the infamous 64k limit issue, you need to enable multidex.

This is how you do it. https://developer.android.com/tools/building/multidex.html

Ashish Rawat
  • 4,674
  • 1
  • 16
  • 16
0

please make sure your jar file is in the libs directory of your project in you are using newer ADT version with your eclipse.

samir
  • 339
  • 5
  • 19
0

I got the exact same problem ... To fix it, I just removed my Android Private Libs in "build path" and clicked ok ... and when i opened op the "build path" again eclipse had added them by itself again, and then it worked for me ;)...

pkdkk
  • 3,437
  • 7
  • 37
  • 64
0

i spent two days trying to solve this issue after updating ADT. finally i was lucky to get this post here:

https://code.google.com/p/android/issues/detail?id=55304

which led me in the right direction. when you follow the solution - remember to replace the android support library of all your projects to the same version (remove it and re-install it to the projects). i hope this helps - good luck

Ron
  • 1
0

If you prefer to know which files the workaround is related to here's what I found. Simple change the .classpath file to

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="src" path="gen"/>
    <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
    <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
    <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
    <classpathentry kind="output" path="bin/classes"/>
</classpath>

Replace the .classpath file in all library projects and in main android project. The .classpath file is in the root folder of the eclipse project. Of cause don't forget to add your own classpath entries, should you have any (so compare with your current version of .classpath).

I believe this is the same result as going through the eclipse menus as componavt-user explained above (Eclipse / Configure Build Path / Order and Export).

arberg
  • 3,672
  • 4
  • 24
  • 29
0

The solution here worked for me. It's a matter of importing the library to the libs folder, then modifying the build.gradle file and then cleaning with gradlew.

Community
  • 1
  • 1
gabr10
  • 124
  • 2
  • 4
0

If you change your order and export in your project build path, this error will not occur. The other way of achieving it is through .classpath in your project folder.

geekgugi
  • 390
  • 1
  • 6
  • 20
0

Try this:-

Step 1

Add all the libraries to build pat in Eclipse( means make all libraries referenced libraries)

Step 2

Delete R.java file and again build the project. Don't worry, R.java will automatically get recreated.

Chill :)

0

As i understood, your Project shows no errors, because you included the Jar. But the Jar won't be used, when your Project got "exported" to the device. Try this

Project -> Properties

Java Build Path / Order and Export

[✔] Your Jar

0

This happens quite very often to me.

Last time that happened I can remembered was caused by switching the Eclipse ADT (Google special edition) to Android Studio, and switching back. I basically tried all methods that I can found on stackoverflow which didn't work for me.

Eventually, I got the app working again (no more NoCalssDeffoundError) by switching my IDE to original Eclipse (Kepler) with ADT.

tyolab
  • 363
  • 3
  • 11
0

I tried various things and the reason for error in my case was conflict between maps.jar and Google Api in Java Build Path-> Libraries. So, when i removed the maps.jar it worked fine.

Regards,

wahib

Wahib Ul Haq
  • 3,742
  • 3
  • 39
  • 38