Android Gradle cannot find symbol class Gson

那年仲夏 提交于 2019-11-28 06:42:00

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')
}
Sahil

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.

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.+'
}

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') 
}
Juan Pablo

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.

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

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

Abu Nayem

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'
    }

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')
}

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 project:

enter link description here

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!