-1

I have found the proguard issues and answer related to com.google.gson library but not with which are related with com.google.code.gson:gson:2.3.1' library

I am using 'com.google.code.gson:gson:2.3.1' library. without proguard its working fine but when i enable proguard the below code gives the wrong result.

 Gson gson = new Gson();
            UserDetails userDetails = gson.fromJson(response, UserDetails.class);
            if (userDetails != null) {
                Logger.appendInfoLog("gsonData" + userDetails, "gsonData");
                Logger.appendInfoLog("gsonData" + userDetails.getCity(), "cityName");
            }

In this code userDetails.getCity() is returning null.

What i have tried is :- To keep this library from obfuscation in proguard-rules.pro I have declared it like this

-keep class com.google.code.gson.**

But its not working, any help is appreciated

Awadesh
  • 3,530
  • 2
  • 20
  • 32

2 Answers2

1

Gson uses member field names (via reflection) by default to do the parsing so if you use tools like ProGuard your class UserDetails will have it's members field names changed. The thing you have to do is to keep proguard from obfuscating your model classes.

ex: if your model classes are in mypackage.model package you can add rule:

-keep class mypackage.model.** { *; }

-keepclassmembers class mypackage.model.** { *; }

Community
  • 1
  • 1
Than
  • 2,759
  • 1
  • 20
  • 41
  • instead of keep class mypackage.model.** { *; } i used -keepclassmembers public class com.leadsquared.app.models.**{ ; } and it works fine please put this statement also in your answer :) – Awadesh Oct 27 '15 at 13:06
0

Do you have @SerializedName() annotation for every field in UserDetails? Proguard obfuscates all names (class/field/method names), so gson will not find fields in your class.

m0rphine
  • 126
  • 4