How to configure ProGuard to respect Jackson model?

笑着哭i 提交于 2020-01-03 03:24:08

问题


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

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