0

我通过以下文档在模块化应用程序上使用 Koin 。我打电话给我的核心模块:startKoinBaseApplication

应用

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 或任何其他方法来解决这个问题?

4

1 回答 1

1

尝试这个:

val myModule = module(override = true) {
    factory { MyClass(androidContext()) }
}
于 2019-07-15T11:54:04.290 回答