Skip to content

Commit

Permalink
Add Android default ProGuard Specs to AndroidSdkProvider
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cpsauer committed Feb 25, 2022
1 parent e33b54e commit 5ec44de
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -98,6 +102,8 @@ public ConfiguredTarget create(RuleContext ruleContext)
apkBuilder,
apkSigner,
proguard,
proguardConfigurationOptimize,
proguardConfigurationDontOptimize,
zipalign,
system,
legacyMainDexListGenerator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -339,6 +355,8 @@ public AndroidSdkProvider createInfo(
Object apkBuilder,
FilesToRunProvider apkSigner,
FilesToRunProvider proguard,
Artifact proguardConfigurationOptimize,
Artifact proguardConfigurationDontOptimize,
FilesToRunProvider zipalign,
Object system,
Object legacyMainDexListGenerator)
Expand All @@ -360,6 +378,8 @@ public AndroidSdkProvider createInfo(
fromNoneable(apkBuilder, FilesToRunProvider.class),
apkSigner,
proguard,
proguardConfigurationOptimize,
proguardConfigurationDontOptimize,
zipalign,
fromNoneable(system, BootClassPathInfo.class),
fromNoneable(legacyMainDexListGenerator, FilesToRunProvider.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -291,6 +307,8 @@ AndroidSdkProviderApi<FileT, FilesToRunProviderT, TransT> createInfo(
Object apkBuilder,
FilesToRunProviderT apkSigner,
FilesToRunProviderT proguard,
FileT proguardConfigurationOptimize,
FileT proguardConfigurationDontOptimize,
FilesToRunProviderT zipalign,
Object system,
Object legacyMainDexListGenerator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ public void setupMockClient(MockToolsConfig config, List<String> 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__'],",
Expand Down Expand Up @@ -415,6 +417,8 @@ private ImmutableList<String> 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__'],",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__'],",
Expand All @@ -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__'],",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__'],",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__'])");
Expand Down Expand Up @@ -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',",
Expand Down Expand Up @@ -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__'])");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__'],",
Expand Down Expand Up @@ -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',",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__'],",
Expand Down Expand Up @@ -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__'],",
Expand Down Expand Up @@ -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__'],",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__'],",
Expand Down Expand Up @@ -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__'],",
Expand Down Expand Up @@ -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__'],",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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__'],",
Expand Down
2 changes: 2 additions & 0 deletions tools/android/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down
4 changes: 4 additions & 0 deletions tools/android/android_sdk_repository_template.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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__"],
Expand Down

0 comments on commit 5ec44de

Please sign in to comment.