Somehow from one App release into another the internals of org.json library broke. It could no longer read the contents of a key as a string. Here is an example of the code that used to work and is breaking when minify/R8/proguard is enabled.
try {
parseJson("{\"rewards\":[{\"amount\":1,\"name\":\"Add Audio\"}]}");
} catch (Exception e) {
Log.w("TEST", "Error parsing rewarded currencies JSON header!!!", e);
}
private void parseJson(@NonNull String rewardedCurrencies) throws JSONException {
final Map<String, String> rewardsMap = jsonStringToMap(rewardedCurrencies);
for (Map.Entry<String, String> entry : rewardsMap.entrySet()) {
Log.e("TEST", entry.getKey() + ":" + entry.getValue());
}
}
public Map<String, String> jsonStringToMap(String jsonParams) throws JSONException {
Map<String, String> jsonMap = new HashMap<String, String>();
if (TextUtils.isEmpty(jsonParams)) return jsonMap;
JSONObject jsonObject = (JSONObject) new JSONTokener(jsonParams).nextValue();
Iterator<String> keys = jsonObject.keys();
Log.e("TEST", "jsonStringToMap() -> jsonObject: "+jsonObject.toString());
while (keys.hasNext()) {
String key = (String) keys.next();
Log.e("TEST", "jsonStringToMap() -> key: "+key);
jsonMap.put(key, jsonObject.getString(key));
}
return jsonMap;
}
Log output with failure:
jsonStringToMap() -> jsonObject: {"rewards":[{"amount":1,"name":"Add Audio"}]}
jsonStringToMap() -> key: rewards
Error parsing rewarded currencies JSON header!!!
j.b.b: JSONObject["rewards"] not a string.
at j.b.d.getString(SourceFile:4)
So what changed on the project since the last working App release?
targetSdkVersion 28 -> 29
sourceCompatibility JavaVersion.VERSION_1_7 -> JavaVersion.VERSION_1_8
'com.android.tools.build:gradle:4.0.0' -> 'com.android.tools.build:gradle:4.1.1'
distributionUrl -> gradle-6.1.1-all.zip -> gradle-6.5-all.zip
The fix was to apply a new line to the proguard script.
-keep class org.json.** { *; }
And now I'm worried. Why do I need to apply this to proguard? Are there other internal libraries potentially breaking? I can't seem to figure out what has caused this and if it's a potential big issue?