2015-09-27 252 views
3

我使用翻新時使用ProGuard收到以下異常(注意,沒有Proguard的一切工作正常):改造和Proguard的錯誤

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.path.to.my.model 
     at com.path.to.my.callback.onResponse(Unknown Source) 
     at retrofit.ExecutorCallAdapterFactory$ExecutorCallback$1.run(Unknown Source) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5351) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 

我ProGuard的文件如下:

-dontwarn okio.** 
-dontwarn retrofit.** 
-keep class retrofit.** { *; } 
-keepattributes Signature 
-keepattributes Exceptions 

的要改造,我使用相關的依賴關係是:

compile 'com.squareup.retrofit:retrofit:2.0.0-beta1' 
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1' 

這究竟是爲什麼?我怎樣才能找到問題?對想法進行故障排除會很有幫助提前致謝!

回答

4

檢查並確保爲gson配置了proguard。您需要確保與gson一起使用的POJO不會被混淆,註釋也不會被去除。

注意:您應該在下面的示例中將com.google.gson.examples.android.model.** { *; }替換爲您的模型類。

從GSON example proguard配置 -

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