anandyn02
anandyn02

Reputation: 345

java.lang.IllegalStateException: KoinApplication has not been started

I am learning kotlin using koin. While running the application in catlog I see the following message.

java.lang.IllegalStateException: KoinApplication has not been started

though I have used startKoin in MyApplication

class MyApplication : Application() {

    var listOfModules = module {
        single { GitHubServiceApi() }
    }

    override fun onCreate() {
        super.onCreate()

        startKoin {
            androidLogger()
            androidContext(this@MyApplication)
            modules(listOfModules)
        }

    }

} 

enter image description here

Upvotes: 29

Views: 31342

Answers (5)

CoolMind
CoolMind

Reputation: 28793

If you write unit test, you also should add startKoin and inject dependencies:

@Before
fun startKoinForTest() {
    startKoin {
        modules(DI.modules(...))
    }
}

@After
fun stopKoinAfterTest() = stopKoin()

See also Koin Android Test.

Upvotes: 1

Sohaib Ahmed
Sohaib Ahmed

Reputation: 3062

In you manifest.xml,

<application>
    android:name=".MyApplication"
    ...
</application

Add this line in application tag.

Upvotes: 13

Acc Manager
Acc Manager

Reputation: 1

private fun getKoin(activity: ComponentActivity): Koin {
return if (activity is KoinComponent) {
    activity.getKoin()
} else {
    GlobalContext.getOrNull() ?: startKoin {
        androidContext(activity)
        modules(listModule)
    }.koin
}

}

 fun ComponentActivity.contextAwareActivityScope() = runCatching {
LifecycleScopeDelegate<Activity>(
    lifecycleOwner = this,
    koin = getKoin(this)
)}.getOrElse { activityScope() 
}

Upvotes: 0

Leonard
Leonard

Reputation: 215

Adding "android:name=".TheApplication" in the Manifest file solved the issue.

    android:name=".TheApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_app_icon_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Shrine">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

"android:name=".TheApplication" is the Class Name from Koin

class TheApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        if (BuildConfig.DEBUG) {
            Timber.plant(Timber.DebugTree())
        }

        startKoin {
            androidLogger()
            androidContext(androidContext = this@TheApplication)

            modules(
                listOfModules
            )
        }
    }
}

Upvotes: 21

Xarybdis
Xarybdis

Reputation: 157

Basically, you need to give the name of the class where you called startKoin() method, in the Manifest as an application name. And this will let your configure logging, properties loading and modules. Check this out: https://doc.insert-koin.io/#/koin-core/dsl

Upvotes: 0

Related Questions