【发布时间】:2019-05-24 09:42:00
【问题描述】:
我的android项目中有两个模块,app模块和lib模块。
这两个模块都需要 Koin 进行 D.I.,所以我在 app 模块中的 MyApplication 类中调用 startKoin,在 lib 模块中调用 IninKointContentProvider,如下所示。
// app module
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin(this, modules1)
}
}
// lib module
class InitKoinContentProvider : ContentProvider() {
override fun onCreate(): Boolean {
startKoin(context.applicationContext, modules2)
return true
}
}
然后应用程序崩溃并显示此消息
Caused by: org.koin.error.BeanOverrideException: Try to override definition with Single [class='android.content.Context'], but override is not allowed. Use 'override' option in your definition or module.
我猜startKoin只能调用一次。
我找到的解决方案是合并两个 koin 模块,然后在 MyApplication 中调用 startKoin,但我不喜欢它。 Lib模块可能被其他不使用koin的android项目导入,在这种情况下,我认为在InitKoinContentProvider中调用startKoin更好。
这个问题有什么解决办法吗??谢谢!
【问题讨论】: