启用缩小功能后,JsonProperty无法正常工作

问题描述:

我有以下课程

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

当我以此类的对象作为主体发送请求(使用改型)时(未启用缩小功能),一切正常,请求将以{"phone": "123"}

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"}

但是使用以下proguard-rules.pro启用缩小功能将导致{"phoneNumber": "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.** { *; }

这里缺少什么?

谢谢.

发布问题几分钟后找到解决方案. 问题与proguardjackson无关,原因是Kotlin删除了存储在kotlin.Metadata中的所需数据. 添加以下规则以保护此问题:

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