41

In my project I use AutoValue for my old model classes. I started using Kotlin and I want to use Data Classes instead of AutoValue. I want to disable the obfuscation for all Data classes in my Data layer but to keep obfuscating the other classes in the package.

Is there a way to do this?

I would expect to have something like this in my Proguard file:

-keepclassmembers data class example.data_layer.** { *; }
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62

4 Answers4

82

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 because it's easy to forget to add it for new classes.

Hopefully in future there will be a way with one ProGuard rule to disable the obfuscation for all Data classes in package without the need to have a sub-package for the model classes.

Danish Ansari
  • 451
  • 1
  • 6
  • 22
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
4

While @Keep annotation works, another option is to add @SerializedName to the properties:

data class SomeDataClass(
    @SerializedName("prop1") val PropertyOne: String, 
    @SerializedName("prop2") val PropertyTwo: Boolean
)
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
Suleyman
  • 2,765
  • 2
  • 18
  • 31
  • 2
    Could you give an explanation of what @SerializedName does and how it fixes the issue? – user1114 Sep 21 '20 at 07:41
  • 2
    This will not work. either @Keep will work or proguard rule needs to be added for telling proguard not to obfuscate data class – silwar Feb 25 '21 at 10:29
3

I'm not sure keeping whole data class is good or not. but if it's requirement, the below proguard rule will work. but, this is just workaround. I suggest without confidence. please consider carefully.

-keepclasseswithmembers class example.data_layer.** {
    public ** component1();
    <fields>;
}

I have an article here with a more detailed explanation: How to make Proguard keep Kotlin data class

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
jeonghyeon kim
  • 833
  • 7
  • 9
  • 3
    Your profile indicates you're associated with the article you have linked. Linking to something you're affiliated with *without disclosing it's yours* is considered spammy on Stack Overflow. See: [What signifies "Good" self promotion?](//meta.stackexchange.com/q/182212) and [some tips and advice about self-promotion](/help/promotion). I've edited your post to resolve this issue, and hope you can take into consideration in future. – Samuel Liew Feb 09 '20 at 08:49
  • 1
    @SamuelLiew Oh.. I didn't know that. I'll definitely follow your teaching in the future. Thanks for your teaching – jeonghyeon kim Feb 09 '20 at 10:21
0

i solve my problem with @Keep annotaion for all model classes that parse api responce data

@Keep

data class MyClass()