Skip to content

Commit 2a71f31

Browse files
committed
Merge remote-tracking branch 'origin/master' into dev
2 parents b211c29 + f6a3432 commit 2a71f31

File tree

3 files changed

+84
-13
lines changed

3 files changed

+84
-13
lines changed

README.md

Lines changed: 72 additions & 9 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.** {
@@ -190,17 +240,30 @@ to add this to your `proguard-rules.pro`:
190240
kotlinx.serialization.KSerializer serializer(...);
191241
}
192242
243+
# Application rules
244+
193245
# Change here com.yourcompany.yourpackage
194-
-keep,includedescriptorclasses class com.yourcompany.yourpackage.**$$serializer { *; } # <-- change package name to your app's
195-
-keepclassmembers class com.yourcompany.yourpackage.** { # <-- change package name to your app's
246+
-keepclassmembers @kotlinx.serialization.Serializable class com.yourcompany.yourpackage.** {
247+
# lookup for plugin generated serializable classes
196248
*** Companion;
249+
# lookup for serializable objects
250+
*** INSTANCE;
251+
kotlinx.serialization.KSerializer serializer(...);
197252
}
198-
-keepclasseswithmembers class com.yourcompany.yourpackage.** { # <-- change package name to your app's
253+
# lookup for plugin generated serializable classes
254+
-if @kotlinx.serialization.Serializable class com.yourcompany.yourpackage.**
255+
-keepclassmembers class com.yourcompany.yourpackage.<1>$Companion {
199256
kotlinx.serialization.KSerializer serializer(...);
200257
}
201-
```
202258
203-
You may also want to keep all custom serializers you've defined.
259+
# Serialization supports named companions but for such classes it is necessary to add an additional rule.
260+
# 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`.
262+
-keep class com.yourcompany.yourpackage.SerializableClassWithNamedCompanion$$serializer {
263+
*** INSTANCE;
264+
}
265+
```
266+
</details>
204267

205268
### Multiplatform (Common, JS, Native)
206269

docs/polymorphism.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ We can update the previous example and turn `Project` superclass into an interfa
367367
mark an interface itself as `@Serializable`. No problem. Interfaces cannot have instances by themselves.
368368
Interfaces can only be represented by instances of their derived classes. Interfaces are used in the Kotlin language to enable polymorphism,
369369
so all interfaces are considered to be implicitly serializable with the [PolymorphicSerializer]
370-
strategy. We just need to mark thier implementing classes as `@Serializable` and register them.
370+
strategy. We just need to mark their implementing classes as `@Serializable` and register them.
371371

372372
<!--- INCLUDE
373373
import kotlinx.serialization.modules.*

formats/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ This library allows serialization and deserialization of objects into and from A
9595

9696
### XML
9797
* GitHub repo: [pdvrieze/xmlutil](https://github.com/pdvrieze/xmlutil)
98-
* Artifact ID: `net.devrieze:xmlutil-serialization`
98+
* Artifact ID: `io.github.pdvrieze.xmlutil:serialization`
9999
* Platform: JVM, Android, JavaScript
100100

101101
This library allows for reading and writing of XML documents with the serialization library.
102102
It is multiplatform, but as the xmlutil library (which handles the multiplatform xml bit)
103103
delegates to platform specific parsers each platform needs to be implemented for each platform
104-
specifically. The library is designed to handle existing formats that use features that would
104+
specifically. The library is designed to handle existing xml formats that use features that would
105105
not be available in other formats such as JSON.
106106

107107
### YAML
@@ -135,4 +135,12 @@ Allow serialization and deserialization of objects to and from [CBOR](https://cb
135135
* Artifact ID: `com.github.dimitark:kotlinx-serialization-ion`
136136
* Platform: JVM
137137

138-
Allow serialization and deserialization of objects to and from [Amazon Ion](https://amzn.github.io/ion-docs/). It stores the data in a flat binary format. Upon destialization, it retains the references between the objects.
138+
Allow serialization and deserialization of objects to and from [Amazon Ion](https://amzn.github.io/ion-docs/). It stores the data in a flat binary format. Upon destialization, it retains the references between the objects.
139+
140+
### android.os.Bundle
141+
142+
* GitHub repo: [AhmedMourad0/bundlizer](https://github.com/AhmedMourad0/bundlizer)
143+
* Artifact ID: `dev.ahmedmourad.bundlizer:bundlizer-core`
144+
* Platform: Android
145+
146+
Allow serialization and deserialization of objects to and from [android.os.Bundle](https://developer.android.com/reference/android/os/Bundle).

0 commit comments

Comments
 (0)