Search code examples
androidjacksonproguardkotlin

JsonProperty not working while minification enabled


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.


Solution

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