From 41f4af25783f55e454bd692af35fb9f53e76695a Mon Sep 17 00:00:00 2001 From: Christopher Peterson Sauer Date: Thu, 24 Feb 2022 21:50:18 -0800 Subject: [PATCH] Add Android default ProGuard Specs to AndroidSdkProvider Android ProGuard actions need to be able to use proguard-android-optimize.txt or proguard-android.txt to be configured properly. This allows configurable access to those actions. Please see #14909 for context. --- .../lib/rules/android/AndroidSdkBase.java | 6 ++++++ .../lib/rules/android/AndroidSdkBaseRule.java | 10 ++++++++++ .../lib/rules/android/AndroidSdkProvider.java | 20 +++++++++++++++++++ .../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 | 2 ++ .../android_sdk_repository_template.bzl | 4 ++++ 14 files changed, 94 insertions(+) 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..b99b9f9b6a07a2 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,16 @@ 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), referred to 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), referred to 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/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..8bdf559c3549aa 100644 --- a/tools/android/BUILD.tools +++ b/tools/android/BUILD.tools @@ -441,6 +441,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..5987dfd6ff2750 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 = "tools/proguard/proguard-android-optimize.txt", + proguard_configuration_dont_optimize = "tools/proguard/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-binary", + proguard_configuration_dont_optimize = ":empty-binary", shrinked_android_jar = "dummy.jar", zipalign = ":empty-binary", tags = ["__ANDROID_RULES_MIGRATION__"],