Skip to content

Provide proguard/r8 rules - #1929

Closed
ganadist wants to merge 1 commit into
google:mainfrom
ganadist:proguard
Closed

Provide proguard/r8 rules#1929
ganadist wants to merge 1 commit into
google:mainfrom
ganadist:proguard

Conversation

@ganadist

@ganadist ganadist commented Aug 5, 2021

Copy link
Copy Markdown

Currently, developer needs to add proguard rules manually.
But rules can be provided as resource and proguard/r8 will read and apply automatically.

Rules are copied from https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

gson/src/main/resources/META-INF/proguard/gson.pro: proguard rules for java applications
gson/src/main/resources/META-INF/com.android.tools/proguard/gson.pro : proguard rules for Android Gradle Plugin
gson/src/main/resources/META-INF/com.android.tools/r8/gson.pro : R8 rules for Android Gradle Plugin

gson/src/main/resources/META-INF/com.android.tools/r8-from-3.0.0/gson.pro : Additional R8 rules for 3.0 or higher

@google-cla google-cla Bot added the cla: yes label Aug 5, 2021
@ganadist
ganadist force-pushed the proguard branch 2 times, most recently from 9353015 to f9a5f55 Compare August 10, 2021 06:10
@eamonnmcmanus

Copy link
Copy Markdown
Member

If these resources are present, does that mean that android-proguard-example can be made to work correctly even if its own copy of the proguard rules is deleted?

How do we test that these rules are working correctly?

@ganadist

ganadist commented Aug 11, 2021

Copy link
Copy Markdown
Author

Note: This repo needs git-lfs
Note2: This branch contains prebuilt gson from ganadist@830c5f664 : ganadist/minimal-reproducible-example@99a6075f7

$ git clone https://github.com/ganadist/VersionCodeDemo -b gson_proguard_rule_demo 
$ cd VersionCodeDemo

### use AGP 4.2
$ git checkout HEAD~1
$ ./gradlew :app:pPRUA
...
> Task :app:packageProductionReleaseUniversalApk

$ cat app/build/outputs/mapping/productionRelease/configuration.txt

### check whether META-INF/com.android.tools/r8/gson.pro is included

### use AGP 7.0
$ git checkout origin/gson_proguard_rule_demo
$ ./gradlew :app:pPRUA
...
> Task :app:packageProductionReleaseUniversalApk

$ cat app/build/outputs/mapping/productionRelease/configuration.txt

### check whether META-INF/com.android.tools/r8/gson.pro and META-INF/com.android.tools/r8-from-3.0.0/gson.pro are included 

See also

@eamonnmcmanus

Copy link
Copy Markdown
Member

Sorry, I should have been a bit more explicit about my concerns. This PR is adding 4 different ProGuard configuration files. Is there a way to add an automated test that will check that each of those files is correct? I think it will be hard to maintain the files otherwise.

@renzified

Copy link
Copy Markdown

Any update to this one? r8 rules should be included its 2022 now

@Marcono1234

Copy link
Copy Markdown
Contributor

Default rules have been added by #2397 and will be available in the next version, so this pull request here might be obsolete.

@ganadist and everyone else who wanted this feature, could you please try building Gson locally and see if the snapshot version of Gson correctly applies the rules.

Any feedback regarding these rules, such as if something is missing or is too coarse, is highly appreciated!

Also keep in mind that these default library rules cannot cover everything; if you have specific classes or fields which should be preserved and you are not using @SerializedName you might have to add additional rules for your build.

@Marcono1234 Marcono1234 added the proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation label Mar 29, 2024
@ganadist

Copy link
Copy Markdown
Author

Closed by #2397

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants