Gson parsing is not working when ProGuard rule enabled
Asked Answered
B

3

6

[![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();
            }
        }
    }
Breadboard answered 14/3, 2017 at 12:57 Comment(0)
W
5

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.

Warsle answered 19/3, 2017 at 6:53 Comment(1)
Thank you very very much @Mike Laren It works actually i have added my model package in proguard rule -keep class com.example.mvc.codebase.models.**{*;} but my ResponseStatus class is in another package so i have added class in proguard file as you told and it's works Thak you againBreadboard
K
2

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>; }
Kristynkrock answered 23/7, 2019 at 8:17 Comment(1)
I understand the basis for your argument; however, when I use your rule, Gson stops working. When I use the originally suggested -keep class syntax, Gson works. Perhaps some behavior changed in the years since your answer. Using -keepclassmembers also seems to work as suggested in this answer: stackoverflow.com/a/22822644Apostatize
V
1

Add Below Proguard Rules in proguard-rules.pro file in Android Sutdio.

Gson

-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }

Keep You Model Exclude From Proguard

-keepclassmembers class com.yourpackage.models** { <fields>; }
-keep class com.yourpackage.models{ *; }
Vincenty answered 23/7, 2019 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.