Move checking whether usages of proguard_apply_* attributes into existing validation method.

validateRuleContext() is renamed to validateContext().

PiperOrigin-RevId: 282020488
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java
index c85b84c..04100a1 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java
@@ -120,7 +120,8 @@
   }
 
   /** Checks expected rule invariants, throws rule errors if anything is set wrong. */
-  private static void validateRuleContext(RuleContext ruleContext) throws RuleErrorException {
+  private static void validateRuleContext(RuleContext ruleContext, AndroidDataContext dataContext)
+      throws RuleErrorException {
     if (getMultidexMode(ruleContext) != MultidexMode.LEGACY
         && ruleContext
             .attributes()
@@ -130,23 +131,33 @@
           "The 'main_dex_proguard_specs' attribute is only allowed if 'multidex' is"
               + " set to 'legacy'");
     }
-    if (ruleContext.attributes().isAttributeValueExplicitlySpecified("proguard_apply_mapping")
-        && ruleContext
-            .attributes()
-            .get(ProguardHelper.PROGUARD_SPECS, BuildType.LABEL_LIST)
-            .isEmpty()) {
-      ruleContext.throwWithAttributeError(
-          "proguard_apply_mapping",
-          "'proguard_apply_mapping' can only be used when 'proguard_specs' is also set");
+    if (ruleContext.attributes().isAttributeValueExplicitlySpecified("proguard_apply_mapping")) {
+      if (dataContext.throwOnProguardApplyMapping()) {
+        ruleContext.throwWithAttributeError(
+            "proguard_apply_mapping", "This attribute is not supported");
+      }
+      if (ruleContext
+          .attributes()
+          .get(ProguardHelper.PROGUARD_SPECS, BuildType.LABEL_LIST)
+          .isEmpty()) {
+        ruleContext.throwWithAttributeError(
+            "proguard_apply_mapping",
+            "'proguard_apply_mapping' can only be used when 'proguard_specs' is also set");
+      }
     }
-    if (ruleContext.attributes().isAttributeValueExplicitlySpecified("proguard_apply_dictionary")
-        && ruleContext
-            .attributes()
-            .get(ProguardHelper.PROGUARD_SPECS, BuildType.LABEL_LIST)
-            .isEmpty()) {
-      ruleContext.throwWithAttributeError(
-          "proguard_apply_dictionary",
-          "'proguard_apply_dictionary' can only be used when 'proguard_specs' is also set");
+    if (ruleContext.attributes().isAttributeValueExplicitlySpecified("proguard_apply_dictionary")) {
+      if (dataContext.throwOnProguardApplyDictionary()) {
+        ruleContext.throwWithAttributeError(
+            "proguard_apply_dictionary", "This attribute is not supported");
+      }
+      if (ruleContext
+          .attributes()
+          .get(ProguardHelper.PROGUARD_SPECS, BuildType.LABEL_LIST)
+          .isEmpty()) {
+        ruleContext.throwWithAttributeError(
+            "proguard_apply_dictionary",
+            "'proguard_apply_dictionary' can only be used when 'proguard_specs' is also set");
+      }
     }
 
     if (AndroidCommon.getAndroidConfig(ruleContext).desugarJava8Libs()
@@ -196,7 +207,8 @@
     ResourceDependencies resourceDeps =
         ResourceDependencies.fromRuleDeps(ruleContext, /* neverlink= */ false);
 
-    validateRuleContext(ruleContext);
+    AndroidDataContext dataContext = androidSemantics.makeContextForNative(ruleContext);
+    validateRuleContext(ruleContext, dataContext);
 
     NativeLibs nativeLibs =
         NativeLibs.fromLinkedNativeDeps(
@@ -208,7 +220,6 @@
     // Retrieve and compile the resources defined on the android_binary rule.
     AndroidResources.validateRuleContext(ruleContext);
 
-    final AndroidDataContext dataContext = androidSemantics.makeContextForNative(ruleContext);
     Map<String, String> manifestValues = StampedAndroidManifest.getManifestValues(ruleContext);
 
     StampedAndroidManifest manifest =
@@ -342,16 +353,6 @@
 
     Artifact proguardMapping =
         ruleContext.getPrerequisiteArtifact("proguard_apply_mapping", Mode.TARGET);
-    if (proguardMapping != null && dataContext.throwOnProguardApplyMapping()) {
-      throw ruleContext.throwWithAttributeError(
-          "proguard_apply_mapping", "This attribute is not supported");
-    }
-    Artifact proguardDictionary =
-        ruleContext.getPrerequisiteArtifact("proguard_apply_dictionary", Mode.TARGET);
-    if (proguardDictionary != null && dataContext.throwOnProguardApplyDictionary()) {
-      throw ruleContext.throwWithAttributeError(
-          "proguard_apply_dictionary", "This attribute is not supported");
-    }
 
     MobileInstallResourceApks mobileInstallResourceApks =
         AndroidBinaryMobileInstall.createMobileInstallResourceApks(
@@ -374,7 +375,6 @@
         ImmutableList.<Artifact>of(),
         ImmutableList.<Artifact>of(),
         proguardMapping,
-        proguardDictionary,
         oneVersionOutputArtifact);
   }
 
@@ -395,7 +395,6 @@
       ImmutableList<Artifact> apksUnderTest,
       ImmutableList<Artifact> additionalMergedManifests,
       Artifact proguardMapping,
-      Artifact proguardDictionary,
       @Nullable Artifact oneVersionEnforcementArtifact)
       throws InterruptedException, RuleErrorException {
 
@@ -440,7 +439,6 @@
             binaryJar,
             proguardSpecs,
             proguardMapping,
-            proguardDictionary,
             proguardOutputMap);
 
     if (dataContext.useResourceShrinking(hasProguardSpecs)) {
@@ -750,7 +748,6 @@
       Artifact deployJarArtifact,
       ImmutableList<Artifact> proguardSpecs,
       Artifact proguardMapping,
-      Artifact proguardDictionary,
       @Nullable Artifact proguardOutputMap)
       throws InterruptedException {
     Artifact proguardOutputJar =
@@ -784,6 +781,8 @@
         ruleContext.getImplicitOutputArtifact(JavaSemantics.JAVA_BINARY_PROGUARD_SEEDS);
     Artifact proguardUsage =
         ruleContext.getImplicitOutputArtifact(JavaSemantics.JAVA_BINARY_PROGUARD_USAGE);
+    Artifact proguardDictionary =
+        ruleContext.getPrerequisiteArtifact("proguard_apply_dictionary", Mode.TARGET);
     ProguardOutput result =
         ProguardHelper.createOptimizationActions(
             ruleContext,