Skip to content

Commit bffb159

Browse files
authored
Bundle kotlinx serialization ProGuard rules into the Compose plugin (#5314)
When users use `androidx.navigation`, they see `@Serialization` annotation they can use, without explictly adding `kotlinx.serialization`. They see, because it is added as an `api` dependency. The Compose Gradle plugin on the other hand provide `./gradlew runRelease` task that uses ProGuard to minify binaries. Because the plugin should support not only Compose, but also all support libraries (androidx, components), we should bundle serialization ProGuard rules into it. Fixes https://youtrack.jetbrains.com/issue/CMP-8050 ## Testing ``` import androidx.compose.ui.window.singleWindowApplication import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController fun main() = singleWindowApplication { NavHost( navController = rememberNavController(), startDestination = LoginRoute() ) { composable<LoginRoute> {} } } sealed interface Route @kotlinx.serialization.Serializable data class LoginRoute(val id: Long? = null) : Route ``` Doesn't crash when run `./gradlew runRelease` ## Release Notes ### Fixes - Desktop - Fix "Serializer for class is not found" using `androidx.navigation` and running `./gradlew runRelease` - `kotlinx.serialization` ProGuard rules are bundled in the Compose Gradle plugin
1 parent 99c2fd6 commit bffb159

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

gradle-plugins/compose/src/main/resources/default-compose-desktop-rules.pro

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
# Kotlinx Coroutines Rules
1515
# https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/resources/META-INF/proguard/coroutines.pro
16-
1716
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
1817
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
1918
-keepclassmembers class kotlinx.coroutines.** {
@@ -68,3 +67,43 @@
6867
-keep,allowshrinking,allowobfuscation class androidx.compose.runtime.SnapshotStateKt__DerivedStateKt { *; }
6968
-keep class androidx.compose.material3.SliderDefaults { *; }
7069
-dontnote androidx.**
70+
71+
# Kotlinx serialization, included by androidx.navigation
72+
# https://github.com/Kotlin/kotlinx.serialization/blob/master/rules/common.pro
73+
-if @kotlinx.serialization.Serializable class **
74+
-keepclassmembers class <1> {
75+
static <1>$* Companion;
76+
}
77+
-keepnames @kotlinx.serialization.internal.NamedCompanion class *
78+
-if @kotlinx.serialization.internal.NamedCompanion class *
79+
-keepclassmembernames class * {
80+
static <1> *;
81+
}
82+
-if @kotlinx.serialization.Serializable class ** {
83+
static **$* *;
84+
}
85+
-keepclassmembers class <2>$<3> {
86+
kotlinx.serialization.KSerializer serializer(...);
87+
}
88+
# Keep `INSTANCE.serializer()` of serializable objects.
89+
-if @kotlinx.serialization.Serializable class ** {
90+
public static ** INSTANCE;
91+
}
92+
-keepclassmembers class <1> {
93+
public static <1> INSTANCE;
94+
kotlinx.serialization.KSerializer serializer(...);
95+
}
96+
-keepattributes RuntimeVisibleAnnotations,AnnotationDefault
97+
-dontnote kotlinx.serialization.**
98+
-dontwarn kotlinx.serialization.internal.ClassValueReferences
99+
-keepclassmembers public class **$$serializer {
100+
private ** descriptor;
101+
}
102+
103+
# Kotlinx serialization, additional rules
104+
# Fixes:
105+
# Exception in thread "main" kotlinx.serialization.SerializationException: Serializer for class 'SomeClass' is not found.
106+
# Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
107+
-keep class **$$serializer {
108+
*;
109+
}

0 commit comments

Comments
 (0)