LoginSignup
0
0

More than 5 years have passed since last update.

kotlinx.serializationの`serializer()`を使うと、ユニットテスト時に「Unresolved reference: serializer」エラーが出る

Last updated at Posted at 2018-11-01

Environment

  • kotlin_version=1.3.0
  • org.jetbrains.kotlin.native.version=0.9.3
  • kotlinx.serialization=0.8.1-rc13
  • プラグイン
    • kotlin-multiplatform
    • kotlinx-serialization

問題

1 . SharedCode内でkotlinx.serializationのserializer()メソッドを呼び出す。

@Serializable
data class Data(val a: Int, @Optional val b: String = "42")

fun hoge() {
  val dataSerializer = Data.serializer()
}

2 . SharedCode内のユニットテストを実行すると、以下のエラーが発生する。

Error:(22, 51) Kotlin: [SharedCode_androidMain] Unresolved reference: serializer

解決策は今のところわからず

以下のように、::class.serializer()を呼ぶように変更すると、SharedCodeのユニットテスト時はエラーが発生しなくなります。

val dataSerializer = Data::class.serializer()

ただし、iOS(Kotlin/Native)のアプリ実行時に以下のエラーが発生するので、解決策にはなりません。

Uncaught Kotlin exception: kotlin.NotImplementedError: An operation is not implemented: Obtaining serializer from KClass is not available on native due to the lack of reflection. Use .serializer()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0