改造和 Proguard 错误
Retrofit and Proguard error
我在使用带有混淆器的改造时遇到以下异常(注意,没有混淆器一切正常):
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.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
我使用的Retrofit相关的依赖是:
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
为什么会这样?我怎样才能找到问题所在?故障排除的想法将非常有帮助。提前致谢!
检查以确保也为 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 ----------
我在使用带有混淆器的改造时遇到以下异常(注意,没有混淆器一切正常):
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.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
我使用的Retrofit相关的依赖是:
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
为什么会这样?我怎样才能找到问题所在?故障排除的想法将非常有帮助。提前致谢!
检查以确保也为 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 ----------