小编Coe*_*eus的帖子

尝试使用 koin 但在 android 上无法正常工作

我正在尝试在我的项目中实施 Koin。到目前为止,我这样做了:

我的共享首选项类:

class MPCUtilSharedPreference(private val sharedPreferences: SharedPreferences{}
Run Code Online (Sandbox Code Playgroud)

我想在其他类中注入该类。所以,我创建了我的 MainApplication 类:

class MPCMainApplication : Application() {

override fun onCreate() {
    super.onCreate()
    startKoin {
        androidContext(this@MPCMainApplication)
        modules(modules)
    }
}
Run Code Online (Sandbox Code Playgroud)

}

这是我的模块类:

private val appModule = module {
single {
    MPCUtilSharedPreference(
        androidContext().getSharedPreferences(
            BuildConfig.APP_PREFERENCE,
            Context.MODE_PRIVATE
        )
    )
  }
}
val modules = listOf(appModule)
Run Code Online (Sandbox Code Playgroud)

我正在尝试注入它:

class MPCNetworkInterceptor : Interceptor {

private val utilSharedPreferences: MPCUtilSharedPreference by inject() }
Run Code Online (Sandbox Code Playgroud)

错误说:

没有为参数“clazz”传递值

我正在尝试使用

import org.koin.android.ext.koin.androidContext
Run Code Online (Sandbox Code Playgroud)

但是 AS 使用

import org.koin.java.KoinJavaComponent.inject
Run Code Online (Sandbox Code Playgroud)

这是我的毕业证书:

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 …
Run Code Online (Sandbox Code Playgroud)

android dependency-injection koin

1
推荐指数
2
解决办法
2513
查看次数

标签 统计

android ×1

dependency-injection ×1

koin ×1