0

i have a polymorphic type that is implemented by objects and classes.

sealed interface Base 

@Serializable
@SerialName("Sub")
class Sub(...) : Base

@Serializable
@SerialName("Obj")
object Obj : Base

i use this type with kotlinx.serialization

polymorphic(Base::class) {
  subclass(Sub::class)
  subclass(Obj::class)
}

this runs when there is no obfuscation, but when obfuscation is turned on, i get: Serializer for class 'Obj' is not found. Mark the class as @Serializable or provide the serializer explicitly.

my proguard configuration regarding kotlinx.serialization is

-keepclassmembers class kotlinx.serialization.json.** {
    *** Companion;
}
-keepclasseswithmembers class kotlinx.serialization.json.** {
    kotlinx.serialization.KSerializer serializer(...);
}
-keepclasseswithmembers class .** {
    kotlinx.serialization.KSerializer serializer(...);
}

-keep,includedescriptorclasses class my.package.**$$serializer { *; }
Noam Freeman
  • 143
  • 1
  • 8
  • 1
    Does this answer your question? [How to make proguard keep kotlinx serializers for objects?](https://stackoverflow.com/questions/70663076/how-to-make-proguard-keep-kotlinx-serializers-for-objects) – Steven Jeuris Jan 15 '22 at 22:33

1 Answers1

0

The problem is the generated serializer is different for objects and classes. while in classes it is Sub$serializer in objects it is Obj$defaultSerializer$delegate, which slips through the Proguard rules.

While a good solution would be changing the Proguard rules to catch this case,

a usefull hack is to pass the serializer directly, instead of letting the polymorphic builder find it by the class.

polymorphic(Base::class) {
  ..
  subclass(Obj.serializer())
}
Noam Freeman
  • 143
  • 1
  • 8