Proguard causes Jackson error

走远了吗. 提交于 2019-12-12 14:07:33

问题


After enabling proguard rules I faced with the following So error come before sending anything to network.

java.lang.RuntimeException: Unable to convert FormDocTankPermission to RequestBody

caused by

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class FormDocTankPermission and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

My class that I send as @Retrofit.Body look like following:

class FormDocTankPermission  (
        @get:JsonProperty("fuelCardId")
        val fuelCardId: Long,
        @get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
        @get:JsonProperty("validityDate")
        val validityDate: Date
)

I assume that somehow @get:JsonProperty causes ISSUE

I cretea Retrofit like this

Builder()
.[...]
.addConverterFactory(JacksonConverterFactory.create())

Proguard rules for Jackson

# Jackson
-keep class com.fasterxml.jackson.databind.ObjectMapper {
    public <methods>;
    protected <methods>;
}
-keep class com.fasterxml.jackson.databind.ObjectWriter {
    public ** writeValueAsString(**);
}
-keepnames class com.fasterxml.jackson.** { *; }
-dontwarn com.fasterxml.jackson.databind.**

回答1:


Proguard may remove default constructor of custom serializers/deserializers. That rules help me in such case:

-keepclassmembers class ** extends com.fasterxml.jackson.databind.ser.std.** {
   public <init>(...);
}

-keepclassmembers class ** extends com.fasterxml.jackson.databind.deser.std.** {
   public <init>(...);
}



回答2:


If it works properly without proguard, you should try the following pro-guard rules:

**-keepattributes** Signature,\*Annotation\*,EnclosingMethod
(Because jackson uses annotation)

**-keep** class com.fasterxml.jackson.** { *; } (Keep everything under the jackson package)

**-dontwarn** com.fasterxml.jackson.databind.** (Do not throw warning from here)

**-dontwarn** com.fasterxml.jackson.** (Do not throw any kind of warning from here)

**-keep** class org.json.JSONObject.** {** put(java.lang.String,java.util.Map);}

If you have a custom Jsonserializer<> , you have to keep it.

I hope, it helps you.



来源:https://stackoverflow.com/questions/48869659/proguard-causes-jackson-error

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