52

So I added gson-2.2.4.jar to the libs dir (I'm using android studio). My project couldn't find the Gson stuff so I added it as a library dependency to my module in the "Project Structure". When I try to run the project, the build is failing with the following errors:

Error:(12, 23) Gradle: package com.google.gson does not exist
Error:(37, 3) Gradle: cannot find symbol class Gson
Error:(37, 19) Gradle: cannot find symbol class Gson

Why can't I get this working? I read elsewhere that Gradle is supposed to handle everything automatically if it's put in the lib dir.

Commander Tvis
  • 2,244
  • 2
  • 15
  • 41
jensengar
  • 6,117
  • 17
  • 58
  • 90
  • I'm not familiar with how Android Studio works, but what does your `build.gradle` file look like? – superEb Jul 28 '13 at 22:51

11 Answers11

97

I faced the same issue. I just added a single line as shown below in my build.gradle dependencies (without adding any jar in project structure) and it worked for me.

dependencies {
    compile 'com.google.code.gson:gson:2.2.+'
    compile 'com.android.support:support-v4:13.0.+'
    compile 'com.android.support:appcompat-v7:18.0.+'
}

Along with above, I found few more things which are required for this to work.

  1. Make sure you have android:targetSdkVersion="18" in AndroidManifest.xml file.

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="18" />
    
  2. Make sure you have targetSdkVersion 18 in build.gradle file.

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 18
    }
    
  3. Make sure you are connected to internet; so that jars will be downloaded from online central maven repository.

Floern
  • 33,559
  • 24
  • 104
  • 119
Pioneer
  • 1,530
  • 1
  • 11
  • 11
  • 2
    #Shai Surve - Is this still working for you? I'm having to include gson in libs and referencing it with "compile files". – AndroidGuy Aug 12 '13 at 18:31
  • Yes it works for me. If it is not working when you are not connected to internet. Then try the same while being connected to internet. Because it fetches the jar from central maven repository which build process will download from the online central maven repository. – Pioneer Aug 27 '13 at 16:01
  • great! i didn't know there was an artifact for gson on google code. how do you find out what else is available to reference as a dependency in gradle. the remote libraries have to have a build.gradle file right? – speedynomads Sep 26 '13 at 15:42
  • Awesome! I tried the "compile files" option and couldn't get it to work. Using gradle/maven as specified above worked beautifully. – Justin Apr 11 '14 at 07:46
  • 1
    @ShaiSurve Specifying `uses-sdk` in the Manifest is unnecessary - it gets overwritten by whatever is in `build.gradle` – Melllvar Jan 31 '15 at 08:13
  • 1
    Thanks Melllvar, you are right. With the new versions of android studio and gradle Manifest uses-sdk is unnecessary. – Pioneer Mar 12 '15 at 01:15
43

Adding it as a dependency in the Project Structure settings is not enough. That setting is only for the IDE. To actually build, Gradle also needs to be aware of it. You must add the .jar file to your build.gradle file like so...

dependencies {
    compile files('libs/gson-2.2.4.jar')
}
Alex Fu
  • 5,509
  • 3
  • 31
  • 40
20

Just to add a point,

As of Gradle 1.7, jcenter() is a superset of mavenCentral()...so no need of adding and repositories directive.

Jars will be downloaded from online central jcenter repository. so adding just the following statement is working.

dependencies {
compile 'com.google.code.gson:gson:2.2.+'
}
Nicks
  • 16,030
  • 8
  • 58
  • 65
14

I've faced with same issue.

To solve it be sure that you specify maven central for android plugin

repositories {
    mavenCentral()
}

And it should be added twice if you are defining build script

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5+'
    } 
}


repositories {
    mavenCentral() 
}


apply plugin: 'android' dependencies {    
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.android.support:support-v4:13.0.0'   
    compile project(':libraries:volley') 
}
user438316
  • 249
  • 3
  • 5
6

In my case, I just added this line:

dependencies {

    compile 'com.google.code.gson:gson:2.7'
}

on my app build.gradle file.

By now 2.7 is last current available version according to: https://mvnrepository.com/artifact/com.google.code.gson/gson

Please check this repository to be sure you are using last available version.

amarnath
  • 785
  • 3
  • 19
  • 23
Juan Pablo
  • 1,213
  • 10
  • 15
4

Try this GSON . Add this on build.gradle (Module :app)

implementation 'com.google.code.gson:gson:2.2.4'

rhaldar
  • 1,075
  • 10
  • 6
4

Add this on build.gradle (Module: app)

    dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      implementation 'com.android.support:appcompat-v7:27.1.1'
      implementation 'com.android.support:design:27.1.1'
      implementation 'com.google.code.gson:gson:2.8.0'
    }
Sudheesh R
  • 1,767
  • 3
  • 23
  • 43
Abu Nayem
  • 101
  • 3
  • 11
2

Just to update the reference (I was searching for):

implementation 'com.google.code.gson:gson:2.8.5'

You can see the last version on its github repository

Cícero Moura
  • 2,027
  • 1
  • 24
  • 36
1

Create a folder name libs and add or edit build.gradle (works for any jar lib in folder)

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}
hirigoshy
  • 59
  • 3
1

Addition of following in dependencies could resolve my issues as of year:2021;

 implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
 implementation 'com.google.code.gson:gson:2.2.4'

 
Ank_247shbm
  • 512
  • 7
  • 17
0

I have resolved the issue, by making targetSdkVersion same for all library module with the app-level module.

Muhammad Noman
  • 1,842
  • 19
  • 14