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?

9
  • do you have it in your class path? Jul 18, 2013 at 13:23
  • and of course did you clean and run the project. Jul 18, 2013 at 13:24
  • 1
    you've verified that the gson-2.2.4.jar JAR file is in /WEB-INF/lib? Jul 18, 2013 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, 2013 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, 2013 at 13:31

3 Answers 3

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'
}
2
  • 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 5, 2013 at 15:43
  • 2
    I had the same issue. Under the "build" menu you can also run "clean project", which does the same thing. Nov 26, 2013 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
3
  • GOD! finally!! finally i added a jar file to my android studio. thanks Dec 9, 2013 at 18:21
  • 1
    Rebuild project did it for me...so odd.
    – SoWeLie
    Jan 15, 2014 at 21:30
  • 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... May 19, 2016 at 8:11
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.