Touhid
Touhid

Reputation: 1686

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.

Upvotes: 0

Views: 863

Answers (2)

andre
andre

Reputation: 1728

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!

Upvotes: 1

Khubaib Khan
Khubaib Khan

Reputation: 133

I Don't have access to code but As I know the issue is due to starting multiple Koin. There are two way to start the Koin.

First way:

 startKoin {
        // Log Koin into Android logger
        androidLogger()
        // Reference Android context
        androidContext(this@MainApplication)
        // Load modules
        modules(myAppModules)
    }

And the other way is By using Koin Application:

KoinApplication(application = {
    // your preview config here
    modules(previewModule)
}) {
    // Compose to preview with Koin
}

Don't use both together. If you do then, you will get this type of error

Upvotes: 0

Related Questions