如何在Android Studio中修复“Failed to resolve: com.google.gson:gson:2.8.5”的问题

6

我正在使用Android Studio创建一个安卓应用程序。我想要使用以下依赖项 'com.google.gson:gson:2.8.5'。我将其添加到我的应用程序的build.gradle中,但是遇到了以下错误:

无法解析:com.google.gson:gson:2.8.5

我尝试了以下方法:

  • 降低依赖项版本
  • 降低buildToolsVersion版本
  • 安装SDK中提供的所有Google软件包
  • 在我的build.gradle项目中添加mavenCentral()

我的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
}

我的 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
}

非常感谢您的帮助。
2个回答

10

组ID应该是com.google.code.gson而不是com.google.gson。

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

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


1
当我按照以下顺序列出存储库时,我的程序开始工作: jcenter(); google() 在build.gradle文件的buildscript {repositories {块中。 - Diederik

1

试试这个 -

implementation 'com.android.support:multidex:1.0.1'
implementation 'com.google.code.gson:gson:2.8.0'

在项目 Gradle 中 -

classpath 'com.google.gms:google-services:4.2.0'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接