Kapt NonExistentClass exception in a Retrofit interface after migrating to androidX - android

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.

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

Related

Got this error when tried a code using dagger hilt, Error:[Hilt] java.lang.reflect.InvocationTargetException (no error message)

I tried a code to make a currency converter in native Android using an API. I have used Retrofit, Dagger-Hilt and the MVVM architectural design pattern. After doing all the coding ,this is the error I got. The API fetches live currency exchange rates,which were converted from json to kotlin and used.
Gradle file
This is my gradle file with all the dependencies added.
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.example.currency"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
buildFeatures{
viewBinding true
dataBinding true
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.appcompat:appcompat:1.3.0-beta01'
implementation 'androidx.appcompat:appcompat:1.3.0-beta01'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
//dagger - hilt
implementation "com.google.dagger:hilt-android:2.31-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0-beta01"
//Activity KTX for viewModels()
implementation "androidx.activity:activity-ktx:1.2.1"
//Architectural components
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
//Lifecycle
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.0"
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
//Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
//Coroutines Lifecycle Scopes
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.0"
}
like i mentioned in the comments and my github PR. The solution is to use the same version of hilt-android, compiler
I used hilt version 2.33-beta and same version of hilt-android,compiler,but I have encountered the same problem.Then I change the hilt version to 2.3.5 refering to Google codelab,everything is worked because I find that version is incompatible the kotlin version 1.5 or higher.

kotlin Corountines and live data integrations error in find class kotlin.coroutines.Continuation<? super androidx.lifecycle.LiveData<

I am facing live data and coroutines problems in my project. I am getting following error
kotlin.coroutines.Continuation> p1) {
^
symbol: class BookJson
location: class BookClientHelper
I have added co-routines in my project recently. I am getting above error. I have kapt,ktx and coroutines dependency in my gradle.
My project build gradle looks like this
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.mine.bookkeeper"
minSdkVersion 22
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = [
"room.schemaLocation":"$projectDir/schemas".toString(),
"room.incremental":"true",
"room.expandProjection":"true"]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.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.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
def room_version = "2.2.3"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.room:room-coroutines:2.1.0-alpha04"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
implementation 'com.intuit.sdp:sdp-android:1.0.6'
implementation 'com.github.bumptech.glide:glide:4.11.0'
kapt 'com.github.bumptech.glide:compiler:4.11.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:retrofit:2.7.1'
implementation "com.squareup.retrofit2:converter-gson:2.7.1"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3'
}
Why it is not able to find normal class file ?
is it dependency problem or gradle plugin problem ?
Little bit new to kotlin. A little description will help me to move ahead with these components
Thanks in advance

Getting error on using classes from kotlinx-coroutine-test

It seems like kotlinx-coroutines-test dependency is not working for me as I can't access members of the dependency like TestCoroutineDispatcher, setMain(), resetMain() etc. I was following this doc but can't access the members despite adding the gradle dependency. I tried rebuilding the project and invalidating the cache and restart but nothing seems to work. I have also tried doing androidExtensions {experimental = true} but still no luck.
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.android.kotlincoroutines"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
androidExtensions {
experimental = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
implementation 'com.google.android.material:material:1.1.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// ViewModel and LiveData
def lifecycle_version = '2.0.0-beta01'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-alpha02"
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
// Room for database
def room_version = '2.0.0'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
def work_version = "1.0.0-rc01"
implementation "android.arch.work:work-runtime:$work_version"
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation "com.google.truth:truth:0.42"
androidTestImplementation "androidx.arch.core:core-testing:$lifecycle_version"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.0-M1'
}
Where am I going wrong here?
replacing testImplementation with implementation did the trick for me.
Change your library version to 1.3.2 instead of 1.3.0-M1 like so:
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
And be sure that your tests are unit test in the test folder, not instrumented test in androidTest folder.
just add :-
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0")
Source :- Here yigit have given solution link and ssems like working .
Try adding core as a dependency on your test. It solved the problem for me.
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0")
The solution from #Squti worked for me, namely moving the class trying to import the TestCoroutineDispatcher from a package in the src directory to a package in the test directory.

