首页 > 解决方案 > 无法解析符号 gson

问题描述

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

“无法解析符号 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,但这无济于事。

标签: javaandroidgradlegson

解决方案


compile已过时。请用implementation

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

您在项目级别build.gradle中需要这些

buildscript {

repositories {
    google()
    jcenter()
}

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

推荐阅读