kotlinandroid-jetpack-composedagger-hilt

Getting issue dependency was found while adding dagger hilt


I am running a project with Jetpack Compose. And I'm using Kotlin DSL. And I added the dagger hilt to my project and I'm having an issue with the project.

java.lang.IllegalStateException: The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found.

dagger-hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
devtools-ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }

hilt version is 2.44 or 2.51.1 and 1.9.20-1.0.14 I tried with the difffent version of ksp also.

Add plugin on main build.gradle.kts

alias(libs.plugins.dagger.hilt.android) apply false
alias(libs.plugins.devtools.ksp) apply false

Add plugin on the app build.gradle.kts

alias(libs.plugins.devtools.ksp)
alias(libs.plugins.dagger.hilt.android) // If I add this in a plugin I'm getting the error.

When I'm building the project getting issues

A problem occurred configuring project ':app'. The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found.

And I follow those steps too - Dependency injection with Hilt


Solution

  • We need to add the dependency at the same time while adding plugins. It will not work with the adding plugins only. Thank you to Chirag Thummar

    Add dependencies

    hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
    hilt-android-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hilt" }
    

    Add bellow code on app level build.gradle.kts too.

    dependencies {
        implementation(libs.hilt.android)
        ksp(libs.hilt.android.compiler)
    }