硬币 Android:org.koin.error.NoBeanDefFoundException

Koin Android: org.koin.error.NoBeanDefFoundException

收到该消息错误

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.invoke(KoinContext.kt:85)
    at org.koin.KoinContext$resolveInstance$$inlined$synchronized$lambda.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.invoke(KoinComponent.kt:114)
    at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:131)
    at com.app.name.constants.EnvironmentConstants$initEnvironmentVariables$$inlined$getKoinInstance.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

但是所有依赖项都是正确的。

我还注意到没有 androidApplication() 参数的模块可以正常工作。

代码如下:

    startKoin(listOf(
            imageManagerModule,
            databaseRepositoryModule
    ))

ImageManager 完美运行

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

但是首选项崩溃了

val preferencesModule: Module = applicationContext {
    bean { PreferencesImpl(androidApplication()) as Preferences }
}

解决方法很简单,但不是很明显。

Studio 以某种方式 Android 导入了独立的 startKoin 函数而不是特定的 android 函数。

所以我不得不更换

import org.koin.standalone.StandAloneContext.startKoin

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

这很有效!

我有类似的问题, 尝试只添加这个依赖它会被解决

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

在我的例子中,我需要这样做:

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() }}

}

并使用旧的依赖项:

 implementation("org.koin:koin-android:2.0.1")
implementation("org.koin:koin-android-viewmodel:2.0.1")