1

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

这是我的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")
        }
    }
}


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


@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 导入工作,或者我应该使用不同版本的序列化程序库?

4

1 回答 1

6

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

https://kotlinlang.org/eap/显示了与 EAP 或 Milestone 版本兼容的版本。您应该使用序列化运行时版本 0.20.0-1.4-M2。请注意,在此版本中,您需要在 commonMain 源集中添加对 kotlinx-serialization-runtime 的单个依赖项,而不是对 kotlinx-serialization-runtime-common 和平台部分的单独依赖项。请参阅此处的“仅指定一次依赖项”部分: 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
            }
        }
于 2020-06-13T09:06:43.947 回答