2014-05-23 25 views
25

Konfiguruję ProGuarda dla mojego projektu Android. Mój projekt korzysta również z GSON.ProGuard dla Androida i GSON

Sprawdziłem konfiguracje ProGuard pod kątem zgodności z GSON i Androidem i natknąłem się na ten przykład oferowany przez google-gson https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg.

ProGuard config przytoczone poniżej:

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

pytania:

  1. widzę, że ten plik nie został zaktualizowany od 2011 roku, jest nadal zalecane do użytku? Pytam, ponieważ od tamtej pory Android/GSON trochę się zmienił, więc nie wiem, ile powyższego jest niepotrzebne lub niepoprawne.

  2. Jeśli nie jest to zalecane, czy istnieje nowa zalecana konfiguracja ProGuard dla GSON w systemie Android?

Odpowiedz

56

Myślę, że większość ustawień, które tam masz, jest już domyślnie uwzględniona w pakiecie Android SDK.

Możesz usunąć większość z nich, pozostawiając w sekcji poświęconej GSON.


Zajmuję się w Eclipse przy użyciu Android SDK Tools 22.6.3 & niezależnie od wersji PROGUARD statkach o tym.

Oto co używam do 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.** { *; } 

To wygląda dokładnie tak samo jak to, co masz, chyba nie muszę linię o adnotacji.


Możesz zobaczyć Skomentowałem niektóre zajęcia, które sam dodałem. Jeśli serializujesz/deserializujesz własne klasy, musisz zadeklarować je tutaj w miejsce odwołania do mypersonalclass.data.model. Jest to bardzo ważne, ponieważ nie chcesz, aby ProGuard zaciemniał pola lub nazwy klas, których GSon używa do serializacji.

Zawsze zostawiam tam te typy komentarzy, więc wiem, jak skonfigurować następną bibliotekę lub aplikację.

+5

Oto, skąd pochodzi: http://google-gson.googlecode.com/svn/trunk/examples/android-proguard-example/proguard.cfg – caw

+1

Po przeszukaniu wielu pytań. tutaj. To sprawia, że ​​jestem szczęśliwy. Dzięki Richard. Jedna uwaga, "mypersonalclass.data.model" to nie jest wymagane dla mnie. po prostu sprawdź. – Noundla

+0

@Marco zawarł twój link w odpowiedzi, thx –

0

W moim przypadku dodałem powyższe, ale nadal mam błąd, dopóki na moim poziomie aplikacji nie zmieniłem compile 'org.immutables:gson:2.4.6' na provided 'org.immutables:gson:2.4.6'. Może ktoś bardziej oświecony może wyjaśnić, dlaczego to rozwiązało mój problem.