Search code examples
c#androidretrofit2signed-apk

Signed apk is changing post parameters name


I have my APP and c# web api which the app calls using retrofit2. I'm facing a problem while making a post call to the api, the parameter names in the call are changing to "a" and "b" instead of there actual names.

ApiInterface:

@POST("users/add")
Call<String> createUser(@Body UserSignUpModel user);

Setting retrofit:

Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();
retrofit = new Retrofit.Builder()
                    .baseUrl(Configuration.API_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .client(get_HTTPClient())
                    .build();

UserSignupModel:

public class UserSignUpModel
{
    public String UserName;
    public long PhoneNumber;
}

Making the request:

userSignUpModel.UserName = userName.getText().toString();
userSignUpModel.PhoneNumber = Long.parseLong(number.getText().toString());
Client.createUser(userSignUpModel, callback);

I have also setup fiddler to intercept the request that I'm getting from my app and below are the results:
1. When I click debug or run on android studio
While debug or run
2. When I generate a signed apk and make request from it
While executing signed apk


My requests are failing because of this. Any help will be appreciated.
Thank you.


Solution

  • Generating a signed .apk will trigger proguard (if it's enabled). Which will obfuscate the classes and such.

    There are 2 ways how you can fix this case:

    Option 1: Using @SerializedName

    Change your UserSignUpModel.java, explicitely stating the name of the variables:

    import com.google.gson.annotations.SerializedName;
    
    public class UserSignUpModel
    {
        @SerializedName("UserName")
        public String UserName;
        @SerializedName("PhoneNumber")
        public long PhoneNumber;
    }
    

    Option 2: Changing the proguard-rules.pro file

    Add the following rule to your proguard-rules.pro file, assuming UserSignUpModel.java is inside the model package:

    -keep class com.<packageName>.model.** { *; }