Search code examples
androidkotlinandroid-jetpack-composedagger-hilt

Hilt dependancy issue: Expected @AndroidEntryPoint to have a value. Did you forget to apply the Gradle Plugin? (com.google.dagger.hilt.android)


I've already looked at the other posts on here but no answer worked

I am trying to use Hilt with Room and KSP

The issue started when I changed

val viewModel: LoginViewModel = viewModel() to val viewModel: LoginViewModel = hiltViewModel()

The LoginScreen that contains that has @Composable and no specific hilt annotation

build.gradle.kts (:app)

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.kotlin.compose)
    alias(libs.plugins.ksp)
//    alias(libs.plugins.hilt)
}

dependencies {
    implementation(libs.hilt.navigation)
    implementation(libs.hilt.android)
    implementation(libs.hilt.compiler)
    ksp(libs.hilt.compiler)
//    ksp(libs.dagger.compiler)
//    implementation(libs.hilt.gradle)
//    implementation(libs.dagger.compiler)

Uncommenting the hilt plugin in here prevents compile:

Unable to load class 'com.google.devtools.ksp.gradle.KspTaskJvm'
com.google.devtools.ksp.gradle.KspTaskJvm
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

build.gradle.kts (top level)

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.kotlin.android) apply false
    alias(libs.plugins.kotlin.compose) apply false
    alias(libs.plugins.hilt) apply false
}

These are all aliases to libs.versions.toml so lets take a peek

[versions]
kotlin = "2.1.0"
hilt = "2.53.1"
daggerCompiler = "2.53.1"
hiltNavigation = "1.2.0"
ksp = "2.1.0-1.0.29"

[libraries]
dagger-compiler = { module = "com.google.dagger:dagger-compiler", version.ref = "daggerCompiler" }
hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
hilt-compiler = { group = "com.google.dagger", name = "hilt-compiler", version.ref = "hilt" }
#   hilt-gradle = { group = "com.google.dagger", name = "hilt-android-gradle-plugin", version.ref = "hilt"}
hilt-navigation = { group = "androidx.hilt", name = "hilt-navigation-compose", version.ref = "hiltNavigation" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }

Also:

  • There is only one activity and it has the @AndroidEntryPoint annotation.
  • Application has @HiltAndroidApp
  • ViewModel has @HiltViewModel and @Inject constructor

As far as I can tell, the issue is that Hilt is missing in the app-level plugin block. However when it's included I cannot compile.

It's possible that defining the viewModel inside LoginScreen is a bad idea (I'm new at android dev). I wanted it for

// Login Button
Button(
    onClick = { viewModel.validateLogin(username, password) },
    modifier = Modifier.fillMaxWidth()
) {}

But maybe thats better off handled elsewhere.


Solution

  • Solved Answer provided here: https://www.reddit.com/r/androiddev/comments/1hwszn9/looking_for_help_for_hilt_dependency_injection/

    Kotlin 2.1 requires ksp2 which is only supported by hilt starting at update 2.54

    Step 1: change hilt from 2.53.1 to 2.54

    Step 2: add ksp to root level plugin declaration

    Step 3: in module level build.gradle, remove "implementation(libs.hilt.compiler)"