Unresolved reference ActivityTestRule for AndroidX

I'm trying to test my UI via instrumentation test, with androidX espresso library.
In my grade I have:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
android {
compileSdkVersion 28
defaultConfig {
applicationId "it.zehus.mybike"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
android.defaultConfig.manifestPlaceholders = ['appAuthRedirectScheme': 'net.openid.appauthdemo']
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
def lifecycle_version = "2.0.0"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
// room persistence library
def room_version = "2.1.0-alpha04"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version" // use kapt for Kotlin
// optional - Coroutines support for Room
implementation "androidx.room:room-coroutines:$room_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
// Espresso
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation project(path: ':remotemanager')
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.21"
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
// required if you want to use Mockito for unit tests
testImplementation 'org.mockito:mockito-core:2.24.5'
// required if you want to use Mockito for Android tests
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
// Bluetooth sdk
implementation project(path: ':bluetoothmanager')
// Custom views
implementation project(path: ':customviews')
}
As specified in the documentation I'm attempting to import ActivityTestRule class in my test, however the reference is unresolved.
import androidx.test.filters.LargeTest
import androidx.test.runner.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
// unresolved reference here
import androidx.test.rule.ActivityTestRule
import it.zehus.mybike.ui.ride.RideActivity
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
// deprecated here
#RunWith(AndroidJUnit4::class)
#LargeTest
class FragmentDashboardUiTest {
#get:Rule // unresolved reference here
val activityRule = ActivityTestRule(RideActivity::class.java)
#Test
fun myClassMethod_ReturnsTrue() { }
}
Am I doing something wrong or there's a problem within AndroidX testing libraries?
I found out from this documentation page that class ActivityTestRule stays under androidx.test.rule in AndroidX. In order to import the package, I simply added:
androidTestImplementation 'androidx.test:rules:1.2.0'
to my gradle.
To sum up my gradle now contains:
// Espresso
// Core library
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
The class is under package: androidx.test.ext.junit.rules;
So obviously the most important dependency is:
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
I am wondering why the other answer is uplifted so much,
but it doesn't work for me at all.
Hope this helps others like me.

Cannot find symbol ViewModel with Dagger 2

I'm creating a new Android project and I adding Dagger 2, but it fails with an error related to androidx libraries.
After using a template from within Android Studio, I've added a few lines, kotlin-kapt, and the dependencies for Dagger 2.
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.xxxx.yyyy"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
androidExtensions {
experimental = true
}
kapt {
generateStubs = true
}
}
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.lifecycle:lifecycle-extensions:2.0.0-alpha1'
implementation 'com.google.android.material:material:1.0.0-alpha1'
// Image Loading
implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
// Dagger
implementation 'com.google.dagger:dagger-android:2.15'
kapt 'com.google.dagger:dagger-android-processor:2.15'
// RxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.3'
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
// Navigation
implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha01'
implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0-alpha01'
// Pretty Logger
implementation 'com.orhanobut:logger:1.15'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha1'
}
When trying to build the application, it fails with the following errors.
error: cannot find symbol
public final class MainActivityViewModel extends android.arch.lifecycle.ViewModel {
^
symbol: class ViewModel
location: package android.arch.lifecycle
error: cannot find symbol
private final androidx.lifecycle.MediatorLiveData
^
symbol: class MediatorLiveData
location: package androidx.lifecycle
error: cannot find symbol
private androidx.navigation.NavController navController;
^
symbol: class NavController
location: package androidx.navigation
...
I've tried with androidx libraries, and the older libraries but both fail. If I removed the Dagger 2 dependencies it will build and run fine.
I'm currently using Android Studio 3.2 Canary 17.
Any idea what's going wrong?
Thanks.
Go to Settings -> Build, Execution, Deployment -> Compiler,
uncheck the 'Configure on demand' option if set.
Also make sure you are using latest gradle and Android Gradle plugin
I managed to track down the cause of the issue, it wasn't related to the build.gradle itself but the version of Java I was using.
Within Android Studios I have changed the JDK to use Java 10.
However, changing this setting back to using the embedded JDK has made the application compile successful.

Categories

Resources