Commit graph

14 commits

Author SHA1 Message Date
Jared Duke
da0d73e580 Further refine VisibleForTesting rules
Additional analysis revealed more edge cases where external library
code was getting kept unnecessarily. Further refine the keep rules to
avoid these cases. Now, we only apply keep globally for the
`com.android.internal.annotations.VisibleForTesting` annotation.

VisibleForTesting annotations defined externally (e.g., by androidx or
guava) will *only* be respected in platform-defined packages. This
helps trim unused code from deps like gRPC and Dagger, saving up to
~hundreds of KB per package that uses these libraries.

Bug: 248580093
Test: m + verify exclusion of external test code (e.g., from dagger)
Change-Id: Iab7559c08d3ae1ac74f18e3cf3a1b4828a3736cf
2023-02-02 16:33:34 +00:00
Jared Duke
97a9903c3e Refine VisibleForTesting proguard rules
Both androidx (Jetpack) and com.google.common (Guava) define
@VisibleForTesting annotations. These are used within these libraries
for test purposes, but have also been adopted throughout the platform
for platform-specific test code. Consequently, the current keep
rules end up keeping not only platform test code, but also library test
code that isn't actually needed.

Refine these rules by restricting VisibleForTesting-annotated keep
rules to include code *outside* of Jetpack/Guava. A longer term
solution will involve migrating away from these annotations toward
the platform-defined @TestApi and internal @VisibleForTesting variants.
Note that platform code using the androidx* and c.g.common*-defined
@VisibleForTesting annotations will still be kept.

This saves about ~11MB of dex code from platform apps/services.

Bug: 239961360
Test: m + validate dex changes for shrunk packages (SystemUI/Launcher)
Change-Id: Icd70090e615b72a2cfb9ce8c43edb17cb46e05e2
2022-08-03 08:35:45 -07:00
Jared Duke
7d0c90b51f Add keep rules for @KeepForWeakReference annotation
Ensure annotated fields are kept when treeshaking-related optimizations
are enabled. Note that this still allows field removal if tree shaking
reveals no references, but will avoid removal if there are references
that might impact downstream usage, e.g., weak references.

Bug: 227455445
Test: atest InternalAnnotationsTest
Change-Id: I59c6277c43b14c5de8154fa1eefad01f0702e413
2022-05-11 09:47:12 -07:00
Jared Duke
4bd749bf38 Generalize @Keep annotation Proguard rules
Consolidate the Proguard rules for @Keep annotations for:
  * android.support.annotation.Keep (existing)
  * androidx.annotation.Keep (existing)
  * com.android.internal.annotation.Keep (new)

This ensures consistent semantics for clients using these annotations
across the codebase. Ideally there would be a single annotation, but
the support libraries are not available everywhere. In practice, this
change should be a no-op.

Bug: 210510433
Test: m + validate identical artifact sizes
Change-Id: Iab494e03269e5758d7f860331e971826ecc08cdd
2022-01-21 19:50:15 +00:00
Colin Cross
38b48debc5 Understand the support library/androidx @Keep annotations
Copied from tools/base/build-system/gradle-core/src/main/resources/com/android/build/gradle/proguard-common.txt.

Fixes: 129311848
Test: none
Change-Id: Ie8b40b416c8772a550bb4d1f7bd6e7e944cdd166
2019-03-26 23:44:01 +00:00
Daichi Hirono
e2a85aa162 Let proguard keep classes/methods annotated with internal VisibleForTesting.
BUG=None

Change-Id: I194aac5057a7368e91b401c639100c86b2426265
2015-07-27 13:24:30 +09:00
Ying Wang
7311a344be Make it easier to enable obfuscation and optimization.
With this change, use:
* LOCAL_PROGUARD_ENABLED := obfuscation  # to enable obfuscation
* LOCAL_PROGUARD_ENABLED := optimization # to enable optimization
* LOCAL_PROGUARD_ENABLED := obfuscation optimization # to enable both

Now the meaning of the LOCAL_PROGUARD_ENABLED options:
* full:
    Use the build system's default configurations:
    with shrink but no obfuscation or optimization,
    global proguard flags in build/core/proguard.flags
    are applied.
* custom:
    The same as "full" except no aapt-generated resource-related
    proguard flags.
* nosystem:
    Don't use any build system's default configurations; but
    aapt-generated proguard flags are still applied. You are
    responsible for any other flags.
* disabled:
    Disable proguard.
* obfuscation:
    The same as "full" but with obfuscation enabled.
* optimization:
    The same as "full" but with optimization enabled.
* no value (the default):
    The build system chooses the proper value: "full" if it's an
    app; "disabled" if it's a library.

You can use more than 1 of them in a meaningful combination,
for example:
LOCAL_PROGUARD_ENABLED := obfuscation optimization

Bug: 10307372
Change-Id: Id248caca3048e99547f16559fae74f4afe85c354
2013-08-22 17:12:38 -07:00
Ying Wang
38cdd4429f Tweak proguard flags in the 2 files.
So all most commonly-used flags go to proguard_basic_keeps.flags
and they will still be applied when LOCAL_PROGUARD_ENABLED=nosystem.
Bug: 9203442

Change-Id: If018076dc625ac1efe31898b179dbba850218aaf
2013-06-12 11:04:49 -07:00
Ying Wang
5745351908 Split out proguard_basic_keeps.flags
For 'LOCAL_PROGUARD_ENABLED := nosystem' we still include
proguard_basic_keeps.flags.

Change-Id: I43ee7e98d2aaeb84879441f90ab6839c0e8f4e65
2013-05-17 10:02:00 -07:00
Ying Wang
f8f2b022e9 Ignore support library Proguard warning
The support library contains references to newer platform versions.
Don't warn about those in case this app is linking against an older
platform version.  We know about them, and they are safe.

Change-Id: Ia2a90398b1db1858d005ec09ded56751cc6ffb00
2012-09-23 13:46:58 -07:00
Hung-ying Tyan
ddefdef5bc Make ProGuard keep necessary members for serializable classes
Change-Id: I845eb8280d0cab7e426b17c94d74bc409e66c193
2010-08-11 08:24:46 +08:00
Bjorn Bringert
4f85d00113 Make ProGuard keep @VisibleForTesting classes and methods
Change-Id: I7f5d14fd4188859c52ab85c33af1ac275888611c
2010-03-09 14:37:21 +00:00
Ying Wang
3b2bdf1033 Fix and enable proguard on packages. 2010-02-11 13:41:10 -08:00
Joe Onorato
2daa2b3b05 Add the ability to turn on proguard for java targets, and to call aapt
to generate a rules file for proguard for stuff from the manifest and
other resources that need to be kept.
2009-08-30 13:43:00 -07:00