Kotlinx.serialization: kotlin Multiplatform module: build errors

Created on 30 Jan 2018  ·  17Comments  ·  Source: Kotlin/kotlinx.serialization

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

question

Most helpful comment

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.

All 17 comments

Seems just library dependency was not recognized in buildscript. Can you check it for errors and post it here?

The dependency tree by gradle is the following:

+--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.21
\--- org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.4
     \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.10 -> 1.2.21

IntelliJ shows them as known but gradlew throws those errors..
However in the tests of the jvm-target kotlinx isn't known . Only when adding an additional dependency to kotlinx-serialization-runtime there. It looks like the common dependency isn't grabbed correctly.

I setup a minimal testproject here: https://github.com/daberni/kotlinx.serialization_test

Another issue which came up is, that Mapper isn't recognizing @Optional correctly. Parsing JSON works successfully, but working with a map where the key is missing doesn't work.

Yes, this is how multiplatform build works for now: you should specify both dependency on common part of the library in your common module and dependency on platform part of the library in your platform module – just as same as deps on kotlin-stdlib and kotlin-stdlib-common

Current Mapper implementation assumes that it can just request all properties from the map. It likely will be changed in future.

Thank you so far for the update.

When I specify the additional dependency in the jvm target and run the tests from the referenced project I get the following runtime exceptions:

kotlinx.serialization.SerializationException: Can't locate companion serializer for class class SomeDataClass (Kotlin reflection is not available)

    at kotlinx.serialization.SerializationKt.serializer(Serialization.kt:25)
    at SampleTest.otherTest(SampleTest.kt:54)

I am not sure if the exception is a bit misleading as I assumed that reflection isn't required and Serializers are generated at compile time by the plugin or am I still missing something?

Steps are reproducible in the given repo.

You need to apply plugin: 'kotlinx-serialization' in platform buildscript too

Thanks 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 😉

You're welcome! Any help would be appreciated :)

I suppose I can close this now.

You need to apply plugin: 'kotlinx-serialization' in platform buildscript too

@sandwwraith Do you mean to add that to the build.gradle at the project root folder?

@hlung No. but for every apply plugin: 'kotlin' or 'kotlin-platform-xxx' or 'kotlin-multiplatform'.

If anyone else runs into this issue make sure have enableFeaturePreview('GRADLE_METADATA') in settings.gradle

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.

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 think it's better way to add import kotlinx.serialization.Serializable instead importing all content of kotlinx.serialization
at least it works for me

I receive Cannot access 'Serializable': it is internal in 'kotlin.io too. If i try to add apply plugin: 'kotlinx-serialization i receive:Plugin with id 'kotlinx-serialization' not found. solved by migrating gradle to kotlin dsl and thanks to @matthiaswelz

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.

@fisiodes Try to add import kotlinx.serialization.* to your file

@sandwwraith yes thanks i already solved the problem with that import and migrating gradle to kotlin dsl as i wrote above. Without migrating to kotlin dsl plugin was not found

Was this page helpful?
0 / 5 - 0 ratings