Search code examples
androidretrofitretrofit2

body params properties name change in signed APK


I created an android application which I use retrofit as REST client.

when i install the app on a phone, the requests are sent without any problem, but when i'm building a signed APK, i don't know why the properties name change when sending the request

I have a class which contains a "phone" field, then the request body must be

{
    "phone": "123456"
}

but in the log, I see that the body turns into

{
    "a": "123456"
}

the problem occurs only in signed APK,

Any help please?


Solution

  • Quick Fix disable minifyEnabled to false.

    release {
            minifyEnabled false
            ...
        }
    

    OR

    If you dont want your class members to be obfuscated then use SerializedName annotation provided by Gson. For example:

    public class ClassNameHere
    {
       @SerializedName("phone")
       public String phone;
         ...
    
    }
    

    Moreover, make sure you do add proper proguard configuration for Gson library too. For 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
    -keep class com.mypackage.ClassNameHere.** { *; }
    -keep class com.google.gson.examples.android.model.** { *; }
    # 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.** { *; }
    
    ##---------------End: proguard configuration for Gson ----------
    

    For more info read proguard rules