ProGuard for Android和GSON [英] ProGuard for Android and GSON

查看:196
本文介绍了ProGuard for Android和GSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我研究了ProGuard配置与GSON和Android的兼容性,并遇到了google-gson提供的这个例子https://code.google.com/p/google-gson/source /browse/trunk/examples/android-proguard-example/proguard.cfg



下面复制ProGuard配置:

  ## ---------------开始:所有Android应用程序通用的proguard配置--------- -  
-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 *注释*
-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

#显式保留所有序列化成员。 Serializable接口
#只是一个标记接口,因此它不会保存它们。
-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();
}

#保留所有本地方法名称及其类的名称。
-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);
}

#保留可能被访问的R类的内部类的静态字段
#通过内省。
-keepclassmembers class **。R $ * {
public static< fields> ;;
}

#保留所有枚举类中所需的特殊静态方法。
-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 *;
}
## ---------------结束:所有Android应用程序都通用的proguard配置----------

## ---------------开始:Gson的proguard配置----------
#Gson使用存储在类中的泛型类型信息文件。 Proguard
#默认删除这样的信息,所以配置它保留所有。
-keepattributes签名

#用于使用GSON @Expose注释
-keepattributes *注释*

#Gson特定类
-keep类sun.misc.Unsafe {*; }
#-keep class com.google.gson.stream。** {*; }

#将通过Gson序列化/反序列化的应用程序类
-keep class com.google.gson.examples.android.model。** {*; }

## --------------- End:Gson的proguard配置----------

问题


  1. 我看到这个文件自2011年以来没有更新,是否仍然建议使用?我问,因为Android / GSON自那以后改变了很多,所以我不知道上面有多少不必要或不正确。


  2. 如果这是不推荐,在Android中有GSON的新推荐ProGuard配置?



解决方案

p>我认为大多数设置都已经包含在Android SDK默认情况下。



因此,您可以删除其中的大部分,只需在专门的部分到GSON。






我在Eclipse中使用Android SDK Tools开发22.6.3&



以下是我用于GSON 2.2.4()。

  ## ---------------开始:Gson的proguard配置---------- 
#Gson使用泛型类型使用字段时存储在类文件中的信息。 Proguard
#默认删除这样的信息,所以配置它保留所有。
-keepattributes签名

#Gson特定类
-keep class sun.misc.Unsafe {*; }
#-keep class com.google.gson.stream。** {*; }

#将通过Gson序列化/反序列化的应用程序类
#-keep class mypersonalclass.data.model。** {*; }

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






您可以看到我注释掉了一些我自己添加的类。如果你序列化/反序列化你自己的类,你需要在这里声明它代替引用 mypersonalclass.data.model 。这真的很重要,因为你不想让ProGuard模糊GSON用于序列化的字段或类名。



我总是留下那些类型的注释,所以我知道如何配置下一个库或应用程序。


I'm setting up ProGuard for my Android project. My project also uses GSON.

I've researched ProGuard configurations for compatibility with GSON and Android and came across this example offered by google-gson https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg.

ProGuard config copied below:

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

Questions:

  1. I see that this file has not been updated since 2011, is it still recommended for use? I ask because Android/GSON has changed quite a bit since then so I don't know how much of the above in unnecessary or incorrect.

  2. If this is not recommended, is there a new recommended ProGuard configuration for GSON in Android?

解决方案

I think most of those settings you have there are already included in the Android SDK by default.

So you can remove most of them, just leaving in the section devoted to GSON.


I am developing in Eclipse using Android SDK Tools 22.6.3 & whatever version of ProGuard ships with that.

Here's what I'm using for GSON 2.2.4 (as per their example):

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

It looks exactly the same as what you have, except I don't need the line about the annotations.


You can see I've commented out some classes that I added myself. If you serialize/deserialize your own classes, you need to declare them here in place of the reference to mypersonalclass.data.model. This is really important, as you don't want ProGuard to obfuscate the field or class names that GSON uses for serialization.

I always leave those types of comments in there, so I know how to configure the next library or app.

这篇关于ProGuard for Android和GSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