I’m using polymorphic serialization in an app, and...
# serialization
l
I’m using polymorphic serialization in an app, and I get a serialization error when I run in release mode, but not debug mode.
The error I get is “l.d.j: Serializer for class ‘c’ is not found. E/AndroidRuntime(27144): Mark the class as @Serializable or provide the serializer explicitly.” I have tried applying the serialization plugin to the final app as well as providing the serializer explicitly, and get the same error.
The class and function that calls Json.encodeToString(strategy, value) are in another library that gets imported as an aar.
e
that sounds like there's something that is trying to look up a serializer at runtime (which is also what happens with
encodeToString<T>(value)
), getting the stacktrace and looking up the original (pre-proguard) classes with retrace should help you narrow down where that is happening
but also the README contains some proguard rules that make it possible for runtime serializer lookup to work, https://github.com/Kotlin/kotlinx.serialization#android
l
I refactored my code to remove encodeToString<T> and use the version passing in a serializer explicitly instead. I was under the impression that this method would not have to look up the type at runtime. Does this apply to polymorphic classes as well?
e
good. until https://github.com/Kotlin/kotlinx.serialization/issues/1348 is resolved, that is the best approach
the reified helper uses reflection currently, due to lack of compiler assistance
l
I seem to get the same error with the encodeToString that passes the serializer explicitly. I wonder if polymorphism causes it to use reflection?
e
serializable sealed classes are registered at compile time
but if you're registering other polymorphic classes via serializersModule, there are reified helpers there that also currently rely on reflection
l
I did make sure that my classes are sealed.
It turned out I forgot to update decodeFromString to use the non-reflective version as well. Fixed that and the code doesn’t crash anymore.