From da0d73e580d641cdcd43f077948f0d53f3771db4 Mon Sep 17 00:00:00 2001 From: Jared Duke Date: Wed, 1 Feb 2023 10:27:04 -0800 Subject: [PATCH] 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 --- core/proguard.flags | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/core/proguard.flags b/core/proguard.flags index 53f63d8ea0..d790061524 100644 --- a/core/proguard.flags +++ b/core/proguard.flags @@ -9,14 +9,19 @@ # Add this flag in your package's own configuration if it's needed. #-flattenpackagehierarchy -# Keep classes and methods that have @VisibleForTesting annotations, except in -# intermediate libraries that export those annotations (e.g., androidx, guava). -# This avoids keeping library-specific test code that isn't actually needed -# for platform testing. +# Keep classes and members with the platform-defined @VisibleForTesting annotation. +-keep @com.android.internal.annotations.VisibleForTesting class * +-keepclassmembers class * { + @com.android.internal.annotations.VisibleForTesting *; +} + +# Keep classes and members with non-platform @VisibleForTesting annotations, but +# only within platform-defined packages. This avoids keeping external, library-specific +# test code that isn't actually needed for platform testing. # TODO(b/239961360): Migrate away from androidx.annotation.VisibleForTesting # and com.google.common.annotations.VisibleForTesting use in platform code. --keep @**.VisibleForTesting class !androidx.**,!com.google.common.**,* --keepclassmembers class !androidx.**,!com.google.common.**,* { +-keep @**.VisibleForTesting class android.**,com.android.**,com.google.android.** +-keepclassmembers class android.**,com.android.**,com.google.android.** { @**.VisibleForTesting *; }