Daiwik Daarun
Daiwik Daarun

Reputation: 3974

Retrofit and Proguard error

I am getting the following exception when using retrofit with proguard (note, without Proguard everything works fine):

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)

My proguard file is as follows:

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

The dependencies related to Retrofit that I am using are:

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

Why is this happening? How can I go about finding the problem? Troubleshooting ideas would be very helpful. Thanks in advance!

Upvotes: 3

Views: 1934

Answers (1)

iagreen
iagreen

Reputation: 31996

Check to make sure are have proguard configured for gson, too. You need to make sure POJO used with gson are not obfuscated and annotations are not stripped.

Note: You should replace com.google.gson.examples.android.model.** { *; } with your model classes in the example below.

From the gson example proguard config --

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

Upvotes: 7

Related Questions