Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proguard - do not obfuscate Kotlin data classes

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.** { *; } 
like image 402
Mario Kutlev Avatar asked Sep 07 '17 14:09

Mario Kutlev


People also ask

Does ProGuard work on Kotlin?

ProGuard will apply the same obfuscations to Kotlin identifiers in metadata, such as class or member names, to match those in the Java class files. This ensures that, even where the Kotlin metadata is required, no sensitive names remain in the metadata.

Does ProGuard obfuscate code?

You can obfuscate Android code to provide security against reverse engineering. You can use the Android ProGuard tool to obfuscate, shrink, and optimize your code.

How do you use Kotlin ProGuard?

if you use android studio, proguards comes with default. But you should on "Enables code shrinking" and "Enables resource shrinking" options for your code security and code optimization. open your gradile file and check below. Show activity on this post.


2 Answers

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.

like image 100
Mario Kutlev Avatar answered Sep 24 '22 17:09

Mario Kutlev


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 ) 
like image 37
Suleyman Avatar answered Sep 20 '22 17:09

Suleyman