Java 适用于 Android 和 GSON 的 ProGuard
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23826171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ProGuard for Android and GSON
提问by AtariPete
I'm setting up ProGuard for my Android project. My project also uses GSON.
我正在为我的 Android 项目设置 ProGuard。我的项目也使用 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.
我研究了与 GSON 和 Android 兼容的 ProGuard 配置,并遇到了 google-gson https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-示例/proguard.cfg。
ProGuard config copied below:
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 ----------
Questions:
问题:
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.
If this is not recommended, is there a new recommended ProGuard configuration for GSON in Android?
我看到这个文件从2011年就没有更新了,还是推荐使用吗?我问是因为 Android/GSON 从那时起发生了很大的变化,所以我不知道上面有多少不必要或不正确的内容。
如果不建议这样做,Android 中是否有针对 GSON 的新推荐 ProGuard 配置?
采纳答案by Richard Le Mesurier
I think most of those settings you have there are already included in the Android SDK by default.
我认为您拥有的大多数设置默认已包含在 Android SDK 中。
So you can remove most of them, just leaving in the section devoted to GSON.
所以你可以删除其中的大部分,只留在专门介绍 GSON 的部分。
I am developing in Eclipse using Android SDK Tools 22.6.3 & whatever version of ProGuard ships with that.
我正在 Eclipse 中使用 Android SDK Tools 22.6.3 以及随附的任何版本的 ProGuard 进行开发。
Here's what I'm using for GSON 2.2.4 (as per their example):
这是我用于 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 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.
你可以看到我已经注释掉了我自己添加的一些类。如果您序列化/反序列化自己的类,则需要在此处声明它们以代替对mypersonalclass.data.model
. 这非常重要,因为您不希望 ProGuard 混淆 GSON 用于序列化的字段或类名。
I always leave those types of comments in there, so I know how to configure the next library or app.
我总是在那里留下这些类型的评论,所以我知道如何配置下一个库或应用程序。
回答by GotaloveCode
In my case I added the above but still got an error until in my app level gradle I changed compile 'org.immutables:gson:2.4.6'
to provided 'org.immutables:gson:2.4.6'
. Maybe someone more enlightened can explain why but this solved my problem.
就我而言,我添加了上述内容,但仍然出现错误,直到在我的应用程序级别 gradle 中更改compile 'org.immutables:gson:2.4.6'
为provided 'org.immutables:gson:2.4.6'
. 也许更开明的人可以解释原因,但这解决了我的问题。
回答by Kaustubh Bhagwat
Using -keep is a bad practice and you should never do it .You almost never want to use -keep; if you do need a ProGuard rule, you usually want one of the more specific variants
使用 -keep 是一种不好的做法,你永远不应该这样做。你几乎从不想使用 -keep;如果您确实需要 ProGuard 规则,您通常需要更具体的变体之一
-keepclassmembers
- This protects only the members of the class from shrinking and obfuscation.
-keepclassmembers
- 这仅保护类的成员免于缩小和混淆。
-keepnames
- This allows shrinking for classes and members, but not obfuscation. That is, any unused code is going to get removed. But the code that is kept will keep its original names.
-keepnames
- 这允许缩小类和成员,但不允许混淆。也就是说,任何未使用的代码都将被删除。但保留的代码将保留其原始名称。
-keepclassmembernames
- Unused classes are removed, the remaining classes are renamed, unused members of those classes are removed, but then the remaining members keep their original names.
-keepclassmembernames
- 删除未使用的类,重命名剩余的类,删除这些类中未使用的成员,但其余成员保留其原始名称。
For more information please read this
有关更多信息,请阅读此
PS - this is what I did for Gson
PS - 这就是我为 Gson 所做的
-keepclassmembernames class rscom.pojo.** { <fields>; }
回答by Milo? ?ernilovsky
The previous answer stopped working for me recently probably due to some changes in Android (R8 is used now instead of Proguard). The configuration I use now is as follows (source - GSON examples):
上一个答案最近对我不起作用,可能是由于 Android 中的一些变化(现在使用 R8 而不是 Proguard)。我现在使用的配置如下(来源-GSON示例):
##---------------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 * implements 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>;
}
##---------------End: proguard configuration for Gson ----------
I found out that classes whose fields are annotated by @SerializedName do not have to be explicitly listed unless they are inner classes.
我发现那些字段由 @SerializedName 注释的类不必明确列出,除非它们是内部类。