Using Gson with Proguard Enabled

Using GSON with proguard enabled

Variable names will be obfuscated with proguard, leaving you with something like

private String a;

Instead of

private String descripcionCategoria;

You can add proguard rules so some classes don't get obfuscated. I got away with it using these:

-keepattributes Signature
# POJOs used with GSON
# The variable names are JSON key values and should not be obfuscated
-keepclassmembers class com.example.apps.android.Categorias { <fields>; }
# You can apply the rule to all the affected classes also
# -keepclassmembers class com.example.apps.android.model.** { <fields>; }

If your POJO class name is also used for parsing then you should also add the rule

-keep class com.example.apps.android.model.** { <fields>; }

In your case, annotations are not used, you would need this if you do

# Keep the annotations
-keepattributes *Annotation*

Another way to solve this problem is to use the SerializedName annotation and let the class get obfuscated. For this you will still need the -keepattributes *Annotation* rule.

import com.google.gson.annotations.SerializedName

@SerializedName("descripcionCategoria")
private String descripcionCategoria;

Gson parsing is not working when ProGuard rule enabled

You're missing a rule for ResponseStatus:

-keep class com.yourapp.yourpackage.ResponseStatus { *; }

What's probably happening is that ProGuard is obfuscating the methods and fields of ResponseStatus and when Gson tries to set them their name no longer matches. Remember that you need a -keep class rule for every model class that you use with Gson.

Gson error when enabling proguard

Whether you come across this error, check if you:

1 - Added @SerializedName to your model's attributes. (This helps GSON to find the real name)

2 - Add this configuration to your proguard file:

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

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

##---------------End: proguard configuration for Gson ----------
-keep class com.myapp.data.remote.request.** { *; } # <--- Add your models package here

3- If you keep getting this error check if ALL your models are using the @SerializedName and are in the package included in above rule

ProGuard for Android and GSON

I think most of those settings you have there are already included in the Android SDK by default.

So you can remove most of them, just leaving in the section devoted to GSON.


I am developing in Eclipse using Android SDK Tools 22.6.3 & whatever version of ProGuard ships with that.

Here's what I'm using for GSON 2.2.4 (as per their example):

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

It looks exactly the same as what you have, except I don't need the line about the annotations.


You can see I've commented out some classes that I added myself. If you serialize/deserialize your own classes, you need to declare them here in place of the reference to mypersonalclass.data.model. This is really important, as you don't want ProGuard to obfuscate the field or class names that GSON uses for serialization.

I always leave those types of comments in there, so I know how to configure the next library or app.

Gson Nested classes are null while using proguard

I was able to fix this issue by doing three things to my proguard-rules.pro file:

First, makes sure ProGuard doesn't change the names of any of your custom classes you serialize with Gson. Let's say that you have a class called Classname. To exempt it, add this to your progaurd-rules.pro:

-keepclassmembernames class com.your.package.classname { <fields>; }

(Replace com.your.package.classname with your actual package and class name)

I had to do this for like a dozen classes. Don't forget to exempt any member variables of those classes that are also custom. Exempt inner classes with classname$innerclass instead of classname.

Second, add the rules that the Gson library recommends. They can be found here. Here they are as of writing:

##---------------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
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { <fields>; }

# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * extends com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}

# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken

##---------------End: proguard configuration for Gson ----------

Last, add in these two rules:

-dontwarn java.lang.reflect.**
-keep class kotlin.** { *; }

These are what fixed the nested null issue I had - Of course, after I had done the above steps.

Cannot parse json objects with GSON when setting minifyEnabled is true

I believe that you need to edit your proguard-rules.pro file to include those data classes with this line. You can read more about it here.

-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}

ProGuard makes Gson return LinkedTreeMap instead of my type

Make sure your proguard.cfg contains all of the rules:

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

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

##---------------End: proguard configuration for Gson ----------
-keep public class MyUserClass
-keep public class UserPosts


Related Topics



Leave a reply



Submit