我想为koin. 我使用RoomDatabase,它在构造函数中接收上下文。应用程序运行良好但测试失败
无法解析应用程序实例。请在 KoinApplication 配置中使用 androidContext() 函数。
如果你想在你的模块中使用上下文,你应该在你的 start koin 方法中传递上下文
// start Koin!
startKoin {
// declare used Android context
androidContext(this@MyApplication)
// declare modules
modules(yourModule)
}
Run Code Online (Sandbox Code Playgroud)
并使用这些库
// Koin AndroidX Scope features
implementation "org.koin:koin-android-scope:2.0.1"
// Koin AndroidX ViewModel features
implementation 'org.koin:koin-androidx-viewmodel:2.0.1'
// Koin AndroidX Experimental features
implementation "org.koin:koin-android-ext:2.0.1"
Run Code Online (Sandbox Code Playgroud)
确保在开始 koin 时设置您的上下文。然后您应该能够通过模块中的 androidContext() 访问它。
startKoin {
androidContext(this@App)
modules(...)
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2134 次 |
| 最近记录: |