ProGuard的Andr​​oid和GSON [英] ProGuard for Android and GSON

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

问题描述

我设立的ProGuard我的Andr​​oid项目。我的项目也使用GSON。

我已经研究ProGuard的配置与GSON和Android的兼容性和碰到这个例子中由谷歌,GSON <一个提供href="https://$c$c.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg">https://$c$c.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg.

下面

ProGuard的配置复制:

  ## ---------------开始:常见ProGuard的配置为所有Android应用程序----------
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dont preverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-optimizations!code /简/算术,!现场/ * ,!类/合并/ *

-allowaccessmodification
-keepattributes *注释*
-renamesourcefileattribute的SourceFile
-keepattributes的SourceFile,LineNumberTable
-repackageclasses''

-keep公共类*扩展android.app.Activity
-keep公共类*扩展android.app.Application
-keep公共类*扩展android.app.Service
-keep公共类*扩展android.content.BroadcastReceiver
-keep公共类*扩展android.content.ContentProvider
-keep公共类*扩展android.app.backup.BackupAgentHelper
-keep公共类*扩展的Andr​​oid。preference。preference
-keep公共类com.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService

#明确preserve所有序列化的成员。 Serializable接口
#只是一个标记接口,这样就不会保存。
-keepclassmembers类*实现了java.io.Serializable {
    静态最后长的serialVersionUID;
    私有静态最后java.io.ObjectStreamField [] serialPersistentFields;
    私人无效的writeObject(java.io.ObjectOutputStream中);
    私人无效的readObject(java.io.ObjectInputStream中);
    java.lang.Object中writeReplace();
    java.lang.Object中的readResolve();
}

#preserve所有本地方法的名称及其类的名称。
-keepclasseswithmembernames类* {
    天然的LT;方法&gt ;;
}

-keepclasseswithmembernames类* {
    公共和LT; INIT&GT;(android.content.Context,android.util.AttributeSet);
}

-keepclasseswithmembernames类* {
    公共和LT; INIT&GT;(android.content.Context,android.util.AttributeSet,INT);
}

内部类 -  [R类的#preserve静态字段可能被访问
#通过内省。
-keepclassmembers类** R $ * {
  公共静态&LT;领域取代;
}

#preserve所需的所有枚举类特殊的静态方法。
-keepclassmembers枚举* {
    公共静态** []值();
    公共静态**的valueOf(java.lang.String中);
}

-keep公共类* {
    公众保护*;
}

-keep类*实现android.os.Parcelable {
  公共静态最终android.os.Parcelable $造物主*;
}
## ---------------结束:适用于所有Android应用程序的ProGuard配置----------

## ---------------开始:ProGuard的配置GSON ----------
#GSON使用带有字段时,存储在一个类文件泛型类型的信息。 Proguard的
#删除默认情况下这些信息,因此其配置,以保持它的全部。
-keepattributes签名

#对于使用GSON @expose注释
-keepattributes *注释*

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

将被序列化#应用程序类/解串过GSON
-keep类com.google.gson.examples.android.model ** {*。 }

## ---------------结束:为GSON ProGuard的配置----------
 

问题:

  1. 我看到这个文件还没有从2011年更新,它仍然推荐使用?我问,因为安卓/ GSON自那时以来改变了不少,所以我不知道有多少以上的不必要的或不正确的。

  2. 如果不建议这样做,是有GSON在Android的一个新的推荐ProGuard的配置?

解决方案

我想大多数的这些你那里已经包含在Android SDK默认设置。

所以,你可以去除大部分,只留下致力于GSON的部分。


我正在开发使用Eclipse中的Andr​​oid SDK工具22.6.3和放大器;任何版本的ProGuard的附带了。

下面是我使用的GSON 2.2.4什么:

  ## ---------------开始:ProGuard的配置GSON ----------
#GSON使用带有字段时,存储在一个类文件泛型类型的信息。 Proguard的
#删除默认情况下这些信息,因此其配置,以保持它的全部。
-keepattributes签名

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

将被序列化#应用程序类/解串过GSON
#-keep类mypersonalclass.data.model ** {*。 }
 

它看起来完全一样,你有什么,但我不需要对注释行。


您可以看到我已经注释掉了,我自己添加一些类。如果您序列化/反序列化自己的类,你需要在此申报。

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

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:

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

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

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

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