我正在modular application上使用documentation上的Koin。
我在核心模块的startKoin
上调用BaseApplication
:
val myModule = module {
factory { MyClass(androidContext()) }
}
override fun onCreate() {
super.onCreate()
startKoin(this, listOf(myModule))
}
并通过loadKoinModules
在其他模块中加载其他模块:
loadKoinModules(otherModules)
第一次运行时一切正常,但是当我重新打开应用程序时,出现此错误:
com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 8252
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: org.koin.error.BeanOverrideException: Try to override definition with Factory [name='MyClass',class='com.example.module.MyClass'], but override is not allowed. Use 'override' option in your definition or module.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
有没有办法销毁Koin或其他解决方案?
答案 0 :(得分:0)
尝试一下:
val myModule = module(override = true) {
factory { MyClass(androidContext()) }
}