Joshua Feltimo
Joshua Feltimo

Reputation: 333

Unresolved reference retrofit2

Started a new project today targeting API28 using androidx libraries. When attempting to use either Retrofit or OkHTTP3 in my project, gradle sync runs fine, but when attempting to run on device, the kotlin compiler complains "Unresolved reference to retrofit2/okhttp".

build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

//apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 'android-P'
    defaultConfig {
        applicationId "com.opticalgenesis.lbp.ktlab"
        minSdkVersion 21
        targetSdkVersion "P"
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
    implementation 'androidx.browser:browser:1.0.0-alpha1'
    implementation 'com.google.android.material:material:1.0.0-alpha1'
    implementation 'androidx.core:core-ktx:1.0.0-alpha1'
    implementation 'com.android.volley:volley:1.1.0'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
}

Relevant kotlin code

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity

import retrofit2.Call
import retrofit2.Callback
import retrofit2.Retrofit
import retrofit2.Response
import retrofit2.converter.gson.GsonConverterFactory

class LoginConfirmActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val i = intent
        val data = i.data

        val u = Uri.parse(data.toString())
        val code = u.getQueryParameter("code")
        val state = u.getQueryParameter("state")

        val generatedState = getSharedPreferences(MainActivity.PREFS_KEY, Context.MODE_PRIVATE).getString(MainActivity.STATE_KEY, "")

        if (state == generatedState) {
            Log.d("OAUTH", code)
            val r = Retrofit.Builder().apply {
                baseUrl("https://gitlab.com/oauth")
                addConverterFactory(GsonConverterFactory.create())
            }.build()
            val authApi = r.create(GitLabAuthApi::class.java)
            val codeResponse = authApi.getAccessToken(
                    Keys.appId,
                    Keys.secret,
                    code,
                    "authorization_code",
                    Keys.redirectUri)
            codeResponse.enqueue(object : Callback<CodeResponse> {
                override fun onResponse(call: Call<CodeResponse>?, response: Response<CodeResponse>) {
                    if (response.isSuccessful) {
                        parseAndStoreCodeResults(response.body()!!)
                    } else {
                        Log.e("AUTH_TAG", "Attempt to get code resulted in error ${response.errorBody()}")
                    }
                }

                override fun onFailure(call: Call<CodeResponse>?, t: Throwable?) {
                    t?.printStackTrace()
                }
            })
        } else {
            Log.e("AUTH_TAG", "States don't match.")
        }
    }
}

Error message errors

Any help is appreciated.

Upvotes: 4

Views: 14190

Answers (4)

Jorge Santos
Jorge Santos

Reputation: 46

I know that it's an old post but, i'm newer to Kotlin and the problem was not solved.

Files -> Invalidate Catches / Restart, worked for me on 2.9.0 retrofit/gson

Upvotes: 0

user3200193
user3200193

Reputation: 77

I don't have enough points to comment or upvote yet but I had the same issue and fixed it with

Files -> invalidate catches/restart.

Into my dependencies of build.gradle.kts I have:

implementation("com.squareup.okhttp3:okhttp:4.9.0")

and then in one of my classes I had a few imports that were not recognize. for instance:

import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response

Upvotes: 2

Poorix
Poorix

Reputation: 126

Sometimes Gradle might not be able to sync properly for some reason, including download timeout or other possible weird stuff, a way around it might be to set another version (hopefully more recent), then resync the Gradle

Although if the problem persists, other causes can be internet/access to repositories issues, or IDE not being able to access valid catches for some reasons (like file permission changes, hardware problems, etc)

  • One thing that has happened many times for me which is not a bad idea to look up, is to check proxy stuff, both from the IDE's settings, and also Properties'/Settings' files (that not sure if it's fixed now or not yet, but from personal experience, sometimes changing IDE's settings alone doesn't remove the settings from some properties files)
  • One in a million chance also this might do something too: Files -> Invalidate Catches / Restart

Upvotes: 2

stefato
stefato

Reputation: 47

Same problem here. The only thing that helped was a gradle build tools downgrade to stable 3.1.3

project: build.gradle

dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }

Upvotes: 0

Related Questions