Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Gradle cannot find symbol class Gson

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.

like image 931
jensengar Avatar asked Jul 28 '13 22:07

jensengar


3 Answers

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.

like image 60
Pioneer Avatar answered Nov 17 '22 15:11

Pioneer


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')
}
like image 27
Alex Fu Avatar answered Nov 17 '22 15:11

Alex Fu


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.+'
}
like image 20
Nicks Avatar answered Nov 17 '22 13:11

Nicks