Proguard – do not obfuscate Kotlin data classes

To fix the problem I moved the model classes to model package and added new ProGuard rule for the package. -keep class com.company.myfeature.model.** { *; } Another solution would be to use @Keep annotation from support library to disable the obfuscation for the class: @Keep data class MyRequestBody(val value: String) Using @Keep may cause problems … Read more

Property include/exclude on Kotlin data classes

I’ve used this approach. data class Person(val id: String, val name: String) { override fun equals(other: Person) = EssentialData(this) == EssentialData(other) override fun hashCode() = EssentialData(this).hashCode() override fun toString() = EssentialData(this).toString().replaceFirst(“EssentialData”, “Person”) } private data class EssentialData(val id: String) { constructor(person: Person) : this(id = person.id) }

Kotlin data class: how to read the value of property if I don’t know its name at compile time?

Here is a function to read a property from an instance of a class given the property name (throws exception if property not found, but you can change that behaviour): import kotlin.reflect.KProperty1 import kotlin.reflect.full.memberProperties @Suppress(“UNCHECKED_CAST”) fun <R> readInstanceProperty(instance: Any, propertyName: String): R { val property = instance::class.members // don’t cast here to <Any, R>, it … Read more

kotlin data class + bean validation jsr 303

You need to use Annotation use-site targets since the default for a property declared in the constructor is to target the annotation on the constructor parameter instead of the getter (which will be seen by JavaBeans compliant hosts) when there are multiple options available. Also using a data class might be inappropriate here (see note … Read more

Kotlin data class copy method not deep copying all members

The copy method of Kotlin is not supposed to be a deep copy at all. As explained in the reference doc (https://kotlinlang.org/docs/reference/data-classes.html), for a class such as: data class User(val name: String = “”, val age: Int = 0) the copy implementation would be: fun copy(name: String = this.name, age: Int = this.age) = User(name, … Read more

How to extend a data class with toString

Adding a .toString() extension function would not work because: Extension functions can’t take part in virtual calls (they are resolved statically). In other words, extensions cannot override member functions. If there is a matching member function, it is preferred to the extension. If you add an extension function fun Something.toString() = …, then s.toString() won’t … Read more

Equals method for data class in Kotlin

In Kotlin data classes equality check, arrays, just like other classes, are compared using equals(…), which compares the arrays references, not the content. This behavior is described here: So, whenever you say arr1 == arr2 DataClass(arr1) == DataClass(arr2) … you get the arrays compared through equals(), i.e. referentially. Given that, val arr1 = intArrayOf(1, 2, … Read more

Kotlin Data Class from Json using GSON

Data class: data class Topic( @SerializedName(“id”) val id: Long, @SerializedName(“name”) val name: String, @SerializedName(“image”) val image: String, @SerializedName(“description”) val description: String ) to JSON: val gson = Gson() val json = gson.toJson(topic) from JSON: val json = getJson() val topic = gson.fromJson(json, Topic::class.java)

Extend data class in Kotlin

The truth is: data classes do not play too well with inheritance. We are considering prohibiting or severely restricting inheritance of data classes. For example, it’s known that there’s no way to implement equals() correctly in a hierarchy on non-abstract classes. So, all I can offer: don’t use inheritance with data classes.