Dan*_*wan 0 android kotlin retrofit android-studio-3.2 androidx
我正在使用Retrofit,Dagger并Glide为我的应用程序.迁移后androidX,kapt与Dagger和Glide注释处理器完美配合.项目构建和同步成功,但是,当我运行应用程序时,我在我的ApiService接口中获得Kotlin编译器异常,我在其中使用Retrofit注释
ApiService.kt
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query
interface ApiService {
@GET("/popular")
fun getPopular(@Query("lang") lang: String = LANGUAGE): Call<...>
...
companion object {
val STATUS_OK = "ok"
var LANGUAGE = "en"
fun create(): ApiService {
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(...)
.build()
return retrofit.create(ApiService::class.java);
}
}
Run Code Online (Sandbox Code Playgroud)
的build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 'android-P'
buildToolsVersion '28.0.0-rc2'
defaultConfig {
applicationId "..."
minSdkVersion 17
targetSdkVersion 27
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 'com.google.android:flexbox:0.3.2'
implementation 'com.google.dagger:dagger:2.16'
kapt 'com.google.dagger:dagger-compiler:2.16'
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0-alpha1"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1"
implementation 'com.github.rubensousa:gravitysnaphelper:1.5'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.github.bumptech.glide:glide:4.7.1'
kapt 'com.github.bumptech.glide:compiler:4.7.1'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
implementation 'com.google.android.material:material:1.0.0-alpha1'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation "com.squareup.retrofit2:converter-gson:2.4.0"
implementation 'androidx.cardview:cardview:1.0.0-alpha1'
implementation 'androidx.recyclerview:recyclerview:1.0.0-alpha1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
}
Run Code Online (Sandbox Code Playgroud)
例外:
NonExistentClass无法转换为注释@ error.NonExistentClass()
我试过根据文档使用它
kapt {
correctErrorTypes = true
}
Run Code Online (Sandbox Code Playgroud)
但它导致了另一个例外
java.lang.ClassCastException:com.sun.tools.javac.code.Type $ ErrorType无法强制转换为com.sun.tools.javac.code.Type $ ArrayType
我认为,第一个异常发生是因为在使用kapt插件更新gradle和android studio之后,它突然开始用NonExistentClass声明未知的Retrofit Java注释(没有任何kapt或annotationProcessor用于Retrofit,正如你在build.gradle中看到的那样) .我对第二个错误意味着什么一无所知.