首页 > 技术文章 > Gson 防止被混淆

shiyeguiji 2017-01-17 18:49 原文

##---------------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  ----------


这是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();
}

-keep public class * implements java.io.Serializable {*;}
2.可以在proguard中 强制使所有混淆失效
-dontobfuscate
-dontoptimize
转载:http://blog.sina.com.cn/s/blog_4e1e357d0101ednf.html

推荐阅读