Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MinifyEnabled = true strips away Serializable objects #2861

Closed
mahmed1987 opened this issue Nov 18, 2024 · 6 comments
Closed

MinifyEnabled = true strips away Serializable objects #2861

mahmed1987 opened this issue Nov 18, 2024 · 6 comments
Assignees

Comments

@mahmed1987
Copy link

I am attaching a working project in which the issue can be observed.

As per the documentation of this library , we are not to change add or modify the proguard files (if we are not using named companion objects).

That hasn't been my observation .

I am attaching a code sample to demonstrate the issue i obtain with minifyEnabled true. The project zip file is uploaded here.

Just open this project in android studio and run the android app on a device or simulator. The app would crash with the following stacktrace .

image

Adding -keep @kotlinx.serialization.Serializable class * {*;} to the proguard file would fix this situation , but this is against what the docs say

@sandwwraith
Copy link
Member

Why did you post it as a separate issue from #2858 though?

@mahmed1987
Copy link
Author

hey @sandwwraith , I closed that issue because it didn't give satisfactory information in my opinion about the issue at hand .

So I created a sample project to demonstrate the issue and opened a new issue and closed that one .

In hindsight what I could have done is made this more clear by adding a comment on the previous issue between closing that , that I would be opening another issue with more details .

Would remain more vigilant moving forward

@sandwwraith
Copy link
Member

It seems there are some problems in the connection between serializable objects and R8 full mode. This code:

@Serializable
class Klas

@Serializable
object Obj

println(serializer(typeOf<Klas>()))
println(serializer(typeOf<Obj>()))

prints

com.syphyr.kmpplayground.android.Klas$$serializer@424ad69
kotlinx.serialization.internal.ObjectSerializer@5aa608f

when R8 full mode is disabled, and throws

com.syphyr.kmpplayground.android.Klas$$serializer@424ad69

kotlinx.serialization.SerializationException: Serializer for class 'Obj' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.

when R8 full mode is enabled (which is the default for recent versions of AGP).
Perhaps proguard rules inside the library indeed have to be updated.

To workaround this, you can either disable full mode (android.enableR8.fullMode=false in gradle.properties) or apply the -keep @kotlinx.serialization.Serializable class * {*;} rule, which you've already mentioned.

Tested on

agp = "8.6.1"
kotlin = "2.0.20"

@sandwwraith sandwwraith changed the title MinifyEnabled = true strips away Serializable classes ? (Working project attached) MinifyEnabled = true strips away Serializable objects Nov 19, 2024
@mahmed1987
Copy link
Author

@sandwwraith I would raise the issue on android issue tracker channel too

I too believe this must be something to do with the navigation library

@shanshin
Copy link
Contributor

@mahmed1987
could you please check this rule?

-if @kotlinx.serialization.Serializable class **
-keepclassmembers class <1> {
    public static <1> INSTANCE;
    kotlinx.serialization.KSerializer serializer(...);
}

This is a stricter rule compared to -keep @kotlinx.serialization.Serializable class * {*;}, if there are no other errors with it, we will add it to the built-in rules for R8

@shanshin
Copy link
Contributor

shanshin commented Nov 20, 2024

Related R8 issue https://issuetracker.google.com/issues/379996140

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants