android studio java.lang.NoClassDefFoundError Gson
Asked Answered
A

3

15

I've made pretty much a fresh project. I added in the gson library by google to my projects /libs directory and added it as a library. I also added it into my build.gradle as

compile files('libs/gson-2.2.4.jar')

So now everything looks ok, there aren't any errors given by Android Studio, no red underlines anywhere. I can build it and give it a shot.

But then I run into this error:

java.lang.NoClassDefFoundError: com.google.gson.Gson

Which I can see in the debugger. The thing is i've added it in and android studio can see that but come build it gives me all these sorts of problems.

What am I doing wrong?

Audiogenic answered 18/7, 2013 at 13:20 Comment(9)
do you have it in your class path?Beauchamp
and of course did you clean and run the project.Beauchamp
I tried everything I could! I updated android studio, restarted it a couple of times, removed the libraries and added them back but I get the same thing back. I'm not too sure where the clean button is on the Android studio but I 'synced' the gradle files too. Where can I check if its in my class path? I tried to right click on my project and look for properties but this option isn't available on the android studioAudiogenic
you've verified that the gson-2.2.4.jar JAR file is in /WEB-INF/lib?Beauchamp
I don't have a WEB-INF directory. I think it used to be needed on Eclipse but is it also needed on the new android studio?Audiogenic
if this doesn't work try gson-2.2.3.jar for giggles, see if it's a version issue.Beauchamp
I think I got it working. I'm not too sure why it works but I used compile fileTree(dir: 'libs', include: '*.jar') in my build.grade as opposed to compile files('libs/gson-2.2.4.jar')Audiogenic
It may need that and META-INF. I've used Studio only marginally. But in Eclipse it's a must for JSONBeauchamp
ahhhh. that essentially recursively adds all .jars. makes sense.Beauchamp
I
18

Had the same issue. What I did was gradle clean and then build my project with gradle from console. In my build.gradle gson dependency looks like this:

dependencies {
    compile 'com.google.code.gson:gson:2.2.4'
}
Insociable answered 13/9, 2013 at 12:54 Comment(2)
Mac and Android Studio users : go to your project cd ~/AndroidStudioProjects/MyApplicationProject/MyApplication and do ../gradlew clean (make sure gradlew is executable)Cytosine
I had the same issue. Under the "build" menu you can also run "clean project", which does the same thing.Shogunate
P
12

Something went wrong in incremental build system. One of this should help:

  1. Menu Build -> Rebuild project
  2. Delete folder /build
  3. Close Android Studio, delete /build folder
  4. Right click on your project -> "Open module settings" -> Dependencies tab -> check if Export is checked for your library
Perni answered 27/11, 2013 at 23:26 Comment(3)
GOD! finally!! finally i added a jar file to my android studio. thanksCurrajong
Rebuild project did it for me...so odd.Ginaginder
Found the weirdest fix, after trying everything here without success :o. Comment crash-responsible line, build (didn't crash anymore), un-comment line, build again, fixed...Pedaias
G
0

I'm using Android Studio. and in my case, gradle clean and/or Rebuild project didn't work. I had to uninstall the current SDK (in my case: 22) that was supposed to be corrupted and then reinstall it. After doing this, the project restarted to compile and run. Hope it can help someone.

Germain answered 19/1, 2016 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.