我正在构建一个使用 Dagger-Hilt 的 Android 应用程序。 尽管 Dagger-Hilt 位于我的 build.gradle 文件中,如下所示,
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"
...
}
我在
hiltViewModel()
文件中得到了 MainActivity.kt
的“未解决的参考”,
@Composable
fun Home(
viewModel: MainViewModel = hiltViewModel()
) {
...
}
我不明白为什么?
添加
androidx.hilt:hilt-navigation-compose
解决了我的问题!
我在应用程序级别的 gradle 文件中添加了此依赖项:
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
参考链接:https://developer.android.com/jetpack/compose/libraries#hilt-navigation