Proguard - do not obfuscate Kotlin data classes

与世无争的帅哥 提交于 2019-12-03 11:17:25

问题


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.** { *; }

回答1:


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.




回答2:


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
)


来源:https://stackoverflow.com/questions/46098682/proguard-do-not-obfuscate-kotlin-data-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!