今天我开始了一个新项目,目标是使用androidx库来开发API28。在尝试在我的项目中使用Retrofit或OkHTTP3时,
build.gradle
gradle sync
没有问题,但是在尝试在设备上运行时,kotlin编译器会抱怨“无法解决到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'
}
相关的 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.")
}
}
}
欢迎提供任何帮助。
./gradlew --refresh-dependencies
,然后执行Invalidate caches/Restart
吗? - David Rawson"https://gitlab.com/oauth/"
。请告诉我这是否解决了您的问题。同时,您能否在界面中指定终端节点并更新问题? - Keshav Aggarwal