让Kotlinx序列化在多平台项目中工作 [英] Getting kotlinx serialization working in multiplatform project
问题描述
按照github上的教程,我正在Kotlin 1.4-M2的多平台项目中对Kotlin序列化进行测试驱动,但是我没有获得要编译的序列化位.
I'm taking Kotlin seriazliation for a test drive in a multiplatform project in Kotlin 1.4-M2, following the tutorial on github, but i'm not getting the serialization bit to compile.
这是我的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")
}
}
}
我已经在一个简单的数据类上尝试过
I've tried it on a simple data 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导入正常工作?或者我应该使用不同版本的序列化程序库吗?
Is there something specific one needs to do to make kotlinx imports work or should i be using different versions of the serializer libraries?
推荐答案
我在Slack上获得了一些帮助,谢谢谢尔盖(Sergey)!
I got some help on Slack, thanks Sergey!
https://kotlinlang.org/eap/显示了与EAP兼容的版本 或里程碑版本.您应该使用序列化运行时 版本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已发布
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版本匹配
So in short, my plugin should be matching my Kotlin version
plugins {
val kotlinVersion = "1.4-M2"
kotlin("multiplatform") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion
}
然后在sourceSets下,我应该使用单个依赖关系,而不是每个平台一个
then under sourceSets, i should be using a single dependency instead of one per platform
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应该没有任何序列化插件,因此应删除这些行
JVM and JS Main should not have any serialization plugin, so those lines should be removed
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
}
}
这篇关于让Kotlinx序列化在多平台项目中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!