I am trying to use kotlinx.serialization in my KMM application to parse a json Http response from a website. I have tried many of the solutions that I have found on the web but non have solved my problem. I had to update to Android Studio/Kotlin Version 1.5.20 to eliminate an error in the Kotlin-datetime module. Know facing new compiler errors. Can anyone help?
Kotlin class:
import com.pagetyler.dailynasa.shared.entity.Defaults
import io.ktor.client.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.request.*
class NasaPicsApi {
private val httpClient = HttpClient {
install(JsonFeature) {
val json = kotlinx.serialization.json.Json { ignoreUnknownKeys = true }
serializer = KotlinxSerializer(json)
}
}
suspend fun getPicture(currDate:String): List<NasaPicture> {
val ep = "$PICTURES_ENDPOINT$EXTENSION?api_key=$DEFAULT_APIKEY;date=$currDate "
return httpClient.get(ep)
}
companion object {
private val PICTURES_ENDPOINT = Defaults().PICTURES_ENDPOINT
private val EXTENSION = Defaults().EXTENSION
private val DEFAULT_APIKEY = Defaults().DEFAULT_APIKEY
}
}
Common/Shared build.gradle.kts:
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val kotlinVersion = "${rootProject.extra["kotlin_version"]}"
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization") version "1.5.20"
id("com.android.library")
id("com.squareup.sqldelight")
}
group = "com.pagetyler"
version = "1.0-SNAPSHOT"
repositories {
gradlePluginPortal()
google()
jcenter()
mavenCentral()
maven(url = "https://kotlin.bintray.com/kotlinx/") // soon will be just jcenter()
}
kotlin {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
useIR = true
}
}
val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iOSTarget("ios") {
binaries {
framework {
baseName = "shared"
}
}
}
jvm().compilations.all {
kotlinOptions {
useIR = true
}
}
android()
val ktorVersion = "1.4.0"
val serializationVersion = "1.0.0-RC-$kotlinVersion"
val sqlDelightVersion: String by project
val coroutinesVersion = "1.3.9-native-mt"
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.2.1")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
implementation(kotlin("stdlib-common"))
implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion")
//implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.3.0")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.2.1")
//implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
implementation("io.ktor:ktor-client-android:$ktorVersion")
implementation("com.squareup.sqldelight:android-driver:$sqlDelightVersion")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13")
}
}
val iosMain by getting {
dependencies {
implementation("io.ktor:ktor-client-ios:$ktorVersion")
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
}
}
val iosTest by getting
}
}
android {
compileSdkVersion(30)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24)
targetSdkVersion(30)
versionCode = 2
versionName = "2.0"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
}
sqldelight {
database("PicturesDB") {
packageName = "com.pagetyler.shared.cache"
}
}
val packForXcode by tasks.creating(Sync::class) {
group = "build"
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val framework = kotlin.targets.getByName<KotlinNativeTarget>
("ios").binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
val targetDir = File(buildDir, "xcode-frameworks")
from({ framework.outputDirectory })
into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
This is the Global build.gradle.kts
buildscript {
val kotlin_version by extra("1.5.20")
repositories {
gradlePluginPortal()
google()
mavenCentral()
maven(url = "https://kotlin.bintray.com/kotlinx/") // soon will be just jcenter()
}
val sqlDelightVersion= "1.5.0"
dependencies {
classpath("com.android.tools.build:gradle:4.2.2")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0")
classpath("org.jetbrains.kotlin:kotlin-serialization:$kotlin_version")
classpath("com.squareup.sqldelight:gradle-plugin:$sqlDelightVersion")
file("libs\\YouTubeAndroidPlayerApi.jarlibs\\YouTubeAndroidPlayerApi.jar")
}
}
apply (plugin = "com.squareup.sqldelight")
apply (plugin = "kotlin")
group = "com.pagetyler"
version = "1.0-SNAPSHOT"
allprojects {
repositories {
google()
mavenCentral()
jcenter()
maven(url = "https://kotlin.bintray.com/kotlinx/") // soon will be just jcenter()
}
}