unresolved reference hiltViewModel() despite dagger-hilt in build.gradle
Asked Answered
C

3

8

I'm building an Android app that uses Dagger-Hilt. Altough Dagger-Hilt is in my build.gradle file as you can see below,

build.gradle (project file):

buildscript {
    ext {
        compose_version = '1.1.0-beta01'
    }
    dependencies {
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
    }
}
...

build.gradle (app file):

...
dependencies {

    ...
    implementation "com.google.dagger:hilt-android:2.38.1"
    kapt "com.google.dagger:hilt-compiler:2.38.1"
    ...
}

I get an "unresolved reference" for hiltViewModel() in the MainActivity.kt file,

@Composable
fun Home(
  viewModel: MainViewModel = hiltViewModel()
) {
  ...
}

I do not understand why ?

Chubby answered 3/9, 2022 at 6:27 Comment(2)
I think you'll need androidx.hilt:hilt-navigation-compose, more info: developer.android.com/jetpack/compose/libraries#hilt-navigation.Mullen
Indeed, that was the problem. Didn't think I needed it since I do not use any navigation in my app.Chubby
U
22

adding androidx.hilt:hilt-navigation-compose solved my issue!

Urinary answered 24/7, 2023 at 14:28 Comment(0)
S
2
Sweatbox answered 27/8, 2023 at 10:9 Comment(0)
H
1

Adding this dependency solved my problem:

    implementation("androidx.hilt:hilt-navigation-compose:1.2.0")
Hutch answered 8/3, 2024 at 1:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.