让 kotlinx 序列化在多平台项目中工作

Getting kotlinx serialization working in multiplatform project

我正在按照 github 上的教程在 Kotlin 1.4-M2 的多平台项目中使用 Kotlin 序列化进行测试,但我没有得到要编译的序列化位。

这是我的build.gradle.kts

plugins {
    val kotlinVersion = "1.4-M2"
    kotlin("multiplatform") version kotlinVersion
    kotlin("plugin.serialization") version kotlinVersion
}
repositories {
    mavenCentral()
    maven {
        url = uri("https://dl.bintray.com/kotlin/kotlin-eap")
    }
    maven {
        url = uri("https://kotlin.bintray.com/kotlinx")
    }
    jcenter()
    gradlePluginPortal()
}
kotlin {
    jvm {
        compilations.all {
            kotlinOptions.jvmTarget = "11"
        }
    }
    js(IR) {
        moduleName = "hotel"
        browser {
            dceTask {
                keep(
                   ...
                )
            }
            binaries.executable()
        }
    }

    sourceSets {

        // val serializationVersion = "0.20.0-1.4-M2"
        val serializationVersion = "0.20.0"

        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializationVersion")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val jvmMain by getting {
            dependencies {
                implementation(kotlin("stdlib-jdk8"))
                implementation(kotlin("reflect"))
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion")
            }
        }
        val jvmTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
            }
        }
        val jsMain by getting {
            dependencies {
                implementation(kotlin("stdlib-js"))
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serializationVersion")
            }
        }
        val jsTest by getting {
            dependencies {
                implementation(kotlin("test-js"))
            }
        }
        all {
            languageSettings.enableLanguageFeature("InlineClasses")
        }
    }
}


我在一个简单的数据上试过了class


@Serializable
data class Test(

    val blah: Int = 0

)
import kotlinx.serialization.json.Json <<<--- Unresolved reference: kotlinx
import kotlinx.serialization.json.JsonConfiguration <<<--- Unresolved reference: kotlinx
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport

...

fun main() {
    val json = Json(JsonConfiguration.Default)
    val jstring = json.toJson(Test.serializer(), Test(blah = 3))
    println(jstring.toString())
}

正在抱怨Unresolved reference: kotlinx

是否需要做一些特定的事情才能使 kotlinx 导入工作,或者我应该使用不同版本的序列化程序库?

我在 Slack 上得到了一些帮助,谢谢谢尔盖!

https://kotlinlang.org/eap/ shows the versions compatible with the EAP or Milestone releases. You should use the serialization runtime version 0.20.0-1.4-M2. Note that with this version, you need to add a single dependency on kotlinx-serialization-runtime in the commonMain source set, not separate dependencies on kotlinx-serialization-runtime-common and the platform parts. See the Specifying dependencies only once section here: https://blog.jetbrains.com/kotlin/2020/06/kotlin-1-4-m2-released

简而言之,我的插件应该与我的 Kotlin 版本相匹配

plugins {
    val kotlinVersion = "1.4-M2"
    kotlin("multiplatform") version kotlinVersion
    kotlin("plugin.serialization") version kotlinVersion
}

然后在 sourceSets 下,我应该使用单个依赖项而不是每个平台一个

    sourceSets {

        val serializationVersion = "0.20.0-1.4-M2"

        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion")
            }
        }

JVM 和 JS Main 不应有任何序列化插件,因此应删除这些行

        val jvmMain by getting {
            dependencies {
                implementation(kotlin("stdlib-jdk8"))
                implementation(kotlin("reflect"))
                // implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion") <<-- remove this

        val jsMain by getting {
            dependencies {
                implementation(kotlin("stdlib-js"))
                // implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serializationVersion") <<-- remove this
            }
        }