androidkotlinkotlin-multiplatformkoinkmp

Koin in Kotlin multiplatform (kmp) desktop platform error: NoClassDefFoundError: org/koin/core/error/KoinAppAlreadyStartedException


I am using Kotlin multiplatform (KMP) compose to develop app. After adding koin I am able to run on Android, iOS and Web but when run for desktop platform it shows an error immediately after running.

Error in UI looks like below: enter image description here

IDE: Android studio.

Run command to run for desktop: ./gradlew :composeApp:run

Error log from logcat:

> Task :composeApp:run
2024-08-02 05:21:45.947 java[1103:11876] WARNING: Secure coding is not enabled for restorable state! Enable secure coding by implementing 
NSApplicationDelegate.applicationSupportsSecureRestorableState: and returning YES.
Exception in thread "main" java.lang.NoClassDefFoundError: org/koin/core/error/KoinAppAlreadyStartedException
    at AppKt.App(App.kt:29)

---
Caused by: java.lang.ClassNotFoundException: org.koin.core.error.KoinAppAlreadyStartedException

Using below library versions for commonMain dependencies:

koin = "4.0.0-RC1"
koin-compose = "1.2.0-Beta4"

koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
koin-compose = { module = "io.insert-koin:koin-compose", version.ref = "koin-compose" 

Koin initialisation like below:

@Composable
@Preview
fun App() {
    KoinApplication(application = { // Exception log referring this line (29 no line in App.kt file)
        modules(appModule, platformModule)
    }) {
        MaterialTheme {
       ...

This error only throwing in desktop(JVM) platform.


Solution

  • I found our problem.

    Which is incorrect versioning of koin-compose on our side.

    Since koin version 4.0.0-RC1, koin-compose has the same version number.

    Which means you just need to do:

    koin = "4.0.0-RC1"
    
    koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
    koin-compose = { module = "io.insert-koin:koin-compose", version.ref = "koin"  }
    

    Hope this helped!