1

I have these dependencies in my build.gradle.kts file in share module.

val coroutinesVersion = "1.3.9-native-mt"
val serializationVersion = "1.0.1"
val ktorVersion = "1.4.2"
val sqlDelightVersion = "1.4.4"

sourceSets {
    val commonMain by getting {
        dependencies {
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion")
            implementation("io.ktor:ktor-client-core:$ktorVersion")
            implementation("io.ktor:ktor-client-serialization:$ktorVersion")
            implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion")
            implementation("com.squareup.sqldelight:coroutines-extensions:$sqlDelightVersion")
        }
    }
    ...
}

When I run the app everything works fine in android app. But I get a runtime crash when running the ios app. In logs, I see that Ktor is complaining about coroutines version not being native-mt. Which I don't understand why because 1.4.# version of coroutines don't have separate native multithreaded branches.

I looked at the External Libraries folder and found that coroutines version in my case was set to 1.3.9 all the time. If I remove com.squareup.sqldelight:coroutines-extensions Everything works just fine again. But I need that dependency to consume Flow from db.

I tried excluding coroutines from sqldelight extension but it didn't compile in Xcode build.

implementation("com.squareup.sqldelight:coroutines-extensions:$sqlDelightVersion") {
    exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core")
}

So my questions are:

  1. Why does SQLDelight override kotlinx-coroutines-core version?
  2. Why does Ktor want kotlinx-coroutines-core versions only with native-mt suffix?
  3. How can I solve this versions problem?

image

Marat
  • 6,142
  • 6
  • 39
  • 67

1 Answers1

4

1 - That's the version SQLdelight uses. Gradle made a decision to pull in that one vs what ktor wants.

2 - Ktor now needs the multithreaded version.

3 - Solutions

a - Force the version ktor wants. Define the coroutines dependency with version and strictly:

implementation(Deps.Coroutines.common) {
            version {
                strictly(Versions.coroutines)
            }
        }

b - The Coroutines extension is one file. We need to update KaMPKit and remove this, but we had a version copied into the source to avoid earlier issues with SQLDelight: https://github.com/touchlab/KaMPKit/blob/master/shared/src/commonMain/kotlin/co/touchlab/kampkit/sqldelight/CoroutinesExtensions.kt

c - SQLDelight needs a version bump. I can hack away at that over the next few days, but not sure when that will be ready. You can just wait for that?

Kevin Galligan
  • 16,159
  • 5
  • 42
  • 62
  • setting version strictly helped. But I had to use same version for `coroutines-android` dependency to make project compile. Not sure if it is good or bad. I came across to `CoroutinesExtensions` file when trying to solve this. I thought that if I can't make extension work I will remove it and copy KaMPKit extension file too. Thank you for providing that! – Marat Nov 25 '20 at 10:58
  • I would appreciate if you could help me to resolve Ktor coroutines confusion. On this page https://github.com/Kotlin/kotlinx.coroutines for Multiplatform it says it is enough to specify `"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1"`. But it does not work with Ktor. Is there 1.4.1-native-mt that we have to use? Or maybe Ktor needs to be updated to support new versions of coroutines? – Marat Nov 25 '20 at 11:01
  • 1
    On the last question, my info is out of date, unfortunately. I'd need to refresh the current situation. When 1.4 released, Ktor moved to the mt version of coroutines, but there were still two distinct versions of coroutines. If I get a chance to dig into it soon, I'll update this. – Kevin Galligan Nov 25 '20 at 14:31
  • 1
    Ok, so there is a `1.4.1-native-mt` version of coroutines. https://kotlinlang.org/docs/mobile/concurrency-and-coroutines.html#multithreaded-coroutines it looks like it was released not long ago https://github.com/Kotlin/kotlinx.coroutines/issues/462#issuecomment-730431361 – Marat Nov 26 '20 at 10:21