Proguard problem when using GSON

I use Gson in my application, and for this I use some classes with the same name as when using Json. My application works well, but when writing proguard, application crashes, I think, some of the classes are reduced. my mistake:

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

+7
source share
2 answers

You need to add these lines to your proguard so that the gson class is stored when signing your application.

##---------------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 ---------- 
+18
source

Another reason: before use

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

you must create the correct type of Token, for example:

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

All Articles