Kotlin has this data class that comes really handy like providing automatic toString, equal etc. However if we don't use these functions, will they be optimized away by proguard?
I'm asking because I'm wondering if we should use data class with cautious (e.g.for cases we don't use toString, equal etc, we should not use data class, but instead normal class, even though they are model classes..)
I'm asking because I'm wondering if we should use data class with cautious
Public methods, that override Object's methods (e.g. equals, hashCode, toString) are always kept during shrinking; it is impossible to safely remove them. This is because JVM and some system classes (such as HashMap) may internally call those methods on your objects, and Proguard can not make assumptions about internals of JVM and system classes.
Other public methods, that override base class methods, as well as methods, required to implement abstract class/interface are also never optimized out. It should theoretically be possible to strip them away, but Proguard currently (as of version 5.*) isn't able to do that.
AFAIK, some auto-generated methods in Kotlin data classes (namely, component*() and copy() methods) don't override any base methods, — they are just used by compiler according to their contract. Those methods can be removed during shrinking, provided that nothing uses them. Auto-generated constructors can also be removed if unused.
TL;DR: You can safely process data classes with Proguard, the only caveat is that equals, hashCode and toString are always kept, even if unused.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With