ProGuard for Android和GSON

我正在为我的Android项目设置ProGuard。 我的项目也使用GSON。

我已经研究了ProGuardconfiguration,以便与GSON和Android兼容,并且遇到了这个由google-gson提供的示例https://code.google.com/p/google-gson/source/browse/trunk/examples/androidproguard– example / proguard.cfg 。

ProGuardconfiguration复制如下:

##---------------Begin: proguard configuration common for all Android apps ---------- -optimizationpasses 5 -dontusemixedcaseclassnames -dontskipnonpubliclibraryclasses -dontskipnonpubliclibraryclassmembers -dontpreverify -verbose -dump class_files.txt -printseeds seeds.txt -printusage unused.txt -printmapping mapping.txt -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* -allowaccessmodification -keepattributes *Annotation* -renamesourcefileattribute SourceFile -keepattributes SourceFile,LineNumberTable -repackageclasses '' -keep public class * extends android.app.Activity -keep public class * extends android.app.Application -keep public class * extends android.app.Service -keep public class * extends android.content.BroadcastReceiver -keep public class * extends android.content.ContentProvider -keep public class * extends android.app.backup.BackupAgentHelper -keep public class * extends android.preference.Preference -keep public class com.android.vending.licensing.ILicensingService -dontnote com.android.vending.licensing.ILicensingService # 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(); } # Preserve all native method names and the names of their classes. -keepclasseswithmembernames class * { native <methods>; } -keepclasseswithmembernames class * { public <init>(android.content.Context, android.util.AttributeSet); } -keepclasseswithmembernames class * { public <init>(android.content.Context, android.util.AttributeSet, int); } # Preserve static fields of inner classes of R classes that might be accessed # through introspection. -keepclassmembers class **.R$* { public static <fields>; } # Preserve the special static methods that are required in all enumeration classes. -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } -keep public class * { public protected *; } -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; } ##---------------End: proguard configuration common for all Android apps ---------- ##---------------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. 我看到这个文件自2011年以来没有更新过,是否仍然推荐使用? 我问,因为Android / GSON已经改变了很多,所以我不知道以上多less不必要或不正确。

  2. 如果不推荐这样做,那么在Android中是否有一个新的推荐的ProGuardconfiguration用于GSON?

Solutions Collecting From Web of "ProGuard for Android和GSON"

我想大多数那些设置默认已经包含在Android SDK中。

所以你可以删除其中的大部分,只是留在专用于GSON的部分。


我正在Eclipse中使用Android SDK Tools 22.6.3进行开发,无论哪种版本的ProGuard。

下面是我用于GSON 2.2.4( 按照他们的例子 ):

 ##---------------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 # 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 mypersonalclass.data.model.** { *; } 

它看起来完全一样,除了我不需要关于注释的一行。


你可以看到我已经注释了一些我自己添加的类。 如果你序列化/反序列化你自己的类,你需要在这里声明它们来代替对mypersonalclass.data.model的引用。 这非常重要,因为您不希望ProGuard混淆GSON用于序列化的字段或类名称。

我总是在那里留下这些types的评论,所以我知道如何configuration下一个库或应用程序。

在我的情况下,我添加了上面,但仍然得到一个错误,直到在我的应用程序级别更改compile 'org.immutables:gson:2.4.6' provided 'org.immutables:gson:2.4.6' 。 也许有人更开明可以解释为什么,但这解决了我的问题。