Skip to content

Commit f6a3432

Browse files
authored
Easier to configure ProGuard rules (#1721)
These rules only require modification when serializable classed with named companion objects are used, and even then, less than the previous rules.
1 parent b746ba8 commit f6a3432

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

README.md

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,62 @@ dependencies {
175175
176176
### Android
177177

178-
Library should work on Android "as is". If you're using proguard, you need
179-
to add this to your `proguard-rules.pro`:
178+
The library works on Android, but, if you're using ProGuard,
179+
you need to add rules to your `proguard-rules.pro` configuration to cover all classes that are serialized at runtime.
180180

181+
The following configuration keeps serializers for _all_ serializable classes that are retained after shrinking.
182+
Uncomment and modify the last section in case you're serializing classes with named companion objects.
183+
184+
```proguard
185+
# Keep `Companion` object fields of serializable classes.
186+
# This avoids serializer lookup through `getDeclaredClasses` as done for named companion objects.
187+
-if @kotlinx.serialization.Serializable class **
188+
-keepclassmembers class <1> {
189+
static <1>$Companion Companion;
190+
}
191+
192+
# Keep `serializer()` on companion objects (both default and named) of serializable classes.
193+
-if @kotlinx.serialization.Serializable class ** {
194+
static **$* *;
195+
}
196+
-keepclassmembers class <1>$<3> {
197+
kotlinx.serialization.KSerializer serializer(...);
198+
}
199+
200+
# Keep `INSTANCE.serializer()` of serializable objects.
201+
-if @kotlinx.serialization.Serializable class ** {
202+
public static ** INSTANCE;
203+
}
204+
-keepclassmembers class <1> {
205+
public static <1> INSTANCE;
206+
kotlinx.serialization.KSerializer serializer(...);
207+
}
208+
209+
# @Serializable and @Polymorphic are used at runtime for polymorphic serialization.
210+
-keepattributes RuntimeVisibleAnnotations,AnnotationDefault
211+
212+
# Serializer for classes with named companion objects are retrieved using `getDeclaredClasses`.
213+
# If you have any, uncomment and replace classes with those containing named companion objects.
214+
#-keepattributes InnerClasses # Needed for `getDeclaredClasses`.
215+
#-if @kotlinx.serialization.Serializable class
216+
#com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions.
217+
#com.example.myapplication.HasNamedCompanion2
218+
#{
219+
# static **$* *;
220+
#}
221+
#-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept.
222+
# static <1>$$serializer INSTANCE;
223+
#}
224+
```
225+
226+
In case you want to exclude serializable classes that are used, but never serialized at runtime,
227+
you will need to write custom rules with narrower [class specifications](https://www.guardsquare.com/manual/configuration/usage).
228+
229+
<details>
230+
<summary>Example of custom rules</summary>
231+
181232
```proguard
182-
-keepattributes *Annotation*, InnerClasses
183-
-dontnote kotlinx.serialization.AnnotationsKt # core serialization annotations
233+
-keepattributes RuntimeVisibleAnnotations,AnnotationDefault
184234
185235
# kotlinx-serialization-json specific. Add this if you have java.lang.NoClassDefFoundError kotlinx.serialization.json.JsonObjectSerializer
186236
-keepclassmembers class kotlinx.serialization.json.** {
@@ -208,12 +258,12 @@ to add this to your `proguard-rules.pro`:
208258
209259
# Serialization supports named companions but for such classes it is necessary to add an additional rule.
210260
# This rule keeps serializer and serializable class from obfuscation. Therefore, it is recommended not to use wildcards in it, but to write rules for each such class.
261+
-keepattributes InnerClasses # Needed for `getDeclaredClasses`.
211262
-keep class com.yourcompany.yourpackage.SerializableClassWithNamedCompanion$$serializer {
212263
*** INSTANCE;
213264
}
214265
```
215-
216-
You may also want to keep all custom serializers you've defined.
266+
</details>
217267

218268
### Multiplatform (Common, JS, Native)
219269

0 commit comments

Comments
 (0)