Ata*_*ete 31 java configuration android proguard gson
我正在为我的Android项目设置ProGuard.我的项目也使用GSON.
我已经研究了与GSON和Android兼容的ProGuard配置,并且遇到了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 ----------
Run Code Online (Sandbox Code Playgroud)
问题:
我发现自2011年以来该文件尚未更新,是否仍建议使用?我问,因为Android/GSON从那时起已经发生了很大变化,所以我不知道上面有多少不必要或不正确.
如果不推荐这样做,Android中是否有针对GSON的新推荐ProGuard配置?
Ric*_*ier 69
我认为您在那里设置的大多数设置都默认包含在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.** { *; }
Run Code Online (Sandbox Code Playgroud)
它看起来与你拥有的完全一样,除了我不需要关于注释的线.
你可以看到我已经注释了我自己添加的一些课程.如果序列化/反序列化自己的类,则需要在此处声明它们以代替引用mypersonalclass.data.model.这非常重要,因为您不希望ProGuard混淆GSON用于序列化的字段或类名.
我总是留下那些类型的评论,所以我知道如何配置下一个库或应用程序.
Mil*_*ský 10
上一个答案最近对我不起作用,可能是由于 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 ----------
Run Code Online (Sandbox Code Playgroud)
我发现那些字段由 @SerializedName 注释的类不必明确列出,除非它们是内部类。
就我而言,我只是使用 GSON 将 JSON 反序列化为对象。因此,将以下行添加到 proguard 文件就足够了。
-keep class your.data.object.modals.package.** { <fields>; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24346 次 |
| 最近记录: |