Android Gradle找不到符号类Gson

时间:2013-07-28 22:43:04

标签: android gradle android-gradle

所以我将gson-2.2.4.jar添加到libs目录(我正在使用android studio)。我的项目找不到gson的东西,所以我在“项目结构”中将它作为库依赖项添加到我的模块中。当我尝试运行项目时,构建失败并出现以下错误:

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

为什么我不能让这个工作?我在其他地方读到gradle应该自动处理所有内容,如果它放在lib目录中。

11 个答案:

答案 0 :(得分:95)

我遇到了同样的问题。 我刚刚在build.gradle依赖项中添加了一行,如下所示 (在项目结构中没有添加任何jar),它对我有用。

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

除此之外,我发现还需要做更多的工作。

  1. 确保您在AndroidManifest.xml文件中有android:targetSdkVersion="18"

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="18" />
    
  2. 确保build.gradle文件中包含targetSdkVersion 18

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 18
    }
    
  3. 确保您已连接到互联网;以便从在线中央maven存储库下载jar。

答案 1 :(得分:41)

在项目结构设置中将其添加为依赖项是不够的。该设置仅适用于IDE。要实际构建,Gradle还需要了解它。您必须将.jar文件添加到build.gradle文件中,如此...

dependencies {
    compile files('libs/gson-2.2.4.jar')
}

答案 2 :(得分:19)

只是添加一个点,

从Gradle 1.7开始,jcenter()是mavenCentral()的超集......所以不需要添加和存储库指令。

Jars将从在线中央jcenter存储库下载。 所以只添加以下语句就可以了。

dependencies {
compile 'com.google.code.gson:gson:2.2.+'
}

答案 3 :(得分:14)

我遇到了同样的问题。

要解决这个问题,请确保为android插件指定maven central

repositories {
    mavenCentral()
}

如果要定义构建脚本

,则应该添加两次
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') 
}

答案 4 :(得分:6)

在我的情况下,我刚添加了这一行:

dependencies {

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

在我的app build.gradle文件中。

到目前为止2.7是最新的当前可用版本,根据:  https://mvnrepository.com/artifact/com.google.code.gson/gson

请检查此存储库以确保您使用的是上一个可用版本。

答案 5 :(得分:3)

试试这个GSON。在build.gradle(Module:app)

上添加它

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

答案 6 :(得分:3)

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

答案 7 :(得分:1)

创建文件夹名称libs并添加或编辑build.gradle(适用于文件夹中的任何jar lib)

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

答案 8 :(得分:0)

通过使用app-level模块为所有库模块设置targetSdkVersion,我解决了这个问题。

答案 9 :(得分:0)

只需更新参考(我正在搜索):

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

您可以在其github项目上看到最新版本:

enter link description here

答案 10 :(得分:0)

在依赖项中添加以下内容可以解决我截至 2021 年的问题;

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