如何修复 Android Studio 中的“无法解析:com.google.gson:gson:2.8.5”

Inf*_*ner 4 java android gson android-studio

我正在使用 android studio 创建一个 android 应用程序。我想使用以下依赖项“com.google.gson:gson:2.8.5”。我将它添加到我的应用程序的 build.gradle 中,但遇到以下错误:无法解决:com.google.gson:gson:2.8.5

我尝试了以下操作: - 降级依赖项的版本 - 降级 buildToolsVersion 的版本 - 安装 SDK 中可用的所有 Google 包 - 将 mavenCentral() 添加到我的 build.gradle 项目

我的 build.gradle 应用程序:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.topquiz"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'org.jetbrains:annotations-java5:15.0'
    implementation 'com.google.gson:gson:2.8.5' //Store object into SharedPreferences
}
Run Code Online (Sandbox Code Playgroud)

我的 build.gradle 项目:

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

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助

小智 6

groupID 应该是 com.google.code.gson 而不是 com.google.gson。

implementation 'com.google.code.gson:gson:2.8.5'
Run Code Online (Sandbox Code Playgroud)

将其更改为上述内容,重新同步项目,错误应该会消失。