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.
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;
ProGuard for Android and Retrofit2 Converter Gson?
you either want to keep the class you are using with gson or use @SerializedName
annotation.
option 1 (keep class)
// all classes in a package
-keep class com.example.app.json.** { *; }
// or a specific class
-keep class com.example.app.json.SpecificClass { *; }
option 2 (use @SerializedName
):
public class YourJsonClass{
@SerializedName("name") String username;
public MyClass(String username) {
this.username = username;
}
}
with the second option proguard still obfuscates the class and field names but gosn can use the annotation to get the correct name for each field
Proguard issue with Gson and Volley
You have to modify your proguard.
For gson use this Gson github config:
-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
For volley use this:
-keep class com.android.volley.** { *; }
-keep class org.apache.commons.logging.**
-keepattributes *Annotation*
-dontwarn org.apache.**
Volley seems not working after ProGuard obfuscate
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>;
}
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 parsing on android with proguard
As darwin commented, adding the @Keep
annotation to my POJOs classes fixed the issue
proguard in android - should i obfuscate gson POJOs
5 months too late but I ran into this issue and this was the first Google result.
My solution was just to keep all POJOs unobfuscated, the easiest way being to keep all classes that are in a "model" folder
-keep class com.example.myapp.**.model.** {*; }
This will keep all POJOs that are in any model package in your app.
Related Topics
Multiple Commands Through Jsch Shell
Efficiency of Searching Using Wherearraycontains
Httpurlconnection.Getresponsecode() Returns -1 on Second Invocation
How to Move Firebase Child from One Node to Another in Android
How to Update Information in an Android Activity from a Background Service
Internet Listener Android Example
Intellij Build Error Context Mismatch
Fileuriexposedexception in Android
Aes/Cbc Encrypt in Java, Decrypt in Ruby
Detecting Device Type in a Web Application
Difference Between Google() and Maven { Url 'Https://Maven.Google.Com' }
Polyline Is Not on the Roads: It Goes Straight from One Point to Other
How to Transform Currenttimemillis to a Readable Date Format
Open Source Face Recognition for Android
How Get Value from Linkedhashmap Based on Index Not on Key
Multiple Dex Files Define <My Package>/Buildconfig, Can't Find the Cause: