Koin: Caused by: org.koin.core.error.NoBeanDefFoundException:

Created on 28 Jun 2019  路  4Comments  路  Source: InsertKoinIO/koin

This might be a stupid question and I'm new to this koin.

val loginModule = module {
    viewModel { LoginViewModel(provideAccountUseCase(provideAccountRepository(get()))) }

}

fun provideAccountUseCase(accountRepository: AccountRepository): AccountUseCases {
    return AccountUseCaseImpl(accountRepository)
}

fun provideAccountRepository(serviceApi: MhomServiceApi): AccountRepository {
    return AccountRepositoryImpl(serviceApi)
}

If I do this everything is working !!

but

val loginModule = module {
    viewModel { LoginViewModel(get()) }

}

fun provideAccountUseCase(accountRepository: AccountRepository): AccountUseCases {
    return AccountUseCaseImpl(accountRepository)
}

fun provideAccountRepository(serviceApi: MhomServiceApi): AccountRepository {
    return AccountRepositoryImpl(serviceApi)
}

This is throwing an exception.

2019-06-28 06:37:48.390 17723-17723/? I/thcare.mhom.de: Not late-enabling -Xcheck:jni (already on)
2019-06-28 06:37:48.456 17723-17723/? W/thcare.mhom.de: Unexpected CPU variant for X86 using defaults: x86
2019-06-28 06:37:49.193 17723-17734/com.hcahealthcare.mhom.dev I/thcare.mhom.de: Background concurrent copying GC freed 15529(7MB) AllocSpace objects, 0(0B) LOS objects, 56% free, 1164KB/2MB, paused 11.002ms total 99.211ms
2019-06-28 06:37:49.347 17723-17723/com.hcahealthcare.mhom.dev D/AndroidRuntime: Shutting down VM
2019-06-28 06:37:49.351 17723-17723/com.hcahealthcare.mhom.dev E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hcahealthcare.mhom.dev, PID: 17723
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hcahealthcare.mhom.dev/com.hcahealthcare.mhom.feature.login.activity.TestActivity}: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Factory,primary_type:'com.hcahealthcare.mhom.feature.login.viewmodel.LoginViewModel']
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Factory,primary_type:'com.hcahealthcare.mhom.feature.login.viewmodel.LoginViewModel']
at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:61)
at org.koin.core.instance.FactoryDefinitionInstance.get(FactoryDefinitionInstance.kt:37)
at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:70)
at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165)
at org.koin.core.scope.Scope.get(Scope.kt:128)
at org.koin.androidx.viewmodel.ViewModelResolutionKt$createViewModelProvider$1.create(ViewModelResolution.kt:66)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130)
at org.koin.androidx.viewmodel.ViewModelResolutionKt.getInstance(ViewModelResolution.kt:43)
at org.koin.androidx.viewmodel.ViewModelResolutionKt.getViewModel(ViewModelResolution.kt:23)
at org.koin.androidx.viewmodel.ext.android.LifecycleOwnerExtKt.getViewModel(LifecycleOwnerExt.kt:85)
at com.hcahealthcare.mhom.feature.login.activity.TestActivity$$special$$inlined$viewModel$1.invoke(LifecycleOwnerExt.kt:95)
at com.hcahealthcare.mhom.feature.login.activity.TestActivity$$special$$inlined$viewModel$1.invoke(Unknown Source:0)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.hcahealthcare.mhom.feature.login.activity.TestActivity.getLoginViewModel(Unknown Source:7)
at com.hcahealthcare.mhom.feature.login.activity.TestActivity.onCreate(TestActivity.kt:19)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)聽
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)聽
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)聽
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)聽
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)聽
at android.os.Handler.dispatchMessage(Handler.java:106)聽
at android.os.Looper.loop(Looper.java:193)聽
at android.app.ActivityThread.main(ActivityThread.java:6669)聽
at java.lang.reflect.Method.invoke(Native Method)聽
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)聽
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)聽
Caused by: org.koin.core.error.NoBeanDefFoundException: No definition found for 'com.hcahealthcare.mhom.domain.account.AccountUseCases' has been found. Check your module definitions.
at org.koin.core.scope.Scope.findDefinition(Scope.kt:170)
at org.koin.core.scope.Scope.resolveInstance(Scope.kt:164)
at org.koin.core.scope.Scope.get(Scope.kt:128)
at com.hcahealthcare.mhom.di.login.LoginModuleKt$loginModule$1$1.invoke(LoginModule.kt:31)
at com.hcahealthcare.mhom.di.login.LoginModuleKt$loginModule$1$1.invoke(Unknown Source:4)
at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:54)
at org.koin.core.instance.FactoryDefinitionInstance.get(FactoryDefinitionInstance.kt:37)聽
at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:70)聽
at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165)聽
at org.koin.core.scope.Scope.get(Scope.kt:128)聽
at org.koin.androidx.viewmodel.ViewModelResolutionKt$createViewModelProvider$1.create(ViewModelResolution.kt:66)聽
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164)聽
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130)聽
at org.koin.androidx.viewmodel.ViewModelResolutionKt.getInstance(ViewModelResolution.kt:43)聽
at org.koin.androidx.viewmodel.ViewModelResolutionKt.getViewModel(ViewModelResolution.kt:23)聽
at org.koin.androidx.viewmodel.ext.android.LifecycleOwnerExtKt.getViewModel(LifecycleOwnerExt.kt:85)聽
at com.hcahealthcare.mhom.feature.login.activity.TestActivity$$special$$inlined$viewModel$1.invoke(LifecycleOwnerExt.kt:95)聽
at com.hcahealthcare.mhom.feature.login.activity.TestActivity$$special$$inlined$viewModel$1.invoke(Unknown Source:0)聽
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)聽
at com.hcahealthcare.mhom.feature.login.activity.TestActivity.getLoginViewModel(Unknown Source:7)聽
at com.hcahealthcare.mhom.feature.login.activity.TestActivity.onCreate(TestActivity.kt:19)聽
at android.app.Activity.performCreate(Activity.java:7136)聽
at android.app.Activity.performCreate(Activity.java:7127)聽
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)聽
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)聽
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)聽
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)聽
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)聽
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)聽
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)聽
at android.os.Handler.dispatchMessage(Handler.java:106)聽
at android.os.Looper.loop(Looper.java:193)聽
at android.app.ActivityThread.main(ActivityThread.java:6669)聽
at java.lang.reflect.Method.invoke(Native Method)聽
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)聽
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)聽

