android Koin安卓系统:发生错误,

ee7vknir  于 2022-11-20  发布在  Android
关注(0)|答案(4)|浏览(187)

收到消息错误

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

但所有依赖关系都是正确的。
我还注意到没有androidApplication()参数的模块工作正常。
代码如下所示:

startKoin(listOf(
            imageManagerModule,
            databaseRepositoryModule
    ))

ImageManager工作正常

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

但首选项崩溃

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

rm5edbpk1#

解决办法很简单,但不是那么明显。
不知何故Android Studio导入了独立的startKoin功能,而不是特定的Android功能。
所以我不得不换掉

import org.koin.standalone.StandAloneContext.startKoin

结束日期

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

这很管用!

31moq8wy

31moq8wy2#

在我的情况下,我需要这样做:

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")
7eumitmz

7eumitmz3#

我遇到过类似问题,尝试添加此依赖项即可解决
// Room implementation "android.arch.persistence.room:runtime:1.1.1" kapt "android.arch.persistence.room:compiler:1.1.1" .

py49o6xq

py49o6xq4#

在我的情况下为另一种情况它写了这样的消息:
“发生错误。未找到类“... Actions”的定义。请检查您的定义!”

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

@Test
fun test() {
    getKoin().get<Actions>().swipe()
}

在本例中,Actions是一个接口。只是将其替换为实现:

getKoin().get<ActionsImpl>().swipe()

相关问题