anroid 使用 Gson 混淆 遇到的bug 总结

使用Gson了的,在发布包时,如果需要对包进行混淆,那么必须加入如下配置
这里有一点比较坑爹,从google android_proguard_example 的proguard.cfg上考下来的文件必须做一定的修改:


##---------------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  ----------
//-----------------------经测试确实可以,比如这里我的,-keep class com.example.bean.** { *; }

这是google官方的proguard的文档,请注意倒数第二行,class 后方到**签名的 这一段包名应该是你所有的java bean 定义的目录(所以自己在写代码时,应该把java  bean 单独放在一个包中)。

另外附上,
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(); } //-----------------------经测试确实可以,实现了两个Activity传递经过混淆的Serializable

-keep public class * implements java.io.Serializable {*;}
2.可以在proguard中 强制使所有混淆失效
-dontobfuscate
-dontoptimize

你可能感兴趣的:(anroid 使用 Gson 混淆 遇到的bug 总结)