2013-05-15 61 views
7

我在我的應用程序中使用Gson,爲此,我使用一些名稱與使用Json相同的類。我的應用程序運行良好,但編寫proguard時,應用程序崩潰,我猜一些類正在縮小。 我的錯誤是:使用GSON時的Proguard問題

java.lang.ClassCastException:com.google.gson.internal.StringMap不能轉換到com.sample.package.GsonClass

+0

這似乎並不ProGuard的錯誤... –

+0

我用了proguard的試了一下,效果很好 – Jithu

回答

18

你需要將這些行添加到您的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 


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

這裏是鏈接到頭部修改該文件:http://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg –

+1

-keep class com.google.gson.examples。 android.model。** {*; } 它適用於我,但是當我在apk上應用反向engg時,我得到了所有模型類中沒有更改的成員,它打破了安全目的,沒有其他選擇,我們也可以混淆模型嗎? – umesh

+1

我還必須添加'-keepattributes *註釋*' –

1

另一個原因是:在使用前

List<T> list = gson.fromJson(jsonString, type); 

你應該建立正確的typeToken,像這樣:

Type type = new TypeToken<List<T>>(){}.getType(); 
List<T> list = gson.fromJson(jsonString,type)