Has anyone succeeded in using KotlinX Serializatio...
# compose-desktop
p
Has anyone succeeded in using KotlinX Serialization with Proguard optimizations and ofuscation? thread⬇️
The problem seems to be with ofuscations because i've troed without it and it works. The things is that i've also tried lots and lots of proguard rules from the internet and none has succeeded this is my current setup (maybe theres repeated ones)
Copy code
# Keep `Companion` object fields of serializable classes.
# This avoids serializer lookup through `getDeclaredClasses` as done for named companion objects.
-if @kotlinx.serialization.Serializable class **
-keepclassmembers class <1> {
    static <1>$Companion Companion;
}

# Keep `serializer()` on companion objects (both default and named) of serializable classes.
-if @kotlinx.serialization.Serializable class ** {
    static **$* *;
}
-keepclassmembers class <2>$<3> {
    kotlinx.serialization.KSerializer serializer(...);
}

# Keep `INSTANCE.serializer()` of serializable objects.
-if @kotlinx.serialization.Serializable class ** {
    public static ** INSTANCE;
}
-keepclassmembers class <1> {
    public static <1> INSTANCE;
    kotlinx.serialization.KSerializer serializer(...);
}

# @Serializable and @Polymorphic are used at runtime for polymorphic serialization.
-keepattributes RuntimeVisibleAnnotations,AnnotationDefault

-keepattributes *Annotation*, InnerClasses
-dontnote kotlinx.serialization.AnnotationsKt # core serialization annotations
-dontnote kotlinx.serialization.SerializationKt

# Keep Serializers

-keep,includedescriptorclasses class com.package.name.**$$serializer { *; }  
-keepclassmembers class com.package.name.** {  
    *** Companion;
}
-keepclasseswithmembers class com.package.name.** { 
    kotlinx.serialization.KSerializer serializer(...);
}

# When kotlinx.serialization.json.JsonObjectSerializer occurs

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

### <https://github.com/Kotlin/kotlinx.serialization/blob/master/rules/r8.pro>

# Rule to save runtime annotations on serializable class.
# If the R8 full mode is used, annotations are removed from classes-files.
#
# For the annotation serializer, it is necessary to read the `Serializable` annotation inside the serializer<T>() function - if it is present,
# then `SealedClassSerializer` is used, if absent, then `PolymorphicSerializer'.
#
# When using R8 full mode, all interfaces will be serialized using `PolymorphicSerializer`.
#
# see <https://github.com/Kotlin/kotlinx.serialization/issues/2050>

-if @kotlinx.serialization.Serializable class **
 -keep, allowshrinking, allowoptimization, allowobfuscation class <1>

### END

#-keep @kotlinx.serialization.Serializable class * {*;}
-keep @kotlinx.serialization.Serializable class **
ofuscation seems to reduce distributable size so thats why its so important to me
c
There are some
com.package.name
in the ruleset you postet. Those should be replaced with the actual package name of your app.
p
They are🙃 i had to censor it
👍 1
c
Just wanted to get sure 👍🏻
😅 1
m
I configure ProGuard like this:
Copy code
buildTypes.release {
    proguard {
        version.set("7.5.0")
        isEnabled.set(true)
        optimize.set(false)
        obfuscate.set(false)
        configurationFiles.from(project.file("<http://compose-desktop.pro|compose-desktop.pro>"))
    }
}
It’s important to enable it because that accounts for the biggest reduction in code. Optimization and obfuscation don’t contribute that much and are simply not worth the pain they are causing. And as long as Jetbrains is, e.g., still adding a 20 MB Intel library to the ARM code on macs I really don’t see a reason to squeeze out the last bit of code reduction via ProGuard optimization and obfuscation. See: https://youtrack.jetbrains.com/issue/COMPT-3859/Intel-binary-in-ARM-library
👍 1
106 Views