0

I have a kotlin multiplatform project and I need to serialize a class. I have included the following dependency in my commonMain and androidMain and iosMain respectively in my multiplatform gradle file:

 //commonMain
 implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.20.0"

 //androidMain
 implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0"

 //iosMain
 implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.20.0"

This is the class I want to serialize:

@Serializable
class Test {

    fun toJsonTestClass() = Json.stringify(Test.serializer(), this)
    var value1:Double = 0.toDouble()
       
    companion object {

        fun buildClass(value1 : Double):Test {

            val test = Test()
            test.value1 = value1
   
            return test
        }

         fun fromJsonTestClass(json:String) = Json.parse(Test.serializer(), json)
    }
}

And in another class (TrialClass), this is how I am testing it:

val test = Test()
val testToJson = test.toJsonTestClass() //<- This is where the error is pointing to.

println(testToJson)

But when I run it, I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: kotlinx/serialization/json/Json
  at Test.toJsonTestClass(Test.kt:28)
  at TrialClass.main(TrialClass.kt:4)
Caused by: java.lang.ClassNotFoundException: kotlinx.serialization.json.Json

I get no issues when having the imports in my class, but when running I get the above mentioned error.

Any help or advice will be highly appreciated.

4

2 回答 2

1

For my case, I did everything right - code & configuration. And gradle clean didn't help, I ended up with the magic Invalidate Caches / Restart from IntelliJ IDEA.

于 2020-11-22T20:12:08.130 回答
0

Your class is probably being obfuscated. You have two options:

Add the @Keep annotation above the class:

@Serializable
@Keep
class Teste {
// ...
}...

or add it to your module's proguard:

-keep (path_to_the_class).test.** { *; }
于 2020-08-16T12:20:25.363 回答