问题
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