9

I am using Gson in my application and for that, i am using some classes with name as same as the one in using Json. My application works well, But while writing proguard, application crashes, I guess some of the classes are shrinking. my error is :

java.lang.ClassCastException: com.google.gson.internal.StringMap cannot be cast to com.sample.package.GsonClass

Jithu
  • 1,478
  • 1
  • 13
  • 21

2 Answers2

22

You need to add these lines to your proguard so that gson class is kept while signing your app..

##---------------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  ----------
Arun C
  • 9,035
  • 2
  • 28
  • 42
  • 1
    here is the link to the head revision of that file: http://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg – Richard Le Mesurier Mar 03 '14 at 16:12
  • 1
    -keep class com.google.gson.examples.android.model.** { *; } it works for me but when i applied reverse engg on apk, i got all the model classes with not change in members, it breaks the security purpose, isn't there any other option, we can obfuscate model too ? – umesh Apr 30 '14 at 10:30
  • 1
    I also had to add `-keepattributes *Annotation*` – Matt Accola May 02 '14 at 22:52
  • 1
    I had to add -keepclassmembers class reference.to.your.data.classes.* { *; } – Otziii Aug 25 '20 at 09:30
3

Another reason is: before use

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

you should construct the correct typeToken, like this:

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