GSON deserialization error with proguard

I have a simple list class as shown below:

public class Foo {
   @Expose
    private ClassA classa;

    @Expose
    private List<ClassB> list;
}

Serialization and deserialization work fine. However, when I mess up my code with ProGurad I get the following exception:

com.google.gson.JsonParseException: Json deserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@406f65 08 could not deserialize the json object [{...}, {...}, ...] given the class java type .util.List

+4
source share
2 answers

You need to configure proguard with GSON in these lines correctly

##---------------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.** { *; }

: https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg

+1

proguard, gson .

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

All Articles