java - Gson fromJson returns null after Proguard
问题描述
We are using Proguard to obfuscate our Java application. We have some POJO class where Gson is used to create this objects at runtime from json
package com.example.app;
public class AppConfiguration {
private String name;
private String title;
private String details;
}
AppConfiguration configuration = new Gson().fromJson(value, AppConfiguration.class);
After obfuscation with the proguard, the configuration return null. I have used the keep option to keep the AppConfiguration, but did not help
Progarud option
-keep class com.example.app.AppConfiguration
解决方案
Gson fromJson expects the POJO class members. So we need to keep all the member variables.(without Proguard obfuscate this attributes). The following proguard keep options keeps all the attributes of the AppConfiguration
Field serialized name
@SerializedName("keyType")
String keyType;
proguard configuration
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}
-keep,allowobfuscation @interface com.google.gson.annotations.**
If you dont have chance to change the class, you can use the following keep options
-keepclassmembers class com.example.app.AppConfiguration {
public protected private *;
#Keep default members & functions
!public !protected !private *;
}
推荐阅读
- apache-kafka - Kafka 无法使用 confluent 命令启动
- sql - 如何在 Oracle SQL 中更新多个重复行的单行
- python-3.x - CSV 搜索和替换特定的行或列
- python - Peewee group_concat/案例问题
- javascript - 模拟同一对象的多个方法时,Sinon 存根返回空响应
- google-chrome - Google Chrome 控制台中的干预错误
- php - PdfParser 或 Pdf2Text 类都无法返回当前转换的数据
- z3 - z3 API 需要太多时间来解决
- c# - Visual Studio 项目无法解析添加为外部的其他项目的符号
- elasticsearch - 每周删除最旧的数据