kotlinx.serialization: kotlin Multiplatform module: build errors
I tried using kotlinx.serialization in a kotlin multiplatform project in a commons module with some small model classes looking like the following:
@Serializable
open class Document {
var _id: String? = null
var _rev: String? = null
}
@Serializable
class User : Document() {
var username: String = ""
var email: String = ""
var rollen: List<UserRoles> = emptyList()
companion object {
val TYPE = "User"
}
}
But when building via command-line I get following errors:
Unresolved reference: kotlinx
Cannot access 'Serializable': it is internal in 'kotlin.io'
This class does not have a constructor
Its mentioned in the description that it should work with the kotlinx-serialization-runtime-common dependency which I am using. The plugin should stay the same as far as I read.
I am using kotlin 1.2.21, but already tried 1.2.20, 1.2.10 with the same results. Gradle 4.4.1
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 11
- Comments: 17 (10 by maintainers)
I received the same error message (“Cannot access ‘Serializable’: it is internal in ‘kotlin.io’”), but only because I forgot the
import kotlinx.serialization.*. IntelliJ did not suggest to add this import automatically, so I first assumed some configuration issue. Maybe it helps someone arrivin here via Google.I get rid of that error by
import java.io.Serializable. I hope it helps.You need to
apply plugin: 'kotlinx-serialization'in platform buildscript too@fisiodes Try to add
import kotlinx.serialization.*to your fileThe dependency tree by gradle is the following:
IntelliJ shows them as known but gradlew throws those errors… However in the tests of the jvm-target
kotlinxisn’t known . Only when adding an additional dependency tokotlinx-serialization-runtimethere. It looks like thecommondependency isn’t grabbed correctly.I setup a minimal testproject here: https://github.com/daberni/kotlinx.serialization_test
Another issue which came up is, that
Mapperisn’t recognizing@Optionalcorrectly. Parsing JSON works successfully, but working with a map where the key is missing doesn’t work.If anyone else runs into this issue make sure have
enableFeaturePreview('GRADLE_METADATA')in settings.gradleThanks for the help 👍
It would be nice if the documentation could explain these steps in more detail. Probably I will make a PR for this 😉