Home > Software engineering >  Koin: getting NoBeanDefFoundException while resolving class instance in Java Activity
Koin: getting NoBeanDefFoundException while resolving class instance in Java Activity

Time:10-06

Using Koin3 for Java project. Currently I have to inject/get Koin instances in some Java Activities. But when I'm trying start an Activity with Koin injection, I get the following error: org.koin.core.error.NoBeanDefFoundException: No definition found for class [class_name]. Check your definitions!. Any suggestions?

Usign the same injections in Kotlin ViewModel class works good as expected.

Application:

KoinApplication koin = KoinAndroidApplication
        .create(this)
        .logger(new AndroidLogger())
        .modules(KoinModuleKt.getAppModule());
startKoin(koin);

Module:

val appModule = module {
    //singletons
    single <SettingsManagerInterface> { SettingsManager(androidContext()) }
    //factories
    factory <SystemServiceHelperInterface> { SystemServiceHelper(androidContext()) }
    viewModel { SettingsViewModel(get(), get()) }
}

Activity:

import static org.koin.java.KoinJavaComponent.get;

    public class LoginActivity extends AppCompatActivity {
        private final SettingsManagerInterface settingsManager = get(SettingsManager.class);
        private final SystemServiceHelperInterface serviceHelper = get(SystemServiceHelper.class);

CodePudding user response:

Use the interface class with a definition in koin graph in your koin get() calls, e.g. chante

private final SettingsManagerInterface settingsManager = get(SettingsManager.class);
private final SystemServiceHelperInterface serviceHelper = get(SystemServiceHelper.class);

to

private final SettingsManagerInterface settingsManager = get(SettingsManagerInterface.class);
private final SystemServiceHelperInterface serviceHelper = get(SystemServiceHelperInterface.class);
  • Related