user477174
user477174

Reputation: 61

Not able to import @Inject annotation

I have implemented Hilt dependencies in my project, but when I need the @Inject annotation for my constructor, it's not working. basically when I tried to import it manually i found out, the inject folder inside javax is empty. So the structure is like javax.inject. And after that I get nothing as the inject folder is empty. I have tried rebuilding and cleaning he project. I also tried by Invalidating caches but nothing seems to work. How can I make this work ?

Dependencies List

dependencies {
    implementation(project(":shared"))

    implementation(AndroidX.appCompat)

    implementation(Compose.runtime)
    implementation(Compose.runtimeLiveData)
    implementation(Compose.ui)
    implementation(Compose.material)
    implementation(Compose.uiTooling)
    implementation(Compose.foundation)
    implementation(Compose.compiler)
    implementation(Compose.constraintLayout)
    implementation(Compose.activity)
    implementation(Compose.navigation)

    implementation(Google.material)

    implementation(Hilt.hiltAndroid)
    implementation(Hilt.hiltNavigation)
    kapt(Hilt.hiltCompiler)

    implementation(Kotlinx.datetime)

    debugImplementation(SquareUp.leakCanary)
}

Hilt version - 2.37

Upvotes: 5

Views: 2135

Answers (4)

M.Ed
M.Ed

Reputation: 1328

Make sure you're annotating with the @Inject (uppercase I) and not @inject with a lowercase i.

Upvotes: 0

Omid Heshmatinia
Omid Heshmatinia

Reputation: 5236

If your javax folder is empty, you need to remove the javax cache and let the Gradle download it again.

  1. right-click on javax library and choose Library Properties enter image description here
  2. in the dialog, go to the address and remove that folder to let the Gradle download the library again. enter image description here
  3. sync the Gradle. It will download the jar and the folder will not be empty anymore

Upvotes: 0

iamkdblue
iamkdblue

Reputation: 3622

All your dependency is right, it's just an android studio bug!

I am having the same problem using Android Studio 4.2.2, I solved using the beta version of Android Studio

Upvotes: 1

Oskar Lasota
Oskar Lasota

Reputation: 66

I believe you are missing the plugins in the gradle file

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

Upvotes: 0

Related Questions