15

Got that message error

java.lang.RuntimeException: Unable to create application com.app.name.application.MainApplication: org.koin.error.BeanInstanceCreationException: Can't create bean Bean[class=com.app.name.general.preferences.Preferences] due to error :
    org.koin.error.NoBeanDefFoundException: No definition found to resolve type 'android.app.Application'. Check your module definition
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5830)
    at android.app.ActivityThread.-wrap1(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1673)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:172)
    at android.app.ActivityThread.main(ActivityThread.java:6637)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: org.koin.error.BeanInstanceCreationException: Can't create bean Bean[class=com.app.name.general.preferences.Preferences] due to error :
    org.koin.error.NoBeanDefFoundException: No definition found to resolve type 'android.app.Application'. Check your module definition
    at org.koin.core.instance.InstanceFactory.createInstance(InstanceFactory.kt:63)
    at org.koin.core.instance.InstanceFactory.retrieveInstance(InstanceFactory.kt:26)
    at org.koin.KoinContext$resolveInstance$$inlined$synchronized$lambda$1.invoke(KoinContext.kt:85)
    at org.koin.KoinContext$resolveInstance$$inlined$synchronized$lambda$1.invoke(KoinContext.kt:23)
    at org.koin.ResolutionStack.resolve(ResolutionStack.kt:23)
    at org.koin.KoinContext.resolveInstance(KoinContext.kt:80)
    at com.app.name.constants.EnvironmentConstants$initEnvironmentVariables$$inlined$getKoinInstance$1$1.invoke(KoinComponent.kt:114)
    at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:131)
    at com.app.name.constants.EnvironmentConstants$initEnvironmentVariables$$inlined$getKoinInstance$1.getValue(Unknown Source:7)
    at com.app.name.constants.EnvironmentConstants.initEnvironmentVariables(EnvironmentConstants.kt:180)
    at com.app.name.application.MainApplication.onCreate(MainApplication.kt:59)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5827)
        ... 8 more

But all dependencies were correct.

Also I noticed that modules without androidApplication() argument works correctly.

Code looks like:

    startKoin(listOf(
            imageManagerModule,
            databaseRepositoryModule
    ))

ImageManager works perfectly

val imageManagerModule: Module = applicationContext {
    bean { ImageManagerImpl() as ImageManager }
}

But Preferences crashes

val preferencesModule: Module = applicationContext {
    bean { PreferencesImpl(androidApplication()) as Preferences }
}
Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56

4 Answers4

10

Solution is easy but not so obvious.

Somehow Android Studio imported standalone startKoin function instead of specific android function.

So I had to replace

import org.koin.standalone.StandAloneContext.startKoin

To

import org.koin.android.ext.android.startKoin

And that works!

Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56
  • 3
    import org.koin.android.ext.android.startKoin is available in production code but not in test. how can I make it available in test code? – windchime Jan 08 '19 at 20:18
  • @windchime If you mean jUnit tests I guess you should avoid ext.android =) In other words use standalone context in jUnit. – Andrey Danilov Jan 09 '19 at 10:47
  • 1
    this error also occurred when testing. koin + coroutines, seems doesn't work well this article only epxlain how to use kotlin coroutines https://medium.com/@tonyowen/android-kotlin-coroutines-unit-test-16e984ba35b4 – mochadwi Feb 18 '19 at 03:57
  • importing org.koin.android.ext.android.startKoin , startKoin cannot resolve symbol.so it is useless – Noorul Jun 22 '22 at 13:30
  • I'm in KMM project. so "startKoin" imported from "org.koin.core.context.startKoin" . is there any issue here? – Ashik Azeez Nov 19 '22 at 17:06
  • Hey but In my case I have same error and my import statement is looks like: import org.koin.core.context.GlobalContext.startKoin – Jaimin Modi Feb 07 '23 at 06:31
3

In my case I needed to make like this:

import android.app.Application
 import org.koin.android.ext.koin.androidContext
  import org.koin.android.ext.koin.androidFileProperties
import org.koin.android.ext.koin.androidLogger
import org.koin.android.viewmodel.dsl.viewModel
import org.koin.core.context.startKoin
import org.koin.core.module.Module
import org.koin.dsl.module
class MyApplication : Application() {
override fun onCreate(){
    super.onCreate()
    // start Koin!
    startKoin {
        // Android context
        androidLogger()
        androidContext(this@MyApplication)
        // use the Android context given there
        // load properties from assets/koin.properties file
        androidFileProperties()


        // modules
        modules(myModule)
    }
}
val myModule: Module = module { viewModel { MyViewModel() }}

}

and use older dependencies:

 implementation("org.koin:koin-android:2.0.1")
implementation("org.koin:koin-android-viewmodel:2.0.1")
Nurseyit Tursunkulov
  • 8,012
  • 12
  • 44
  • 78
1

I had similar issue , Try to just add this dependency it will be resolved

// Room implementation "android.arch.persistence.room:runtime:1.1.1" kapt "android.arch.persistence.room:compiler:1.1.1" .

Priya
  • 289
  • 2
  • 14
0
  1. In my case for another situation it wrote this message:

"org.koin.core.error.NoBeanDefFoundException: No definition found for class:'...Actions'. Check your definitions!"

private val actions: Actions by inject()

var modules = module(override = true) {
    single<Actions> { ActionsImpl(get()) }
}

@Test
fun test() {
    actions.swipe()
}

In this case Actions is an interface.

  1. Make sure you call a variable before unloadKoinModules(modules) has been called!
CoolMind
  • 26,736
  • 15
  • 188
  • 224