JsonProperty not working while minification enabled

老子叫甜甜 提交于 2019-12-11 04:47:38

问题


I have the following class

class CodeRequest(@JsonProperty("phone") val phoneNumber: String)

When I send request (using retrofit) with an object of this class as body (while minification is not enabled) everything works and request will be send in this form {"phone": "123"}

But enabling minification with the following proguard-rules.pro will result in a {"phoneNumber": "123"} request body.

# Jackson
-keep class com.fasterxml.jackson.databind.ObjectMapper {
    public <methods>;
    protected <methods>;
}
-keep class com.fasterxml.jackson.databind.ObjectWriter {
    public ** writeValueAsString(**);
}
-keep @com.fasterxml.jackson.annotation.* class * { *; }
-keep @com.fasterxml.jackson.annotation.** class * { *; }
-keep class com.fasterxml.** { *; }
-keepattributes *Annotation*,EnclosingMethod,Signature,Exceptions,InnerClasses
-keep class * {
    @com.fasterxml.jackson.annotation.* *;
}
-keep class * { @com.fasterxml.jackson.annotation.JsonProperty *;}

# Application classes that will be serialized/deserialized over Jackson
-keepclassmembers class my.application.data.models.** { *; }
-keepclassmembers class my.application.domain.network.rest.** { *; }

What's missing here?

Thank you.


回答1:


Found solution a couple of minutes after posting the question. The problem is not with proguard nor jackson, it's that Kotlin erases required data which are stored in kotlin.Metadata. Adding the following rule to proguard fixed the issue:

-keep class kotlin.Metadata { *; }


来源:https://stackoverflow.com/questions/44098227/jsonproperty-not-working-while-minification-enabled

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