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?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Tarang
  • 75,157
  • 39
  • 215
  • 276
  • do you have it in your class path? – apollosoftware.org Jul 18 '13 at 13:23
  • and of course did you clean and run the project. – apollosoftware.org Jul 18 '13 at 13:24
  • 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 studio – Tarang Jul 18 '13 at 13:26
  • 1
    you've verified that the gson-2.2.4.jar JAR file is in /WEB-INF/lib? – apollosoftware.org Jul 18 '13 at 13:27
  • 1
    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? – Tarang Jul 18 '13 at 13:29
  • if this doesn't work try gson-2.2.3.jar for giggles, see if it's a version issue. – apollosoftware.org Jul 18 '13 at 13:29
  • 2
    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')` – Tarang Jul 18 '13 at 13:31
  • It may need that and META-INF. I've used Studio only marginally. But in Eclipse it's a must for JSON – apollosoftware.org Jul 18 '13 at 13:32
  • ahhhh. that essentially recursively adds all .jars. makes sense. – apollosoftware.org Jul 18 '13 at 13:33

3 Answers3

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'
}
JJD
  • 50,076
  • 60
  • 203
  • 339
Nemanja Milkov
  • 256
  • 2
  • 7
  • 1
    Mac and Android Studio users : go to your project `cd ~/AndroidStudioProjects/MyApplicationProject/MyApplication` and do `../gradlew clean` (make sure `gradlew` is executable) – Maxence Nov 05 '13 at 15:43
  • 2
    I had the same issue. Under the "build" menu you can also run "clean project", which does the same thing. – Jared Forsyth Nov 26 '13 at 19:48
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
gingo
  • 3,149
  • 1
  • 23
  • 32
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.

Lisitso
  • 495
  • 10
  • 14