【发布时间】:2020-04-16 21:30:02
【问题描述】:
我正在尝试在我的项目中实现 Koin。到目前为止,我是这样做的:
我的共享偏好类:
class MPCUtilSharedPreference(private val sharedPreferences: SharedPreferences{}
我想将该类注入其他类。所以,我创建了我的 MainApplication 类:
class MPCMainApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@MPCMainApplication)
modules(modules)
}
}
}
这是我的模块类:
private val appModule = module {
single {
MPCUtilSharedPreference(
androidContext().getSharedPreferences(
BuildConfig.APP_PREFERENCE,
Context.MODE_PRIVATE
)
)
}
}
val modules = listOf(appModule)
我正在尝试注入它:
class MPCNetworkInterceptor : Interceptor {
private val utilSharedPreferences: MPCUtilSharedPreference by inject() }
错误提示:
没有为参数“clazz”传递值
我正在尝试使用
import org.koin.android.ext.koin.androidContext
但是AS使用
import org.koin.java.KoinJavaComponent.inject
这是我的毕业典礼:
implementation 'org.koin:koin-android:2.1.5'
implementation 'org.koin:koin-androidx-scope:2.1.5'
implementation 'org.koin:koin-androidx-viewmodel:2.1.5'
implementation 'org.koin:koin-androidx-fragment:2.1.5'
知道这里有什么问题吗?
【问题讨论】:
-
您为什么不直接传递上下文并让您的实现在内部选择首选项文件和模式?你能发布完整的错误信息吗?
标签: android dependency-injection koin