2014-05-23 38 views
25

Tôi đang thiết lập ProGuard cho dự án Android của mình. Dự án của tôi cũng sử dụng GSON.ProGuard dành cho Android và GSON

Tôi đã nghiên cứu cấu hình ProGuard để tương thích với GSON và Android và đi qua ví dụ này được cung cấp bởi google-gson https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg.

ProGuard cấu hình sao chép dưới đây:

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

Câu hỏi:

  1. Tôi thấy rằng tập tin này chưa được cập nhật kể từ năm 2011, là nó vẫn khuyến khích sử dụng? Tôi hỏi vì Android/GSON đã thay đổi một chút kể từ đó nên tôi không biết có bao nhiêu điều trên không cần thiết hoặc không chính xác.

  2. Nếu điều này không được khuyến nghị, có cấu hình ProGuard mới được đề xuất cho GSON trong Android không?

Trả lời

56

Tôi cho rằng hầu hết các cài đặt bạn đã có đã được bao gồm trong SDK Android theo mặc định.

Vì vậy, bạn có thể xóa hầu hết trong số họ, chỉ cần thoát khỏi phần dành riêng cho GSON.


Tôi đang phát triển trong Eclipse bằng cách sử dụng Công cụ SDK Android 22.6.3 & bất kỳ phiên bản nào của ProGuard kèm theo.

Đây là những gì tôi đang sử dụng cho 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.** { *; } 

Nó trông giống hệt như những gì bạn có, ngoại trừ tôi không cần dòng về chú thích.


Bạn có thể thấy tôi đã nhận xét một số lớp tôi đã tự thêm vào. Nếu bạn tuần tự hóa/deserialize các lớp học của riêng bạn, bạn cần phải khai báo chúng ở đây thay cho tham chiếu đến mypersonalclass.data.model. Điều này thực sự quan trọng, vì bạn không muốn ProGuard làm xáo trộn tên trường hoặc tên lớp mà GSON sử dụng để tuần tự hóa.

Tôi luôn để lại các loại nhận xét đó ở đó, vì vậy tôi biết cách định cấu hình thư viện hoặc ứng dụng tiếp theo.

+5

Đây là địa điểm từ đó: http://google-gson.googlecode.com/svn/trunk/examples/android-proguard-example/proguard.cfg – caw

+1

Sau khi tìm kiếm nhiều QA .. Tôi đã liên hệ đây. Nó làm tôi vui. Cảm ơn Richard. một lưu ý, "mypersonalclass.data.model" điều này không bắt buộc đối với tôi. Chỉ kiểm tra thôi. – Noundla

+0

@Marco bao gồm liên kết của bạn trong câu trả lời, thx –

0

Trong trường hợp của tôi, tôi đã thêm ở trên nhưng vẫn gặp lỗi cho đến khi ở cấp độ ứng dụng của tôi, tôi đã thay đổi compile 'org.immutables:gson:2.4.6' thành provided 'org.immutables:gson:2.4.6'. Có thể ai đó khai ngộ hơn có thể giải thích tại sao nhưng điều này giải quyết được vấn đề của tôi.