今天开始了一个使用 androidx 库针对 API28 的新项目。在我的项目中尝试使用 Retrofit 或 OkHTTP3 时,gradle sync
运行良好,但尝试在设备上运行时,kotlin 编译器会提示“未解决对 retrofit2/okhttp 的引用”。
构建.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'
}
相关的kotlin代码
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.")
}
}
}
错误信息
任何帮助表示赞赏。
最佳答案
有时 Gradle 可能由于某种原因无法正确同步,包括下载超时或其他可能的奇怪问题,解决方法可能是设置另一个版本(希望是更新的版本),然后重新同步 Gradle
虽然如果问题仍然存在,其他原因可能是 Internet/访问存储库问题,或者 IDE 由于某些原因(如文件权限更改、硬件问题等)无法访问有效捕获
关于android - Unresolved reference 改造2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50709163/