简体   繁体   中英

Cannot create ViewModel instance when using Hilt dependency injection

I'm just starting with Jetpack Compose and Hilt. But I'm running into issues when I inject into a ViewModel.

The error that I get:

java.lang.RuntimeException: Cannot create an instance of class com.example.chaes.login.viewModel.SignUpViewModel

Caused by: java.lang.InstantiationException: java.lang.Class<com.example.chaes.login.viewModel.SignUpViewModel> has no zero argument constructor

I can inject all fine in the Activity but not in the ViewModel. I've tried all solutions I could find.

My gradle files:

Project root level:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
        classpath 'com.google.gms:google-services:4.3.8'
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.37'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

Module level:

apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
kapt{
    correctErrorTypes true
}
dependencies {

    ...
    // Hilt
    implementation "com.google.dagger:hilt-android:2.37"
    kapt "com.google.dagger:hilt-android-compiler:2.37"
    ...
}

My application file

@HiltAndroidApp
class BaseApplication: Application() {
}

My Module File:

@Module
@InstallIn(SingletonComponent::class)
object AuthRepoModule {
    @Singleton
    @Provides
    fun provideAuthRepo(): FirebaseAuthRepo{
        return FirebaseAuthRepo()
    }

    @Singleton
    @Provides
    fun provideRandomString(): String{
        return "gejifeg"
    }
}

The project is single activity with composable screens so the MainActivity:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() { ... }

ViewModel:

@HiltViewModel
class SignUpViewModel @Inject constructor(
    firebaseAuthRepo: FirebaseAuthRepo,
) : ViewModel() { ... }

Things I've tried:

  • Changing to ViewModelComponent instead of Singleton
  • changing to kapt "com.google.dagger:hilt-compiler:2.37" instead of kapt "com.google.dagger:hilt-android-compiler:2.37"
  • deleting the build folder and rebuilding the project
  • invalidating cache and restarting

Edit: Solution Found! As mentioned by @sitatech in the comments, one needs to use hiltViewModel() instead of viewModel() to provide the viewModel to the composable.

In app module dependency add

...
// Hilt
implementation "com.google.dagger:hilt-android:2.37"
kapt "com.google.dagger:hilt-android-compiler:2.37"

implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0-alpha03"
...

Other portions seem ok to me.

正如@sitatech 在评论中提到的,我需要在可组合中使用 hiltViewModel() 而不是 viewModel()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM