9

I save the json presentation of some objects as cache in my app. I had no problem after updating each release of the app and json objects was correctly converted to java objects. After updating to android gradle plugin 3.5 (Adding this line):

classpath 'com.android.tools.build:gradle:3.5.0'

When I create the release apk, It seems that it can't convert the json string to java objects and it seems that the obfuscated names has changed. When I revert it to gradle plugin 3.4.2, everything is OK. So what is the problem and how can I fix that?

Misagh Emamverdi
  • 3,654
  • 5
  • 33
  • 57
  • can't you add that json files as exception in proguard rules – Manoj Perumarath Aug 31 '19 at 08:46
  • https://stackoverflow.com/a/57156980/7254873 – Sumit Shukla Aug 31 '19 at 08:49
  • @ManojPerumarath, the problem is that the obfuscated objects have been saved as cache and if I exclude them now, saved jason objects cannot convert to java objects. – Misagh Emamverdi Sep 01 '19 at 04:16
  • 2
    Its the Anroid R8 which is disabling the progaurd, since it has different algorithm even though your objects are obfuscated they are not deserializable anymore. I assume Android did not consider this use case. See the answers below from Bhaven Shah for the fix. – MG Developer Oct 17 '19 at 01:44

2 Answers2

6

I also got the same error.

And I found that it's an error of enabledR8 which is used to reduce app size.

and It is true predefined. So by set value, a false error was gone.


Set it value false of r8 in gradle.properties file.

android.enableR8=false

Bhaven Shah
  • 692
  • 1
  • 5
  • 18
  • 1
    This answer makes sense, thank you. So basically R8 is the new way of compressing, minifying and obfuscating the code, hence it automatically disables progaurd rules. This created a havoc all my serialized data no longer readable. Whew. Big Thank you for the quick answer. – MG Developer Oct 17 '19 at 01:42
  • @MGDeveloper Google is going to get rid of useProguard property soon, and we will need to enable R8 in that case. Not sure how long this can ben done – user2234 Jan 30 '20 at 01:30
5

I should exclude cached objects from obfuscating but temporarily I disabled R8 and enabled proguard, by adding this line to gradle.properties file:

android.enableR8=false

And adding this line to app build.gradle file:

useProguard true
minifyEnabled true

And the problem solved.

Misagh Emamverdi
  • 3,654
  • 5
  • 33
  • 57
  • Does this issue still occur as Google is going to remove useProguard and use only android.enableR8 . "WARNING: DSL element 'useProguard' is obsolete and will be removed soon. Use 'android.enableR8' in gradle.properties to switch between R8 and Proguard" – user2234 Jan 30 '20 at 01:31
  • @user12865, the issue still occurs, but as you said, `useProguard` is going to be removed and the temporary solution won't work anymore. – Misagh Emamverdi Jan 30 '20 at 06:05