HiltAndroidApp/HiltViewModel "Unresolved Reference" in IDE but it stills builds without error

Viewed 544

So I followed the steps from https://developer.android.com/training/dependency-injection/hilt-android to add Dagger Hilt to my project.

It builds without error, but Android Studio shows errors for @HiltAndroidApp and also for @HiltViewModel, but some of them such as @AndroidEntryPoint are there

  • I have tried invalidate caches/restart
  • I have tried deleting .idea, .gradle.
  • I have tried opening an older project which was working with Hilt, and now that one also shows this error (and also builds without any problem)
  • I have tried using the latest version of Hilt available, 2.39.1, and also with 2.38.1 as it's what's used on the developer.android.com hilt documentation page

I'm out of ideas

1 Answers

I faced the same issue with @Inject. I tried invalidating cache and restart and everything but nothing worked.

The import path for @Inject was javax.inject.Inject.

When I tried to go to the "inject" package by hovering over "inject" from the import statement and CTRL + clicking it, it took me to some gradle cache folder and there was an interface named "Inject" inside it, so ideally the interface was there but android studio wasn't detecting it.

Then I went through some other folders inside the gradle cache with long random names and stuff, I found another package with the same structure (javax.inject) but it was empty. I deleted that empty package manually and the issue was fixed automatically. So I guess all this time android was considering the package with nothing inside it (that I deleted) and not the other one which had the Inject interface from cache. Deleting the empty package from cache fixed it for me.

I tried to recreate the issue to answer it with screenshots and stuff but I am not able to recreate it now. Sorry about that.

Related