首页 文章

Android Gradle找不到符号类Gson

提问于
浏览
49

所以我将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目录中 .

9 回答

  • 6

    只是为了增加一点,

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

    Jars将从在线中央jcenter存储库下载 . 所以只添加以下语句是有效的 .

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

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

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

    我遇到了同样的问题 . 我刚刚在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.+'
    }
    

    除此之外,我还发现了一些工作所需的东西 .

    • 确保AndroidManifest.xml文件中有 android:targetSdkVersion="18" .
    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="18" />
    
    • 确保build.gradle文件中包含 targetSdkVersion 18 .
    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 18
    }
    
    • 确保您已连接到互联网;以便从在线中央maven存储库下载jar .
  • 1

    我遇到过同样的问题 .

    要解决它,请确保为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') 
    }
    
  • 14

    就我而言,我刚添加了这一行:

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

    在我的app build.gradle文件中 .

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

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

  • 94

    试试这个GSON . 在build.gradle上添加它(模块:app)

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

  • 3

    在build.gradle上添加它(模块: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'
        }
    
  • 3

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

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

    我已经通过使用app-level模块为所有库模块使targetSdkVersion相同来解决了这个问题 .

相关问题