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.