Kotlin / kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Apache License 2.0
5.32k stars 619 forks source link

ProGuard optimization causes "java.lang.VerifyError: Bad type on operand stack" #2719

Closed wtlgo closed 1 week ago

wtlgo commented 2 months ago

Describe the bug

If I try to optimize the application by disabling

# -dontoptimize

I get the following error:

Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    dev/mikchan/misc/ksp/model/Model$$serializer.<clinit>()V @38: putstatic
  Reason:
    Type 'dev/mikchan/misc/ksp/shadow/kotlinx/serialization/descriptors/SerialDescriptor' (current frame, stack[0]) is not assignable to 'dev/mikchan/misc/ksp/shadow/kotlinx/serialization/internal/PluginGeneratedSerialDescriptor'
  Current Frame:
    bci: @38
    flags: { }
    locals: { 'dev/mikchan/misc/ksp/shadow/kotlinx/serialization/internal/PluginGeneratedSerialDescriptor' }
    stack: { 'dev/mikchan/misc/ksp/shadow/kotlinx/serialization/descriptors/SerialDescriptor' }
  Bytecode:
    0000000: bb00 0759 b700 18b3 0014 bb00 1059 1202
    0000010: b200 14c0 000f 04b7 001b 594b 1203 03b6
    0000020: 001c 2ac0 000c b300 15b1               

    at dev.mikchan.misc.ksp.model.Model$Companion.serializer(Model.kt:5)
    at dev.mikchan.misc.ksp.MainKt.main(Main.kt:1014)

Process finished with exit code 1

If I disable the optimization, though, everything works OK.

To Reproduce

Here's a project with reproduction: https://github.com/wtlgo/Kotlin-Serialization-Proguard

Expected behavior

It should work correctly either with or without optimization

Environment

buehlerjochen commented 2 months ago

We have exactly the same issue in our project. Any updates on this?

shanshin commented 2 months ago

Hi, we will try to investigate this problem soon

shanshin commented 2 months ago

@buehlerjochen, @wtlgo try to add this rule to your ProGuard rules file

-keepclassmembers public class **$$serializer {
    private ** descriptor;
}
shanshin commented 2 months ago

Related issue in ProGuard https://github.com/Guardsquare/proguard/issues/415

buehlerjochen commented 2 months ago

@shanshin Sorry for the late response, Just tested you proposal and it seems to solve the issue. Thank you very much!

wtlgo commented 2 months ago

Yeah, works for me too. Thank you! I'm going to remove my reproduction repo, here's a zip of it for archive purposes.