Android 使用 GSON 库和 ProGuard 时崩溃

Android crash while using GSON Library and ProGuard

我在 GSON 库中使用以下 Proguard 配置:

# 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.** { *; }

# Keep any class that intereracts with GSON
-keep class com.trifecta.engine.api.** { *; }

但是在测试导出的 apk 时出现错误:

Caused by: java.lang.AbstractMethodError: abstract method 
"void com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$a.a(com.google.gson.stream.JsonWriter, java.lang.Object)"
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:200)
       at com.google.gson.Gson.toJson(Gson.java:546)
       at com.google.gson.Gson.toJson(Gson.java:525)
       at com.google.gson.Gson.toJson(Gson.java:480)
       at com.google.gson.Gson.toJson(Gson.java:460)

我已经在 SO 上研究了这个问题,我遵循了 Proguard 配置来自:[https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg] but still no success. 1

试试这个片段然后告诉我

## GSON 2.2.4 specific rules ##

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

-keepattributes EnclosingMethod

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }