How to fix "Failed to resolve: com.google.gson:gson:2.8.5" in Android studio

Viewed 11920

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

2 Answers

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.

Try this -

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'
Related