问题
The following class describes a model I read from a .json file using Jackson 2.2.
public class Product {
public String name;
public int width;
}
The Gradle build process invokes ProGuard to shrink and obfuscate the release build. When I start the application the following error message occurs:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "name" (class com.example.b.c),
not marked as ignorable (0 known properties: ])
How can I configure ProGuard or annotate the Product class so that Jackson still works?
回答1:
I have to admin that I simplified the example in my question. The solution to the problem was to keep getters and setters in ProGuard which I had to additionally define in model classes to make Jackson work.
-keep public class com.example.models.Product {
public *** get*();
public void set*(***);
}
回答2:
You can use @jsonProperty annotation
回答3:
In many cases these classes already implement the Serializable interface. It could make sense to keep all those classes implementing this interface depending on your project. If so add this line to you proguard configuration.
-keep class * implements java.io.Serializable
来源:https://stackoverflow.com/questions/19793513/how-to-configure-proguard-to-respect-jackson-model