I'm trying to send requests with Kotlin using Ktor. My attempts to import JSON serialization are unrecognized by Android Studio.
My build.gradle.kts (:app) dependencies are:
```
implementation("io.ktor:ktor-client-core:2.3.9")
implementation("io.ktor:ktor-client-android:2.3.9")
implementation("io.ktor:ktor-client-serialization:2.3.9")
implementation("io.ktor:ktor-client-logging:2.3.9")
implementation("ch.qos.logback:logback-classic1.2.3")
implementation("io.ktor:ktor-server-content-negotiation:2.3.9")
implementation("io.ktor:ktor-client-apache:2.3.9")
implementation("io.ktor:ktor-client-content-negotiation:2.3.9")
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.9")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:2.3.9")
implementation("io.ktor:ktor-server-default-headers:2.3.9")
```
My plugins are:
```
plugins {
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.jetbrainsKotlinAndroid) apply false
id("io.ktor.plugin") version "2.3.9"
kotlin("plugin.serialization") version "1.9.22"
kotlin("jvm") version "1.9.22"
}
```
My imports are:
```
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.android.Android
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.http.ContentType.Application.Json
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import kotlinx.coroutines.*
import kotlinx.serialization.*
import kotlinx.serialization.Serializable
```
The specific block that's throwing the errors is:
'''
runBlocking {
val client = HttpClient(Android) {
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
})
}
}
'''
Specifically the json(Json are being called unresolved references. While android studio claims "Cannot access class 'kotlinx.serialization.json.Json"
I'm using Android Studio Iguana 2023.2.1 Patch 1 and Kotlin version 232-1.9.0.
I have tried installing and re-installing various dependencies to no avail. I was expecting the Json reference to be resolved by a updated dependencies but no luck so far. It could potentially be conflicting dependencies. I'm also open to alternative ways of parsing JSON while still using Ktor.
Your serializer version is wrong. kotlinx-serialization-json is not from ktor but from kotlin serialization library. change the version to 1.6.0 as specified here
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
}