java - 无法解析符号 gson

标签 java android gradle gson

我在使用 gson 库时遇到了很大的麻烦。构建项目时我得到:

"Cannot resolve symbol gson"

我已经用谷歌搜索了几个小时,但似乎没有解决方案有效。这是我的 build.gradle 文件:

android {
    compileSdkVersion 24
    buildToolsVersion "28.0.2"
    defaultConfig {
        applicationId "com.example.jasminkrhan.vaktija"
        minSdkVersion 24
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.8.2'

}

还有我的项目的 build.gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        jcenter {
            url "http://jcenter.bintray.com/"
        }
    }

    repositories {
        maven  {
            url "http://repo1.maven.org/maven2"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {

    repositories {
        jcenter {
            url "http://jcenter.bintray.com/"
        }
    }

    repositories {
        maven  {
            url "http://repo1.maven.org/maven2"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

有人发现我的代码有问题吗?为什么android studio找不到这个库?我已经尝试过重新启动 Android Studio,但这没有帮助。

最佳答案

compile is outdated. Please use implementation

实现“com.google.code.gson:gson:2.8.5”

You need these in Project Level build.gradle

buildscript {

repositories {
    google()
    jcenter()
}

.....
allprojects {
    repositories {
        google()
        jcenter()
    }
}

关于java - 无法解析符号 gson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52130361/

相关文章:

Java:从子类调用 super.getClass()

android - GSM RSSI 和 LTE RSSI 和 RSRP

android - 如何在jquerymobile中改变collapsible set collapsed object的主题

android - 如何从 Gradle 中的另一个函数调用 buildConfigField()?

android - 从库模块引用 flatDir

java - 在 ImageView FloodFill 算法中获取触摸坐标不准确

java - Word、docx4j 和内容控制文本格式

java - 解决int + string的问题

android - 无法从内部存储读取文件(文件不存在问题)

android - Google Map V2 和 GCM 在一个项目中