androidgsonproguardretrofit2android-proguard

ProGuard for Android and Retrofit2 Converter Gson?


i am using ProGuard in my project but its giving wrong data in new Gson().toJson(Request);

i am getting out put

{"a":"manage","b":"689184d4418b6d975d9a8e53105d3382","c":"10","d":"76"}

instead of

{"username":"manage","password":"689184d4418b6d975d9a8e53105d3382","value":"10","store":"76"}

My ProGuard rule

-dontwarn okio.**
-dontwarn retrofit2.Platform$Java8
-dontwarn sun.misc.Unsafe
-dontwarn org.w3c.dom.bootstrap.DOMImplementationRegistry
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclassmembers class rx.internal.util.unsafe.** {
    long producerIndex;
    long consumerIndex;
}

-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}
-keep class com.google.gson.** { *; }
-keep class com.google.inject.** { *; }

and i am using

 compile 'com.squareup.retrofit2:converter-gson:2.0.0'

is there a new recommended ProGuard configuration for retrofit2:converter-gson in Android?


Solution

  • 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