适用于Android和GSON的ProGuard

np8igboo  于 2023-05-12  发布在  Android
关注(0)|答案(5)|浏览(141)

我正在为我的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配置复制如下:

##---------------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已经改变了很多,所以我不知道有多少以上在不必要或不正确的。
1.如果不建议这样做,那么Android中是否有针对GSON的新建议ProGuard配置?

j9per5c4

j9per5c41#

我想你的大多数设置都已经默认包含在Android SDK中了。
因此,您可以删除其中的大部分,只需要保留在专门介绍GSON的部分。
我在Eclipse中使用AndroidSDKTools22.6.3进行开发&无论ProGuard附带哪个版本。
下面是我在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.** { *; }

它看起来和你的完全一样,除了我不需要关于注解的那一行。
你可以看到我已经注解掉了一些我自己添加的类。如果你序列化/反序列化你自己的类,你需要在这里声明它们来代替对mypersonalclass.data.model的引用。这非常重要,因为您不希望ProGuard混淆GSON用于序列化的字段或类名。
我总是在那里留下这些类型的注解,所以我知道如何配置下一个库或应用程序。

xpszyzbs

xpszyzbs2#

以前的答案最近对我停止工作,可能是由于Android的一些变化(现在使用R8而不是Proguard)。我现在使用的配置如下(source - GSON examples):

##---------------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
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { <fields>; }

# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * extends com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName <fields>;
}

# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken

##---------------End: proguard configuration for Gson  ----------

我发现,字段由@SerializedName注解的类不必显式列出,除非它们是内部类。

elcex8rz

elcex8rz3#

在我的例子中,我只是使用GSON将JSON反序列化为一个Object。因此,将以下行添加到proguard文件就足够了。

-keep class your.data.object.modals.package.** { <fields>; }
nimxete2

nimxete24#

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

ifmq2ha2

ifmq2ha25#

使用-keep是一种不好的做法,你永远不应该这样做。你几乎从来不想使用-keep;如果确实需要ProGuard规则,通常需要更具体的变体之一
-keepclassmembers-这只保护类的成员不被收缩和混淆。
-keepnames-这允许收缩类和成员,但不允许混淆。也就是说,任何未使用的代码都将被删除。但保留的代码将保留其原始名称。
-keepclassmembernames-删除未使用的类,重命名剩余的类,删除这些类中未使用的成员,但剩余的成员保持其原始名称。
欲了解更多信息,请阅读此
PS -这是我为Gson做的

-keepclassmembernames class rscom.pojo.** { <fields>; }

相关问题