Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson parsing is not working when ProGuard rule enabled

[![Proguard rules][gson parsing version 2.8.0]][android official volley version 1.0.0]

Gson parsing is not working when ProGuard rules enabled. i have used official version of Volley 1.0.0 and Gson version 2.8.0. So, whenever I enable ProGuard rules, Gson parsing is not working. I have also added required rules in proguard-rules.pro file still it is not working.

Dependency

compile 'com.google.code.gson:gson:2.8.0'
compile 'com.android.volley:volley:1.0.0'

ProGuard rules:

-ignorewarnings
-keep class org.joda.** { *; }
-dontwarn org.joda.convert.FromString
-dontwarn org.joda.convert.ToString
-dontwarn org.joda.convert.**
-dontwarn org.joda.time.**
-keep class org.joda.time.** { *; }
-keep interface org.joda.time.** { *; }
-dontwarn org.mockito.**
-dontwarn sun.reflect.**
-dontwarn android.test.**
-dontwarn java.lang.management.**
-keepattributes Signature
-keep class com.google.gson.examples.android.model.** { *; }
-keep class com.google.gson.**{ *; }
-dontwarn com.fasterxml.jackson.**
-keep class com.fasterxml.jackson.** { *; }

Parsing method

 private static void verifyResponse(final String response, final RequestCode requestCode, final IListener listener) throws IOException, ClassNotFoundException {

        if (listener != null) {
            ResponseStatus responseStatus;
            try {
                JSONObject jResult = new JSONObject(response);
                if (jResult.has("d")) {
                    String strResult = jResult.getString("d");
                    jResult = new JSONObject(strResult);
                    Debug.trace("ResponseStatusA " + jResult.toString());
                }

                responseStatus = gson.fromJson(new JSONObject(jResult.toString()).toString(), ResponseStatus.class);


                Debug.trace("ResponseStatusB " + responseStatus.getStatus());
                processSuccess(jResult.toString(), responseStatus, requestCode, listener);
               /* if (responseStatus.isFail()) {
                    processError(responseStatus, listener);
                } else {

                    if (responseStatus.isSuccess()) {

                        listener.onHideProgressDialog();

                        processSuccess(jResult.toString(), responseStatus, requestCode, listener);
                    }
                }*/
            } catch (JsonSyntaxException | JSONException e) {
                e.printStackTrace();
            }
        }
    }
like image 275
Gaurav patel Avatar asked Mar 14 '17 12:03

Gaurav patel


2 Answers

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.

like image 131
Mike Laren Avatar answered Nov 12 '22 19:11

Mike Laren


Using -keep is a bad practice and you should never do it .You almost never want to use -keep; if you do need a ProGuard rule, you usually want one of the more specific variants

-keepclassmembers - This protects only the members of the class from shrinking and obfuscation.

-keepnames - This allows shrinking for classes and members, but not obfuscation. That is, any unused code is going to get removed. But the code that is kept will keep its original names.

-keepclassmembernames - Unused classes are removed, the remaining classes are renamed, unused members of those classes are removed, but then the remaining members keep their original names.

For more information please read this

PS - this is what I did for Gson

-keepclassmembernames class rscom.pojo.** { <fields>; }
like image 41
Kaustubh Bhagwat Avatar answered Nov 12 '22 18:11

Kaustubh Bhagwat