Skip to content

Commit

Permalink
Configure ProGuard for Android
Browse files Browse the repository at this point in the history
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 bazelbuild#14909 for more context.
  • Loading branch information
cpsauer committed Mar 1, 2022
1 parent e33b54e commit 8ee2df2
Show file tree
Hide file tree
Showing 18 changed files with 370 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,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()
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 @@ -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;
Expand Down Expand Up @@ -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.<Artifact>builder()
.add(opt ? sdk.getProguardConfigurationOptimize()
: sdk.getProguardConfigurationDontOptimize())
.addAll(proguardSpecs)
.build();

ProguardOutput output =
getProguardOutputs(
proguardOutputJar,
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 @@ -251,6 +251,12 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten
" cmd = 'touch $@',",
" executable = 1,",
")",
"genrule(",
" name = 'empty-proguard-mocks-android-default',",
" srcs = [],",
" outs = ['empty-proguard-mocks-android-default.pro'],",
" cmd = 'touch $@',",
")",
"android_sdk(",
" name = 'invalid-fallback-sdk',",
" aapt = ':empty_binary',",
Expand All @@ -264,6 +270,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-proguard-mocks-android-default.pro',",
" proguard_configuration_dont_optimize = 'empty-proguard-mocks-android-default.pro',",
" shrinked_android_jar = 'dummy.jar',",
" zipalign = ':empty_binary',",
" tags = ['__ANDROID_RULES_MIGRATION__'],",
Expand Down Expand Up @@ -415,6 +423,8 @@ private ImmutableList<String> createAndroidBuildContents() {
" main_dex_classes = ':mainDexClasses.rules',",
" main_dex_list_creator = ':main_dex_list_creator',",
" proguard = ':ProGuard',",
" proguard_configuration_optimize = 'empty-proguard-mocks-android-default.pro',",
" proguard_configuration_dont_optimize = 'empty-proguard-mocks-android-default.pro',",
" 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 @@ -58,6 +58,7 @@ public void setup() throws Exception {
scratch.file(
"embedded_tools/tools/android/android_sdk_repository_template.bzl",
ResourceLoader.readFromResources("tools/android/android_sdk_repository_template.bzl"));
scratch.appendFile("embedded_tools/tools/android/BUILD", "exports_files(['proguard-android-optimize.txt', 'proguard-android.txt'])");
scratch.appendFile("WORKSPACE", "local_config_platform(name='local_config_platform')");
}

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 Expand Up @@ -4094,6 +4100,7 @@ public void testCommandLineForMultipleProguardSpecs() throws Exception {
// Only one combined library jar
"-libraryjars",
execPathEndingWith(action.getInputs(), "legacy_b_combined_library_jars.jar"),
"@" + execPathEndingWith(action.getInputs(), "/empty-proguard-mocks-android-default.pro"),
"@" + execPathEndingWith(action.getInputs(), "b_proguard.cfg"),
"@java/com/google/android/hello/proguard-spec.pro",
"@java/com/google/android/hello/proguard-spec1.pro",
Expand Down Expand Up @@ -4137,6 +4144,7 @@ public void testNoDuplicatesInProguardCommand() throws Exception {
// Only one combined library jar
"-libraryjars",
execPathEndingWith(action.getInputs(), "legacy_b_combined_library_jars.jar"),
"@" + execPathEndingWith(action.getInputs(), "/empty-proguard-mocks-android-default.pro"),
"@" + execPathEndingWith(action.getInputs(), "b_proguard.cfg"),
"@java/com/google/android/hello/proguard-spec.pro",
"@java/com/google/android/hello/proguard-spec1.pro",
Expand Down Expand Up @@ -4233,6 +4241,7 @@ public void testLegacyOptimizationModeUsesExtraProguardSpecs() throws Exception
assertThat(prettyArtifactNames(action.getInputs())).containsNoDuplicates();
assertThat(Collections2.filter(action.getArguments(), arg -> arg.startsWith("@")))
.containsExactly(
"@" + execPathEndingWith(action.getInputs(), "/empty-proguard-mocks-android-default.pro"),
"@" + execPathEndingWith(action.getInputs(), "/proguard-spec.pro"),
"@" + execPathEndingWith(action.getInputs(), "/_b_proguard.cfg"),
"@java/com/google/android/hello/extra.pro");
Expand Down Expand Up @@ -4264,6 +4273,7 @@ public void testExtraProguardSpecsDontDuplicateProguardInputFiles() throws Excep
assertThat(prettyArtifactNames(action.getInputs())).containsNoDuplicates();
assertThat(Collections2.filter(action.getArguments(), arg -> arg.startsWith("@")))
.containsExactly(
"@" + execPathEndingWith(action.getInputs(), "/empty-proguard-mocks-android-default.pro"),
"@java/com/google/android/hello/proguard-spec.pro",
"@" + execPathEndingWith(action.getInputs(), "/_b_proguard.cfg"));
}
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
Loading

0 comments on commit 8ee2df2

Please sign in to comment.