Dagger-Hilt: @ViewModelInject is not injecting MyViewModel and crash?
Apparently I miss out adding a kapt, which is kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
The list of dependencies in my build.gradle
is as below
implementation 'androidx.fragment:fragment-ktx:1.2.5'
implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01"
implementation 'com.google.dagger:hilt-android:2.28-alpha'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
kapt 'com.google.dagger:hilt-android-compiler:2.28-alpha'
Just for completeness, the plugin is below
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'kotlin-kapt'
@ViewModelInject
is deprecated in the newer hilt
version
https://developer.android.com/reference/androidx/hilt/lifecycle/ViewModelInject
Use HiltViewModel
@HiltViewModel
class TasksViewModel @Inject constructor(
val taskRepository: TaskRepository
) : ViewModel() {
}
I'd like to add that If you are on a multi-module project, you have to have kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
on the :app
module as well for it to work.