Piotr
Piotr

Reputation: 3970

Proguard causes Jackson error

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.**

Upvotes: 5

Views: 2104

Answers (2)

GeRip003
GeRip003

Reputation: 11

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.

Upvotes: 0

chudo xl
chudo xl

Reputation: 221

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>(...);
}

Upvotes: 2

Related Questions