相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am creating an android application using android studio. I want to use the following dependence 'com.google.gson:gson:2.8.5'. I add it into my build.gradle of my app but I encountered the following error : Failed to resolve: com.google.gson:gson:2.8.5

I tried the following : - downgrade the version of the dependence - downgrade the version of the buildToolsVersion - install all Google packages available in the SDK - add mavenCentral() to my build.gradle project

My build.gradle app :

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

My build.gradle project :

// 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

Thank you very much for your help

The groupID should be com.google.code.gson instead of com.google.gson.

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

Change it to the above, resync the project, and the error should go away.

Mine started working when I listed the repositories in the following order: jcenter(); google() in the buildscript { repositories { block of the bulid.gradle file – Diederik Oct 23, 2020 at 14:38
implementation 'com.android.support:multidex:1.0.1'
implementation 'com.google.code.gson:gson:2.8.0'

And in project gradle -

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章