From b8f3534d989951c05ca76a301448ef1eeffd0ff6 Mon Sep 17 00:00:00 2001 From: Christopher Peterson Sauer Date: Sat, 26 Feb 2022 02:53:59 -0800 Subject: [PATCH] Configure ProGuard for Android Adds Android default ProGuard Specs to AndroidSdkProvider ...and uses them when ProGuarding an android_binary. Background: Android ProGuard actions need to use proguard-android-optimize.txt or proguard-android.txt to be configured properly. Please see #14909 for more context. --- .../lib/rules/android/AndroidSdkBase.java | 6 + .../lib/rules/android/AndroidSdkBaseRule.java | 12 ++ .../lib/rules/android/AndroidSdkProvider.java | 20 +++ .../lib/rules/android/ProguardHelper.java | 14 ++ .../android/AndroidSdkProviderApi.java | 18 +++ .../lib/analysis/mock/BazelAnalysisMock.java | 4 + .../util/BazelMockAndroidSupport.java | 4 + .../lib/rules/android/AarImportTest.java | 2 + .../lib/rules/android/AndroidBinaryTest.java | 6 + .../rules/android/AndroidDataBindingTest.java | 4 + .../android/AndroidDataBindingV2Test.java | 6 + .../lib/rules/android/AndroidLibraryTest.java | 6 + .../lib/rules/android/AndroidSdkTest.java | 2 + tools/android/BUILD.tools | 4 + .../android_sdk_repository_template.bzl | 4 + tools/android/proguard-android-optimize.txt | 122 ++++++++++++++++++ tools/android/proguard-android.txt | 121 +++++++++++++++++ 17 files changed, 355 insertions(+) create mode 100644 tools/android/proguard-android-optimize.txt create mode 100644 tools/android/proguard-android.txt diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkBase.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkBase.java index ae63cf6d36c0d3..cfdc8728d96efb 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkBase.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkBase.java @@ -51,6 +51,10 @@ public ConfiguredTarget create(RuleContext ruleContext) ruleContext.getFragment(JavaConfiguration.class).getProguardBinary() == null ? ruleContext.getExecutablePrerequisite("proguard") : ruleContext.getExecutablePrerequisite(":proguard"); + Artifact proguardConfigurationOptimize = + ruleContext.getPrerequisiteArtifact("proguard_configuration_optimize"); + Artifact proguardConfigurationDontOptimize = + ruleContext.getPrerequisiteArtifact("proguard_configuration_dont_optimize"); String buildToolsVersion = AggregatingAttributeMapper.of(ruleContext.getRule()) @@ -98,6 +102,8 @@ public ConfiguredTarget create(RuleContext ruleContext) apkBuilder, apkSigner, proguard, + proguardConfigurationOptimize, + proguardConfigurationDontOptimize, zipalign, system, legacyMainDexListGenerator); diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkBaseRule.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkBaseRule.java index e53ef49a2086ad..0c93eca3553260 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkBaseRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkBaseRule.java @@ -52,6 +52,18 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment envi .cfg(ExecutionTransitionFactory.create()) .allowedFileTypes(ANY_FILE) .exec()) + // This is the equivalent of getDefaultProguardFile(proguard-android-optimize.txt) + // For more, see https://developer.android.com/studio/build/shrink-code + .add( + attr("proguard_configuration_optimize", LABEL) + .mandatory() + .allowedFileTypes(ANY_FILE)) + // This is the equivalent of getDefaultProguardFile(proguard-android.txt) + // For more, see https://developer.android.com/studio/build/shrink-code + .add( + attr("proguard_configuration_dont_optimize", LABEL) + .mandatory() + .allowedFileTypes(ANY_FILE)) .add( attr("aapt", LABEL) .mandatory() diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkProvider.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkProvider.java index b95d3cd599f596..3534acc65c2f93 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkProvider.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSdkProvider.java @@ -65,6 +65,8 @@ public final class AndroidSdkProvider extends NativeInfo private final FilesToRunProvider apkBuilder; private final FilesToRunProvider apkSigner; private final FilesToRunProvider proguard; + private final Artifact proguardConfigurationOptimize; + private final Artifact proguardConfigurationDontOptimize; private final FilesToRunProvider zipalign; @Nullable private final BootClassPathInfo system; @Nullable private final FilesToRunProvider legacyMainDexListGenerator; @@ -86,6 +88,8 @@ public AndroidSdkProvider( @Nullable FilesToRunProvider apkBuilder, FilesToRunProvider apkSigner, FilesToRunProvider proguard, + Artifact proguardConfigurationOptimize, + Artifact proguardConfigurationDontOptimize, FilesToRunProvider zipalign, @Nullable BootClassPathInfo system, @Nullable FilesToRunProvider legacyMainDexListGenerator) { @@ -105,6 +109,8 @@ public AndroidSdkProvider( this.apkBuilder = apkBuilder; this.apkSigner = apkSigner; this.proguard = proguard; + this.proguardConfigurationOptimize = proguardConfigurationOptimize; + this.proguardConfigurationDontOptimize = proguardConfigurationDontOptimize; this.zipalign = zipalign; this.system = system; this.legacyMainDexListGenerator = legacyMainDexListGenerator; @@ -297,6 +303,16 @@ public FilesToRunProvider getProguard() { return proguard; } + @Override + public Artifact getProguardConfigurationOptimize() { + return proguardConfigurationOptimize; + } + + @Override + public Artifact getProguardConfigurationDontOptimize() { + return proguardConfigurationDontOptimize; + } + @Override public FilesToRunProvider getZipalign() { return zipalign; @@ -339,6 +355,8 @@ public AndroidSdkProvider createInfo( Object apkBuilder, FilesToRunProvider apkSigner, FilesToRunProvider proguard, + Artifact proguardConfigurationOptimize, + Artifact proguardConfigurationDontOptimize, FilesToRunProvider zipalign, Object system, Object legacyMainDexListGenerator) @@ -360,6 +378,8 @@ public AndroidSdkProvider createInfo( fromNoneable(apkBuilder, FilesToRunProvider.class), apkSigner, proguard, + proguardConfigurationOptimize, + proguardConfigurationDontOptimize, zipalign, fromNoneable(system, BootClassPathInfo.class), fromNoneable(legacyMainDexListGenerator, FilesToRunProvider.class)); diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/ProguardHelper.java b/src/main/java/com/google/devtools/build/lib/rules/android/ProguardHelper.java index 98cfd71ba066b7..90a51282e6a80b 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/ProguardHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/ProguardHelper.java @@ -30,6 +30,7 @@ import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext; import com.google.devtools.build.lib.analysis.actions.CustomCommandLine; import com.google.devtools.build.lib.analysis.actions.SpawnAction; +import com.google.devtools.build.lib.analysis.config.CompilationMode; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; @@ -314,6 +315,19 @@ public static ProguardOutput createOptimizationActions( throws InterruptedException { Preconditions.checkArgument(!proguardSpecs.isEmpty()); + // Configure proguard for Android + // This is the equivalent of getDefaultProguardFile(proguard-android-optimize.txt) + // or getDefaultProguardFile(proguard-android.txt) + // For more, see https://developer.android.com/studio/build/shrink-code + boolean opt = ruleContext.getConfiguration().getCompilationMode() == CompilationMode.OPT; + AndroidSdkProvider sdk = AndroidSdkProvider.fromRuleContext(ruleContext); + proguardSpecs = + ImmutableList.builder() + .add(opt ? sdk.getProguardConfigurationOptimize() + : sdk.getProguardConfigurationDontOptimize()) + .addAll(proguardSpecs) + .build(); + ProguardOutput output = getProguardOutputs( proguardOutputJar, diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/android/AndroidSdkProviderApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/android/AndroidSdkProviderApi.java index c25fabe5a92791..fd7e3732242b38 100644 --- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/android/AndroidSdkProviderApi.java +++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/android/AndroidSdkProviderApi.java @@ -125,6 +125,12 @@ public interface AndroidSdkProviderApi< @StarlarkMethod(name = "proguard", structField = true, doc = "", documented = false) FilesToRunProviderT getProguard(); + @StarlarkMethod(name = "proguard_configuration_optimize", structField = true, doc = "", documented = false) + FileT getProguardConfigurationOptimize(); + + @StarlarkMethod(name = "proguard_configuration_dont_optimize", structField = true, doc = "", documented = false) + FileT getProguardConfigurationDontOptimize(); + @StarlarkMethod(name = "zip_align", structField = true, doc = "", documented = false) FilesToRunProviderT getZipalign(); @@ -251,6 +257,16 @@ interface Provider< doc = "A files to run provider of Proguard.", positional = true, named = false), + @Param( + name = "proguard_configuration_optimize", + doc = "File containing the equivalent of getDefaultProguardFile(proguard-android-optimize.txt).", + positional = true, + named = false), + @Param( + name = "proguard_configuration_dont_optimize", + doc = "File containing the equivalent of getDefaultProguardFile(proguard-android.txt).", + positional = true, + named = false), @Param( name = "zipalign", doc = "A files to run provider of Zipalign.", @@ -291,6 +307,8 @@ AndroidSdkProviderApi createInfo( Object apkBuilder, FilesToRunProviderT apkSigner, FilesToRunProviderT proguard, + FileT proguardConfigurationOptimize, + FileT proguardConfigurationDontOptimize, FilesToRunProviderT zipalign, Object system, Object legacyMainDexListGenerator) diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java index e9df17e390de80..9f9b7d6f708069 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java @@ -264,6 +264,8 @@ public void setupMockClient(MockToolsConfig config, List workspaceConten " main_dex_classes = 'dummy.jar',", " main_dex_list_creator = ':empty_binary',", " proguard = 'empty_binary',", + " proguard_configuration_optimize = 'empty.sh',", + " proguard_configuration_dont_optimize = 'empty.sh',", " shrinked_android_jar = 'dummy.jar',", " zipalign = ':empty_binary',", " tags = ['__ANDROID_RULES_MIGRATION__'],", @@ -415,6 +417,8 @@ private ImmutableList createAndroidBuildContents() { " main_dex_classes = ':mainDexClasses.rules',", " main_dex_list_creator = ':main_dex_list_creator',", " proguard = ':ProGuard',", + " proguard_configuration_optimize = 'empty.sh',", + " proguard_configuration_dont_optimize = 'empty.sh',", " shrinked_android_jar = ':shrinkedAndroid.jar',", " zipalign = ':zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'],", diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockAndroidSupport.java b/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockAndroidSupport.java index 89d2b40474f5e9..c734532c6284aa 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockAndroidSupport.java +++ b/src/test/java/com/google/devtools/build/lib/packages/util/BazelMockAndroidSupport.java @@ -110,6 +110,8 @@ public static void setupPlatformResolvableSdks(MockToolsConfig config) throws Ex " main_dex_classes = ':main_dex_classes_x86_64',", " main_dex_list_creator = ':main_dex_list_creator_x86_64',", " proguard = ':proguard_x86_64',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar =':shrinked_android_x86_64.jar',", " zipalign = ':zipalign_x86_64',", " tags = ['__ANDROID_RULES_MIGRATION__'],", @@ -127,6 +129,8 @@ public static void setupPlatformResolvableSdks(MockToolsConfig config) throws Ex " main_dex_classes = ':main_dex_classes_arm',", " main_dex_list_creator = ':main_dex_list_creator_arm',", " proguard = ':proguard_arm',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar =':shrinked_android_arm.jar',", " zipalign = ':zipalign_arm',", " tags = ['__ANDROID_RULES_MIGRATION__'],", diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AarImportTest.java b/src/test/java/com/google/devtools/build/lib/rules/android/AarImportTest.java index cc7268514b3c81..808a49d1c5d17d 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/AarImportTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AarImportTest.java @@ -86,6 +86,8 @@ public void setup() throws Exception { " main_dex_classes = 'main_dex_classes',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'proguard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'shrinked_android_jar',", " zipalign = 'zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'],", diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBinaryTest.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBinaryTest.java index 423fcb7898fc73..57742326b3fc20 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBinaryTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBinaryTest.java @@ -2569,6 +2569,8 @@ public void testMainDexListWithAndroidSdk() throws Exception { " main_dex_classes = 'main_dex_classes',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'proguard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'shrinked_android_jar',", " zipalign = 'zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'])"); @@ -2614,6 +2616,8 @@ public void testLegacyMainDexListWithAndroidSdk() throws Exception { " main_dex_classes = 'main_dex_classes',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'proguard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'shrinked_android_jar',", " zipalign = 'zipalign',", " legacy_main_dex_list_generator = '//tools/fake:generate_main_dex_list',", @@ -2660,6 +2664,8 @@ public void testMainDexAaptGenerationSupported() throws Exception { " main_dex_classes = 'main_dex_classes',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'proguard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'shrinked_android_jar',", " zipalign = 'zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'])"); diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingTest.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingTest.java index 697da645f21e3b..f4f5556dc0ca16 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingTest.java @@ -358,6 +358,8 @@ public void dataBindingProviderIsProvided() throws Exception { " main_dex_classes = 'main_dex_classes',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'proguard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'shrinked_android_jar',", " zipalign = 'zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'],", @@ -400,6 +402,8 @@ public void ensureDataBindingProviderIsPropagatedThroughNonDataBindingLibs() thr " aidl = 'aidl',", " android_jar = 'android.jar',", " apksigner = 'apksigner',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " dx = 'dx',", " framework_aidl = 'framework_aidl',", " main_dex_classes = 'main_dex_classes',", diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingV2Test.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingV2Test.java index 6ee9b1899aeed0..0b04c1551e06fc 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingV2Test.java +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingV2Test.java @@ -502,6 +502,8 @@ public void dataBindingProviderIsProvided() throws Exception { " main_dex_classes = 'main_dex_classes',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'proguard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'shrinked_android_jar',", " zipalign = 'zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'],", @@ -571,6 +573,8 @@ public void ensureDataBindingProviderIsPropagatedThroughNonDataBindingLibs() thr " main_dex_classes = 'main_dex_classes',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'proguard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'shrinked_android_jar',", " zipalign = 'zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'],", @@ -616,6 +620,8 @@ public void testDataBindingCollectedThroughExports() throws Exception { " main_dex_classes = 'main_dex_classes',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'proguard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'shrinked_android_jar',", " zipalign = 'zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'],", diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidLibraryTest.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidLibraryTest.java index 1ab55240e5b934..02ce7e81b5f45f 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidLibraryTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidLibraryTest.java @@ -833,6 +833,8 @@ public void testAidlLibAddsProguardSpecs() throws Exception { " main_dex_classes = 'main_dex_classes',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'proguard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'shrinked_android_jar',", " zipalign = 'zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'],", @@ -1920,6 +1922,8 @@ public void aapt2ArtifactGenerationWhenSdkIsDefined() throws Exception { " main_dex_classes = 'main_dex_classes',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'proguard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'shrinked_android_jar',", " zipalign = 'zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'],", @@ -2005,6 +2009,8 @@ public void compileDataBindingOutputWhenDataBindingEnabled() throws Exception { " main_dex_classes = 'main_dex_classes',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'proguard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'shrinked_android_jar',", " zipalign = 'zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'],", diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidSdkTest.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidSdkTest.java index 1a61485d4588aa..700c6c1de357ea 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidSdkTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidSdkTest.java @@ -57,6 +57,8 @@ public void testSourcePropertiesProvided() throws Exception { " main_dex_classes = 'mainDexClasses.rules',", " main_dex_list_creator = 'main_dex_list_creator',", " proguard = 'ProGuard',", + " proguard_configuration_optimize = 'proguard-android-optimize.txt',", + " proguard_configuration_dont_optimize = 'proguard-android.txt',", " shrinked_android_jar = 'android.jar',", " zipalign = 'zipalign',", " tags = ['__ANDROID_RULES_MIGRATION__'],", diff --git a/tools/android/BUILD.tools b/tools/android/BUILD.tools index a693feddb3a14a..1743e431bd2f47 100644 --- a/tools/android/BUILD.tools +++ b/tools/android/BUILD.tools @@ -77,6 +77,8 @@ alias( actual = "//src/tools/android/java/com/google/devtools/build/android/dexer:DexBuilder", ) +exports_files(["proguard-android-optimize.txt", "proguard-android.txt"]) + # Defines d8_dexmerger using @//:d8_jar_import generated by android_sdk_repository rule # without needing the SDK repo's name (typically @androidsdk but not required). java_binary( @@ -441,6 +443,8 @@ android_sdk( main_dex_classes = ":error_message", main_dex_list_creator = ":error_message", proguard = ":error_message", + proguard_configuration_optimize = ":error_message", + proguard_configuration_dont_optimize = ":error_message", shrinked_android_jar = ":error_message.jar", zipalign = ":error_message", ) diff --git a/tools/android/android_sdk_repository_template.bzl b/tools/android/android_sdk_repository_template.bzl index cf6c298734e2d8..0f9106f7550344 100644 --- a/tools/android/android_sdk_repository_template.bzl +++ b/tools/android/android_sdk_repository_template.bzl @@ -125,6 +125,8 @@ def create_android_sdk_rules( name = "sdk-%d" % api_level, build_tools_version = build_tools_version, proguard = "@bazel_tools//tools/jdk:proguard", + proguard_configuration_optimize = "@bazel_tools//tools/android:proguard-android-optimize.txt", + proguard_configuration_dont_optimize = "@bazel_tools//tools/android:proguard-android.txt", aapt = select({ ":windows": "build-tools/%s/aapt.exe" % build_tools_directory, "//conditions:default": ":aapt_binary", @@ -351,6 +353,8 @@ def create_dummy_sdk_toolchain(): main_dex_classes = "dummy.jar", main_dex_list_creator = ":empty-binary", proguard = ":empty-binary", + proguard_configuration_optimize = ":empty.sh", + proguard_configuration_dont_optimize = ":empty.sh", shrinked_android_jar = "dummy.jar", zipalign = ":empty-binary", tags = ["__ANDROID_RULES_MIGRATION__"], diff --git a/tools/android/proguard-android-optimize.txt b/tools/android/proguard-android-optimize.txt new file mode 100644 index 00000000000000..5f95a343a5a2a5 --- /dev/null +++ b/tools/android/proguard-android-optimize.txt @@ -0,0 +1,122 @@ +# Manually generated for Bazel using Android Gradle Plugin 7.1.2, the latest as of 2/26/22 +# To update, configure an app per https://developer.android.com/studio/build/shrink-code +# And after building the app for release, extract the two files from +# app/build/intermediates/default_proguard_files/global/ + +# This is a configuration file for ProGuard. +# http://proguard.sourceforge.net/index.html#manual/usage.html +# +# Starting with version 2.2 of the Android plugin for Gradle, this file is distributed together with +# the plugin and unpacked at build-time. The files in $ANDROID_HOME are no longer maintained and +# will be ignored by new version of the Android plugin for Gradle. + +# Optimizations: If you don't want to optimize, use the proguard-android.txt configuration file +# instead of this one, which turns off the optimization flags. +# Adding optimization introduces certain risks, since for example not all optimizations performed by +# ProGuard works on all versions of Dalvik. The following flags turn off various optimizations +# known to have issues, but the list may not be complete or up to date. (The "arithmetic" +# optimization can be used if you are only targeting Android 2.0 or later.) Make sure you test +# thoroughly if you go this route. +-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* +-optimizationpasses 5 +-allowaccessmodification + +-dontusemixedcaseclassnames +-dontskipnonpubliclibraryclasses +-verbose + +# Preserve some attributes that may be required for reflection. +-keepattributes AnnotationDefault, + EnclosingMethod, + InnerClasses, + RuntimeVisibleAnnotations, + RuntimeVisibleParameterAnnotations, + RuntimeVisibleTypeAnnotations, + Signature + +-keep public class com.google.vending.licensing.ILicensingService +-keep public class com.android.vending.licensing.ILicensingService +-keep public class com.google.android.vending.licensing.ILicensingService +-dontnote com.android.vending.licensing.ILicensingService +-dontnote com.google.vending.licensing.ILicensingService +-dontnote com.google.android.vending.licensing.ILicensingService + +# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native +-keepclasseswithmembernames,includedescriptorclasses class * { + native ; +} + +# Keep setters in Views so that animations can still work. +-keepclassmembers public class * extends android.view.View { + void set*(***); + *** get*(); +} + +# We want to keep methods in Activity that could be used in the XML attribute onClick. +-keepclassmembers class * extends android.app.Activity { + public void *(android.view.View); +} + +# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations +-keepclassmembers enum * { + public static **[] values(); + public static ** valueOf(java.lang.String); +} + +-keepclassmembers class * implements android.os.Parcelable { + public static final ** CREATOR; +} + +# Preserve annotated Javascript interface methods. +-keepclassmembers class * { + @android.webkit.JavascriptInterface ; +} + +# The support libraries 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. +-dontnote android.support.** +-dontnote androidx.** +-dontwarn android.support.** +-dontwarn androidx.** + +# This class is deprecated, but remains for backward compatibility. +-dontwarn android.util.FloatMath + +# Understand the @Keep support annotation. +-keep class android.support.annotation.Keep +-keep class androidx.annotation.Keep + +-keep @android.support.annotation.Keep class * {*;} +-keep @androidx.annotation.Keep class * {*;} + +-keepclasseswithmembers class * { + @android.support.annotation.Keep ; +} + +-keepclasseswithmembers class * { + @androidx.annotation.Keep ; +} + +-keepclasseswithmembers class * { + @android.support.annotation.Keep ; +} + +-keepclasseswithmembers class * { + @androidx.annotation.Keep ; +} + +-keepclasseswithmembers class * { + @android.support.annotation.Keep (...); +} + +-keepclasseswithmembers class * { + @androidx.annotation.Keep (...); +} + +# These classes are duplicated between android.jar and org.apache.http.legacy.jar. +-dontnote org.apache.http.** +-dontnote android.net.http.** + +# These classes are duplicated between android.jar and core-lambda-stubs.jar. +-dontnote java.lang.invoke.** diff --git a/tools/android/proguard-android.txt b/tools/android/proguard-android.txt new file mode 100644 index 00000000000000..6d8b48194b1088 --- /dev/null +++ b/tools/android/proguard-android.txt @@ -0,0 +1,121 @@ +# Manually generated for Bazel using Android Gradle Plugin 7.1.2, the latest as of 2/26/22 +# To update, configure an app per https://developer.android.com/studio/build/shrink-code +# And after building the app for release, extract the two files from +# app/build/intermediates/default_proguard_files/global/ + +# This is a configuration file for ProGuard. +# http://proguard.sourceforge.net/index.html#manual/usage.html +# +# Starting with version 2.2 of the Android plugin for Gradle, this file is distributed together with +# the plugin and unpacked at build-time. The files in $ANDROID_HOME are no longer maintained and +# will be ignored by new version of the Android plugin for Gradle. + +# Optimization is turned off by default. Dex does not like code run +# through the ProGuard optimize steps (and performs some +# of these optimizations on its own). +# Note that if you want to enable optimization, you cannot just +# include optimization flags in your own project configuration file; +# instead you will need to point to the +# "proguard-android-optimize.txt" file instead of this one from your +# project.properties file. +-dontoptimize + +-dontusemixedcaseclassnames +-dontskipnonpubliclibraryclasses +-verbose + +# Preserve some attributes that may be required for reflection. +-keepattributes AnnotationDefault, + EnclosingMethod, + InnerClasses, + RuntimeVisibleAnnotations, + RuntimeVisibleParameterAnnotations, + RuntimeVisibleTypeAnnotations, + Signature + +-keep public class com.google.vending.licensing.ILicensingService +-keep public class com.android.vending.licensing.ILicensingService +-keep public class com.google.android.vending.licensing.ILicensingService +-dontnote com.android.vending.licensing.ILicensingService +-dontnote com.google.vending.licensing.ILicensingService +-dontnote com.google.android.vending.licensing.ILicensingService + +# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native +-keepclasseswithmembernames,includedescriptorclasses class * { + native ; +} + +# Keep setters in Views so that animations can still work. +-keepclassmembers public class * extends android.view.View { + void set*(***); + *** get*(); +} + +# We want to keep methods in Activity that could be used in the XML attribute onClick. +-keepclassmembers class * extends android.app.Activity { + public void *(android.view.View); +} + +# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations +-keepclassmembers enum * { + public static **[] values(); + public static ** valueOf(java.lang.String); +} + +-keepclassmembers class * implements android.os.Parcelable { + public static final ** CREATOR; +} + +# Preserve annotated Javascript interface methods. +-keepclassmembers class * { + @android.webkit.JavascriptInterface ; +} + +# The support libraries 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. +-dontnote android.support.** +-dontnote androidx.** +-dontwarn android.support.** +-dontwarn androidx.** + +# This class is deprecated, but remains for backward compatibility. +-dontwarn android.util.FloatMath + +# Understand the @Keep support annotation. +-keep class android.support.annotation.Keep +-keep class androidx.annotation.Keep + +-keep @android.support.annotation.Keep class * {*;} +-keep @androidx.annotation.Keep class * {*;} + +-keepclasseswithmembers class * { + @android.support.annotation.Keep ; +} + +-keepclasseswithmembers class * { + @androidx.annotation.Keep ; +} + +-keepclasseswithmembers class * { + @android.support.annotation.Keep ; +} + +-keepclasseswithmembers class * { + @androidx.annotation.Keep ; +} + +-keepclasseswithmembers class * { + @android.support.annotation.Keep (...); +} + +-keepclasseswithmembers class * { + @androidx.annotation.Keep (...); +} + +# These classes are duplicated between android.jar and org.apache.http.legacy.jar. +-dontnote org.apache.http.** +-dontnote android.net.http.** + +# These classes are duplicated between android.jar and core-lambda-stubs.jar. +-dontnote java.lang.invoke.**