Hey all ! I’m using Ktor in a multiplatform libra...
# ktor
m
Hey all ! I’m using Ktor in a multiplatform library, where I need to post some json body to url. My current setup for dependencies is
Copy code
sourceSets {
        commonMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutine_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializer_version"

                implementation "io.ktor:ktor-client-core:$ktor_version"
                implementation "io.ktor:ktor-client-json:$ktor_version"
            }
        }
        androidMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib'
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version"
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializer_version"

                implementation "io.ktor:ktor-client-android:$ktor_version"
                implementation "io.ktor:ktor-client-json-jvm:$ktor_version"
            }
        }
        iosMain {
            dependencies {
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutine_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serializer_version"


                implementation "io.ktor:ktor-client-ios:$ktor_version"
                implementation "io.ktor:ktor-client-json-native:$ktor_version"
            }

        }
    }
I’m having trouble with keeping client in common. If I use client in common module as just an
HttpClient()
, without features, I’m getting class cast exception as in https://github.com/ktorio/ktor/issues/997. Defining content type does not solve it, but I can add
JsonFeature
to my client. This ends up crashing in Android tho:
Copy code
Caused by: java.lang.IllegalStateException: Fail to find serializer. Consider to add one of the following dependencies: 
     - ktor-client-gson
     - ktor-client-json // <- Oh well, it's actually there isn't it ?
     - ktor-client-serialization
As seen above in dependencies, I’m including
io.ktor:ktor-client-json
in common, but it does not contain any serializer that I can assign in
Copy code
install(JsonFeature) {
            serializer = ????
}
block. If I define serializer as
expect
and try to implement
actual
for Android, it’s fine, but again, I can’t find any for iOS implmentation (
io.ktor:ktor-client-json-native
). What is the right way to do this by using
kotlinx.serialization
for both platforms ?
d
serializer = KotlinxSerializer()
You also want
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
m
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
I’ll try with this one, but should it be a dependency for all three modules or just for common ? I found other examples using
serializer = KotlinxSerializer()
, but I don’t have
KotlinxSerializer
class in common.
d
Just common is fine.
You also need
implementation("io.ktor:ktor-client-serialization-jvm:$ktorVersion")
, etc.
m
And
implementation "io.ktor:ktor-client-serialization-native:$ktor_version"
for iOS
Now it’s all connected and working, thanks a lot !! It’d be great if documentation was mentioning them 😞
1
📝 1
m
+1 to that, having to copy/paste various dependencies in all blocks feels like a lot of duplication. Would it be possible to land on a convention like "if artifact-common is a dependency and artifact-jvm exists, then depend on it automatically" ?
that would have to be a lot smarter than that of course...
d
They have that already for some artefacts. Not sure why it didn't work for this particular one.
m
Is that the gradle metadata thing ?
d
Yeah
m
👍