Koin尚未在多模块项目中启动

问题描述 投票:0回答:1

我正在使用 Koin 来注入一些类。

我遵循了 Koin 官方文档,但每当我启动我的应用程序时,它就会崩溃。我的项目是一个多模块项目,但我所有的注入都在同一个模块中使用。

我的Koin模块的定义:

val databaseModule = module {
    factory { provideIoDispatcher() }
    single { provideHerowDatabase(get()) }
    single { provideZoneDAO(get()) }
    single { providePoiDAO(get()) }
    single { provideCampaignDAO(get()) }
    single { provideZoneRepository(get()) }
    single { providePoiRepository(get()) }
    single { provideCampaignRepository(get()) }
}

private fun provideIoDispatcher(): CoroutineDispatcher = Dispatchers.IO

/** and so on **/

应用类别:

class DetectionApplication : Application(), Configuration.Provider {

    override fun getWorkManagerConfiguration(): Configuration = Configuration.Builder()
        .setMinimumLoggingLevel(android.util.Log.INFO)
        .build()

    override fun onCreate() {
        super.onCreate()

        startKoin {
            androidLogger()
            androidContext(this@DetectionApplication)
            modules(listOf(databaseModule))
        }
    }
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="io.herow.sdk.detection">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <application android:name=".DetectionApplication">

        <receiver android:name=".location.LocationReceiver" />
        <receiver android:name=".geofencing.GeofencingReceiver" />
        <receiver android:name=".notification.NotificationReceiver" />

        <service
            android:name="androidx.work.impl.foreground.SystemForegroundService"
            android:foregroundServiceType="location"
            tools:node="merge" />

        <provider
            android:name="androidx.work.impl.WorkManagerInitializer"
            android:authorities="${applicationId}.workmanager-init"
            tools:node="merge" />
    </application>
</manifest>

我的项目的入口点

class HerowInitializer private constructor(val context: Context) : KoinComponent {

    //Koin
    private val ioDispatcher: CoroutineDispatcher by inject()

    init {
        loadIdentifiers(context)
    }

    private fun loadIdentifiers(context: Context) {
        CoroutineScope(ioDispatcher).launch {
            
            /** code  **/
        }
    }

错误:

enter image description here

android kotlin dependency-injection koin
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.