【问题标题】:Kapt NonExistentClass exception in a Retrofit interface after migrating to androidX迁移到 androidX 后,Retrofit 接口中的 Kapt NonExistentClass 异常
【发布时间】:2019-02-28 22:31:35
【问题描述】:

我正在为我的应用程序使用RetrofitDaggerGlide。迁移到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);
        }
    }

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'
}

例外:

NonExistentClass 无法转换为注解@error.NonExistentClass()

我根据docs尝试使用这个

kapt {
    correctErrorTypes = true
}

但它导致了另一个异常

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,正如您在 build 中看到的那样.gradle)。我不知道第二个错误是什么意思。

【问题讨论】:

    标签: android kotlin retrofit android-studio-3.2 androidx


    【解决方案1】:

    我发现在 gradle.properties 中将 android.enableJetifier 设置为 false 可以修复编译错误。我在Issue Tracker 中提交了一个错误。

    【讨论】:

    • 似乎该修复将在 gradle 插件 3.2.0-beta01 中可用,正如他们所说的 here
    猜你喜欢
    • 2020-01-27
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    相关资源
    最近更新 更多