'Unable to resolve dependency for retrofit2

I try to import a retrofit library to my gradle file but unfortunately, I am facing too many errors.

Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.squareup.retrofit2:retrofit:2.4.0.
Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve com.squareup.retrofit2:retrofit:2.4.0.
Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Could not resolve com.squareup.retrofit2:retrofit:2.4.0.
Unable to resolve dependency for ':app@release/compileClasspath': Could not resolve com.squareup.retrofit2:retrofit:2.4.0.
Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Could not resolve com.squareup.retrofit2:retrofit:2.4.0.

EDIT This is my app build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.practice"
    minSdkVersion 19
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:27.1.1'
implementation 'com.dinuscxj:recyclerrefreshlayout:2.0.5'
implementation 'com.makeramen:roundedimageview:2.3.0'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.aurelhubert:ahbottomnavigation:2.1.0'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.google.firebase:firebase-storage:16.0.1'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.firebaseui:firebase-ui-database:4.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.jakewharton:butterknife:8.8.1'
implementation 'com.jakewharton.timber:timber:4.7.1'
implementation 'com.squareup.picasso:picasso:2.71828'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'id.zelory:compressor:2.1.0'
implementation 'com.vanniktech:emoji-one:0.5.1'
implementation 'net.danlew:android.joda:2.9.9.3'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
}

apply plugin: 'com.google.gms.google-services'

This is my project build.gradle

buildscript {

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

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    classpath 'com.google.gms:google-services:4.0.1'
  }
}

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

task clean(type: Delete) {
   delete rootProject.buildDir
}

I added the latest version of the retrofit2 but still get these errors. I have search for possible solutions but I can't find anything. Can someone help me with this problem?



Solution 1:[1]

Can you add this plugins are your app build.gradle file

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'

And Can u change project build.gradle file like below code in allprojects

 allprojects {
repositories {
    google()
    maven { url "https://jitpack.io" }
    jcenter()
} }

Solution 2:[2]

Try this

  implementation('com.squareup.retrofit2:retrofit:2.1.0') {
    // exclude Retrofit’s OkHttp dependency module and define your own module import
    exclude module: 'okhttp'
}
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.okhttp3:okhttp:3.4.1'

and project build.gradle

allprojects {
repositories {
    jcenter()
    google()
    mavenCentral()
    maven { url 'https://jitpack.io' }
}
}

Solution 3:[3]

Add your build Gradle file

'com.squareup.retrofit2:retrofit:2.4.0'

Solution 4:[4]

Try adding this to the module (i.e. app) level gradle:

repositories {
    maven {
        mavenCentral()
    }
}

Also you can try:

Go to File/Invalidate Cache-Restart

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kishan Viramgama
Solution 2
Solution 3 sasikumar
Solution 4 breakline