1

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.

2 Answers2

2

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.

Jing Li
  • 14,547
  • 7
  • 57
  • 69
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.** { *; }
Augusto Carmo
  • 4,386
  • 2
  • 28
  • 61
  • Thank you for your response. I'm not the best at proguard, so I want to use the `Keep` annotation, but for some reason, I can't find it in Kotlin Multiplatform. Is there a specific library I have to include? –  Aug 16 '20 at 12:38
  • I don't think so, but you have to `import androidx.annotation.Keep`. – Augusto Carmo Aug 16 '20 at 12:42
  • Understood. I couldn't even `import androidx.annotation.Keep`. So instead I went with the proguard way, and it worked. Thank you so much for your time and effort. –  Aug 16 '20 at 12:46
  • I am glad that it worked for you. Tip: Try to migrate your project to AndroidX: https://developer.android.com/jetpack/androidx/migrate – Augusto Carmo Aug 16 '20 at 12:48
  • Just a quick question, since my class is in the `commonMain` package, I can't make use of the ` @Keep` annotation, do you know if there is anything similar to that keep annotation? –  Aug 16 '20 at 14:51
  • I've already seen some people making an `interface`, adding it to proguard, and whenever you need some class or enum to not be obfuscated, you make them implement that interface. But migrating to androidx is really a good deal :) – Augusto Carmo Aug 16 '20 at 14:55
  • Awesome, sounds good. Thank you so much once again. –  Aug 16 '20 at 14:58