Question:?

Why koin can Auto resolve the dependencies, why I need to do it explicitly?

Most helpful comment

@andre-artus You can use

single<PreferencesHelper> { PreferencesHelperImpl(androidContext()) } 

All 4 comments

Hey @pollux-

Why koin can Auto resolve the dependencies, why I need to do it explicitly?

I'm not sure I completely got your question (you probably miss a not somewhere).
Can I ask you to rephrase?

The reason why this is crashing:

val loginModule = module {
    viewModel { LoginViewModel(get()) }
}

Is because in order to build a LoginViewModel you need to tell Koin how to build instances of type AccountUseCases. Looks like you don't have any single or factories in your modules that can be used to provide instances of AccountUseCases.

I don't know your exact Koin setup but something like that would work:

val loginModule = module {
    viewModel { LoginViewModel(get()) }
    single { provideAccountUseCase(get()) }
    single { provideAccountRepository(get()) }
}

@cortinico My understanding was only I need a provider method, just like that we do it for Dagger and annotate with @provider. Thanks a lot. Awesome !!

I had this error appear after a code cleanup removed a "useless cast"
From

  single { PreferencesHelperImpl(androidContext()) as PreferencesHelper } 

to

  single { PreferencesHelperImpl(androidContext()) } 

What I'm doing for the moment is to just annotate with @Suppress("USELESS_CAST"), not sure if there is a better way.

@andre-artus You can use

single<PreferencesHelper> { PreferencesHelperImpl(androidContext()) } 
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jeevuz picture Jeevuz  路  4Comments

AHarazim picture AHarazim  路  3Comments

iRYO400 picture iRYO400  路  3Comments

fmobus picture fmobus  路  4Comments

erikhuizinga picture erikhuizinga  路  3Comments