Search code examples
androiddagger-2dagger-hilt

Cannot create an instance of ViewModel with dagger-hilt


Cannot create a viewmodel class when injecting from a data module. I have this module layer app -> feature -> data and i want to inject repository into viewModel

@HiltViewModel
class SignUpViewModel @Inject constructor(private val authRepository: AuthRepository) :
    ViewModel() {

    private val _signUpState = MutableStateFlow(RegistrationState())
    val signUpState = _signUpState.asStateFlow()

    fun onNameChange(name: String) {
        _signUpState.value = _signUpState.value.copy(name = name)
    }

    fun onEmailChange(email: String) {
        _signUpState.value = _signUpState.value.copy(email = email)
    }

    fun onPasswordChange(password: String) {
        _signUpState.value = _signUpState.value.copy(password = password)
    }

    fun onSignUpClick() {
        authRepository.registerUser()
    }

}

ViewModel creating

@Composable
fun SignUpScreen() {
    SignUpScreen(viewModel = hiltViewModel())
}

@Module
@InstallIn(SingletonComponent::class)
abstract class AuthModule {


    @Binds
    internal abstract fun bindsAuthRepository(
        authRepository: AuthRepositoryImpl,
    ): AuthRepository

}

@Module
@InstallIn(SingletonComponent::class)
object DataStorageModule {

    @Provides
    @Singleton
    fun provideAuthLocalDataSource(@ApplicationContext context: Context): AuthDataStorage {
        return AuthDataStorage(context)
    }
}

When i launch application that crash with this error: Cannot create an instance of class com.example.auth_feature.presentation.SignUpViewModel

Tried to create specific @provides with no interface, but unfortunately even that didn't help

Full crash log:

java.lang.RuntimeException: Cannot create an instance of class ru.yangel.auth_feature.presentation.SignUpViewModel
                                                                                                        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:201)
                                                                                                        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:320)
                                                                                                        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:302)
                                                                                                        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:276)
                                                                                                        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.kt:128)
                                                                                                        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:172)
                                                                                                        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:172)
                                                                                                        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:184)
                                                                                                        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:150)
                                                                                                        at androidx.lifecycle.viewmodel.compose.ViewModelKt.get(ViewModel.kt:215)
                                                                                                        at androidx.lifecycle.viewmodel.compose.ViewModelKt.viewModel(ViewModel.kt:156)
                                                                                                        at ru.yangel.auth_feature.presentation.SignUpScreenKt.SignUpScreen(SignUpScreen.kt:98)
                                                                                                        at com.yuriyyangel.wordsfactory.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:18)
                                                                                                        at com.yuriyyangel.wordsfactory.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:17)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                                                                                        at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:428)
                                                                                                        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:252)
                                                                                                        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:251)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                                                                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                                                                                        at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:186)
                                                                                                        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:119)
                                                                                                        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:118)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                                                                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                                                                                        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:110)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$2.invoke(Wrapper.android.kt:139)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$2.invoke(Wrapper.android.kt:138)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
                                                                                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:248)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:138)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:123)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
2024-03-26 18:21:03.438  4512-4512  AndroidRuntime          com.yuriyyangel.wordsfactory         E      at androidx.compose.runtime.ActualJvm_jvmKt.invokeComposable(ActualJvm.jvm.kt:90)
                                                                                                        at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3302)
                                                                                                        at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:3235)
                                                                                                        at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:725)
                                                                                                        at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:1071)
                                                                                                        at androidx.compose.runtime.CompositionImpl.composeInitial(Composition.kt:633)
                                                                                                        at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:619)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:123)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:114)
                                                                                                        at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:1289)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:114)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:164)
                                                                                                        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.kt:322)
                                                                                                        at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.kt:199)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:121)
                                                                                                        at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:114)
                                                                                                        at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:1364)
                                                                                                        at android.view.View.dispatchAttachedToWindow(View.java:21980)
                                                                                                        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3490)
                                                                                                        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
                                                                                                        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
                                                                                                        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
                                                                                                        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
                                                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3014)
                                                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2465)
                                                                                                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9305)
                                                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1339)
                                                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1348)
                                                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:952)
                                                                                                        at android.view.Choreographer.doFrame(Choreographer.java:882)
                                                                                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1322)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:958)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:205)
                                                                                                        at android.os.Looper.loop(Looper.java:294)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8177)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
                                                                                                    Caused by: java.lang.NoSuchMethodException: ru.yangel.auth_feature.presentation.SignUpViewModel.<init> []
                                                                                                        at java.lang.Class.getConstructor0(Class.java:3325)
                                                                                                        at java.lang.Class.getDeclaredConstructor(Class.java:3063)
                                                                                                        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:199)
                                                                                                        ... 76 more

Solution

  • The problem lay on the surface, I had different versions of the dagger and dagger-hilt compiler plugin. We need to be more careful)