【发布时间】:2021-05-04 04:10:37
【问题描述】:
我的 Android 应用使用 API 获取 JSON 字符串并希望将其转换为自定义对象。
当 minifyEnabled 为 false 时,它运行良好,但是当设置为 true 并制作 APK 时,经过一些测试我发现 Gson 无法将 JSONObject 字符串映射到我的客户对象,并且所有属性都为 null。
JSONObject obj = new JSONObject(result);// result is a string that have got from the API
Gson gson = new Gson();
String v=String.valueOf(obj);
MyObjectModel mobj= gson.fromJson(v,MyObjectModel.class);//Null Execption happen here
String t=goodMoodContentModel.getContentText();//t is null when minifyEnabled is true
为什么会发生这种情况以及如何处理?
更新:
我用这个代码来测试它:
debug {
minifyEnabled true
shrinkResources false
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
【问题讨论】: