androidkotlinretrofitandroid-studio-3.2androidx

Kapt NonExistentClass exception in a Retrofit interface after migrating to androidX


I am using Retrofit, Dagger and Glide for my app. After migrating to androidX, kapt works perfectly with Dagger and Glide annotation processors. The projects build and syncs successfully, however, when I run the app, I get Kotlin compiler exceptions in my ApiService interface, where I use Retrofit annotations

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

The exception:

NonExistentClass cannot be converted to Annotation @error.NonExistentClass()

I tried using this according to docs

kapt {
    correctErrorTypes = true
}

But it led to another exception

java.lang.ClassCastException: com.sun.tools.javac.code.Type$ErrorType cannot be cast to com.sun.tools.javac.code.Type$ArrayType

As I think, the first exception occurs because after updating gradle and android studio with kapt plugin, it suddenly started declaring unknown Retrofit Java annotations with NonExistentClass (there isn't any kapt or annotationProcessor for Retrofit, as you can see in build.gradle). I'm clueless as to what does the second error mean.


Solution

  • I found that setting android.enableJetifier to false in gradle.properties fixes the compilation error. I've filed a bug in the Issue Tracker.