混淆Gson导致无法解析JSON

1.使用Google的Gson解析Jason,需要引入gson-2.24.jar

引入方式修改gradle.build文件

dependencies {
          compile 'com.google.code.gson:gson:2.2.4'
}

编译过程会下载该jar到External Libraries


gson-2.2.4

2.gradle.build release开启混淆

开启混淆模式

3.配置混淆文件proguard-rules.pro

配置不混淆jar配置

打包没问题,运行程序,Gson不能正常使用,Debug模式没问题的,估计混淆的关系
查找原因
这是google官方的proguard的文档,请注意倒数第二行,class 后方到**签名的 这一段包名应该是你所有的java bean 定义的目录(所以自己在写代码时,应该把java bean 单独放在一个包中)。

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

##---------------End: proguard configuration for Gson  ----------

另外附上,
1.Serializable 的配置

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-keepclassmembers class * implements java.io.Serializable { 
static final long serialVersionUID; 
private static final java.io.ObjectStreamField[] serialPersistentFields; 
private void writeObject(java.io.ObjectOutputStream);
 private void readObject(java.io.ObjectInputStream);
 java.lang.Object writeReplace(); 
java.lang.Object readResolve();
}
-keep public class * implements java.io.Serializable {*;}

2.可以在proguard中 强制使所有混淆失效
-dontobfuscate
-dontoptimize

-keep class com.google.gson.examples.android.model.** { *; }这里就是你定义的要解析的对象所在的包名
我程序中定义对象的包名为:com.jishang.yunji.dao.model

解决混淆完整配置

完整配置

参考文

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,428评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,969评论 19 139
  • 声明 这篇文章更多的是做一个整理,内容来自于ProGuard官方文档以及各种博客等,相关文章的链接在参考目录里,感...
    夷陵小祖阅读 3,708评论 0 23
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,959评论 6 342
  • 上个月28岁的生日刚过,本来近段时间幸福到无话可说了,不过还是想好好记录下这一年已过去一半的时光感想,当...
    戴菡纯阅读 424评论 0 1