0

While obfuscating the android app using R8 and minifyEnabled true in build.gradle it adds duplicate key like below in one of webservice response.

Response: {"key1":"value1", ......., "key1":"value1"} it adds "key1" multiple time and flexJson throws exception and crashes the app

Caused by: flexjson.JSONException: Duplicate key "key1"
    at flexjson.JSONTokener.putOnce(JSONTokener.java:498)
    at flexjson.JSONTokener.parseObject(JSONTokener.java:471)
    at flexjson.JSONTokener.nextValue(JSONTokener.java:357)
    at flexjson.JSONTokener.parseObject(JSONTokener.java:471)
    at flexjson.JSONTokener.nextValue(JSONTokener.java:357)
    at flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:197)

Everything works fine without obfuscation(minifyEnabled false). Gradle Plugin Version used: 3.4.2, Also flexJson is used by one of the library included in the project.

takharsh
  • 2,258
  • 1
  • 21
  • 26

1 Answers1

0

In general you should ensure that all fields which are used for generating JSON through reflection are covered by a keep rule. Otherwise the name in the JSON can change from build to build. Also R8 use the property that the JVM and Android runtimes allow fields of different types to have the same name, you can end up in the situation described here.

One option could be to annotate all classes which are serialized, and use a keep rule like this:

-keep class @MyAnnotation ** {
  <fields>;
}

or if all these classes are in a separate package:

-keep class com.example.mypackage.serialized_classes.** {
  <fields>;
}
sgjesse
  • 3,793
  • 14
  • 17