美文网首页
混淆Gson导致无法解析JSON

混淆Gson导致无法解析JSON

作者: IT枫 | 来源:发表于2016-10-28 12:12 被阅读4710次

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

解决混淆完整配置

完整配置

参考文

相关文章

  • 混淆Gson导致无法解析JSON

    1.使用Google的Gson解析Jason,需要引入gson-2.24.jar 引入方式修改gradle.bui...

  • GSON 解析 JSON

    GSON JSON 介绍 Gson 下载 Gson 解析 和 格式化Gson 格式化Gson 解析 解析asset...

  • Android Gson官方推荐的json解析方式

    导航 XML的三种解析方式 json全面解析和使用 Gson官方推荐的json解析方式 Gson Gson解析是g...

  • 2018-01-11

    Gson解析复杂json数据常用的两种解析方式 Gson gson = new Gson(); 1.gson.fr...

  • Gson解析数据容错率

    增加Gson解析容错率。比如我只需要判断code字段,根据不同状态返回不同json数据,而有些gson 是无法解析...

  • Json解析(使用Gson)

    Json的解析成 java 对象 Gson gson = new Gson(); // 将json 转化成 j...

  • 混淆后json解析出错

    开启混淆后打包,程序出现json解析出错 原因:没有配置忽略要解析的model。(已经配置了gson忽略文件和泛型...

  • Gson混淆,遇到的问题

    Gson的混淆里面要对Gson解析的 自己写的 bean 进行混淆保护。 要不然Gson不识别混淆后的代码,会出现...

  • Gson序列化那些事

    Android开发中会经常解析json,使用retrofit框架常用gson解析json。Gson有一定的容错机制...

  • Json解析方式

    1.传统的JSON解析 1.1 生成Json 1.2 解析Json 2.Gson解析Json 2.1生成Json ...

网友评论

      本文标题:混淆Gson导致无法解析JSON

      本文链接:https://www.haomeiwen.com/subject/qufeuttx.html