Alfredo Bejarano
Alfredo Bejarano

Reputation: 588

NonExistentClass cannot be converted to Annotation

I added a new Retrofit interface to my project containing a couple of Endpoints annotated with the @GET and @HEADERS annotations, after Injecting said interface to a repository class using the @Inject annotation in the constructor of said class, Android Studio throws this error:

NonExistentClass cannot be converted to Annotation

After taking a look at the generated Java code, it replaces the @GET and @HEADERS annotations with this:

@error.NonExistentClass()

I've already tried the following:

Im using:

could it be some dagger scope issue? or Retrofit / dagger not fully compatible with the new versions of the Kapt plugin?

Upvotes: 5

Views: 20082

Answers (8)

Marino
Marino

Reputation: 1330

Yet another case:

Had this issue on some Worker class after removing some dependencies from the module it lived in.

The worker has the @HiltWorker annotation, which as far as the IDE can tell is resolved just fine: at compile time, it is replaced with the @NonExistentClass one and break.

The issue is solved by adding an explicit dependency to

implementation("androidx.hilt:hilt-work:$version")

Assumptions on why the issue happens:

  • such dependency was imported transitively before doing some changes.
  • the following dependency (which was in place all the time) somehow fools the IDE into thinking it knows @HiltWorker annotation
implementation("com.google.dagger:hilt-android:$version")

Upvotes: 0

pram
pram

Reputation: 1513

For me the problem was caused by migration to view binding from kotlin-android-extensions and Android Studio couldn't find @Parcelize annotation. To add annotations back add this to build.gradle:

apply plugin: 'kotlin-parcelize'

Upvotes: 2

Mnemotechnican
Mnemotechnican

Reputation: 1

Just had a similar problem: I was trying to use picocli's @Command annotation and was getting the same error. It turned out to be an issue with my imports. I was importing this and other annotations via a wildcard import: import picocli.CommandLine.*. When i replaced it with separate imports for each annotation (among which was import picocli.CommandLine.Command), kapt began working correctly.

I assume kapt simply doesn't understand wildcard imports.

Upvotes: 0

Shubham Mogarkar
Shubham Mogarkar

Reputation: 293

In my case I was using this ""com.fasterxml.jackson.core:jackson-databind:2.7.3" library, but later I removed this dependency from gradle, but didn't remove the code that I was using of this library, so removed code and annotations related to this library solved my problem.

Upvotes: 0

hmac
hmac

Reputation: 343

For me was in here:

apply plugin: 'kotlin-android-extensions'

Upvotes: 0

Val Okafor
Val Okafor

Reputation: 3457

For me, its painfully removing all the @Singleton and @OpenForTesting on my Module classes. And removing two DAO classes and Repository whose backing model classes is no longer annotated with @Entity.

Upvotes: 0

Myk
Myk

Reputation: 1019

For me, I had recently removed dagger from a project and forgot to remove the @Singleton and @Inject annotations from relevant classes.

Upvotes: 3

Daniel Wilson
Daniel Wilson

Reputation: 19834

Luckily this question led me to figure out my issue. While moving around classes from an app module into a library, I was referencing an annotation class which only existed in a debug folder. So debug builds were fine, but calls to gradlew install failed when generating release files.

The error for me was very explicit although it took me a long time to realize - the generated file had literally replaced the missing annotation with @error.NonExistentClass()

Moving the file into the main src set meant both debug and release builds could see the class. What took me a while to figure out was that I assumed this was a Dagger issue being masked by kapt, but really it was just a regular old Dagger issue. My advice is to look at your Dagger setup carefully.

Upvotes: 4

Related Questions