Hello :slightly_smiling_face: I'd like to try the...
# kotlin-native
k
Hello 🙂 I'd like to try the Quick sample of Serialization with Kotlin native. I have latest 1.3.73 / 0.20.0 of libraries, but cannot figure out how to express gradle dependencies such that I can see the serialization library in my project. Can anyone point me to a working example of Kotlin Native + Serialization?
b
Add serialisation to common source set dependencies
p
https://github.com/touchlab/KaMPKit uses kotlinx.serialization for ktor repsonses, and might work as an example for you.
k
Thanks for the suggestions, I will check! 😄
l
Wow! Lucky me! I was going to ask the same question! 😁
I tried this, but I'm getting e: FlightPath.kt: (3, 16): Unresolved reference: serialization e: FlightPath.kt: (5, 2): Cannot access 'Serializable': it is internal in 'kotlin.io' e: FlightPath.kt: (5, 2): This class does not have a constructor
Copy code
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

val serialization_version = "0.20.0"

plugins {
    kotlin("multiplatform") version "1.3.72"
    kotlin("plugin.serialization") version "1.3.72"
    `maven-publish`
}

repositories {
    mavenCentral()
    mavenLocal()
}

kotlin {
    jvm{
        withJava()
    }

    mingwX86("mingw"){
        compilations["main"].cinterops {
        }
        binaries {
            executable()
        }
    }

    sourceSets["commonMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version")
    }
    sourceSets["commonTest"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-test-common")
        implementation("org.jetbrains.kotlin:kotlin-test-annotations-common")
    }

    // Default source set for JVM-specific sources and dependencies:
    jvm().compilations["main"].defaultSourceSet {
        dependencies {
            implementation(kotlin("stdlib-jdk8"))
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version")
            implementation("net.java.dev.jna:jna:4.5.2")
        }
    }
    // JVM-specific tests and their dependencies:
    jvm().compilations["test"].defaultSourceSet {
        dependencies {
            implementation(kotlin("test-junit"))
        }
    }

    mingwX86().compilations["main"].defaultSourceSet {
        dependencies {
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version")
        }
    }

}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}
Ok. Mingw sourceset was wrong. This one is working
Copy code
val serialization_version = "0.20.0"

plugins {
    kotlin("multiplatform") version "1.3.72"
    kotlin("plugin.serialization") version "1.3.72"
    `maven-publish`
}

repositories {
    mavenCentral()
    mavenLocal()
}

kotlin {
    jvm{
        withJava()
    }

    mingwX86("mingw"){
        binaries {
            executable()
        }
    }


    sourceSets["commonMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version")
    }
    sourceSets["commonTest"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-test-common")
        implementation("org.jetbrains.kotlin:kotlin-test-annotations-common")
    }

    // Default source set for JVM-specific sources and dependencies:
    jvm().compilations["main"].defaultSourceSet {
        dependencies {
            implementation(kotlin("stdlib-jdk8"))
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version")
            implementation("net.java.dev.jna:jna:4.5.2")
        }
    }
    // JVM-specific tests and their dependencies:
    jvm().compilations["test"].defaultSourceSet {
        dependencies {
            implementation(kotlin("test-junit"))
        }
    }

    sourceSets.findByName("mingwMain")?.dependencies {
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version")
    }

}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}