Android Gradle找不到符号类Gson



所以我在libs-dir中添加了gson-2.2.4.jar(我使用的是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目录中,它应该自动处理所有事情。

我也遇到了同样的问题。我只是在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. 确保您已连接到互联网;这样jar就可以从在线的maven中央存储库中下载。

在项目结构设置中将其作为依赖项添加是不够的。该设置仅适用于IDE。要真正构建,Gradle也需要意识到这一点。你必须像这样将.jar文件添加到你的build.Gradle文件中…

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

只需添加一个点,

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

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

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

我也遇到过同样的问题。

要解决它,请确保您为安卓插件指定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') 
}

在我的例子中,我刚刚添加了这一行:

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

在我的应用程序build.gradle文件上。

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

请检查此存储库,以确保您使用的是最新的可用版本。

试试这个GSON。将此添加到build.gradle(模块:应用程序)上

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

将其添加到build.gradle(模块:应用程序)上

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

只是为了更新引用(我正在搜索):

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

您可以在github存储库上看到最后一个版本

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

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

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

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

我已经解决了这个问题,通过使targetSdkVersion对所有库模块和应用程序级模块都相同。

最新更新