Skip to content

Commit

Permalink
Introduce --incompatible_prohibit_aapt1 flag.
Browse files Browse the repository at this point in the history
    If this flag is active, AAPT2 is the only Android resource packaging tool in use and we prohibit methods of configuring the AAPT tool version.

    bazelbuild/bazel#10000

    PiperOrigin-RevId: 274703802
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent 2cc689c commit a6001a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,6 @@ public FragmentOptions getHost() {
oneVersionEnforcementUseTransitiveJarsForBinaryUnderTest;
host.persistentBusyboxTools = persistentBusyboxTools;

host.incompatibleProhibitAapt1 = incompatibleProhibitAapt1;

// Unless the build was started from an Android device, host means MAIN.
host.configurationDistinguisher = ConfigurationDistinguisher.MAIN;
return host;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.ActionAnalysisMetadata;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidAaptVersion;
import com.google.devtools.build.lib.rules.android.databinding.DataBinding;
import com.google.devtools.build.lib.rules.android.databinding.DataBindingContext;
import com.google.devtools.build.lib.testutil.MoreAsserts;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.Optional;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -377,10 +375,6 @@ public void testValidateAapt() throws Exception {
MergedAndroidResources merged = makeMergedResources(ruleContext, AndroidAaptVersion.AAPT);
ValidatedAndroidResources validated =
merged.validate(AndroidDataContext.forNative(ruleContext), AndroidAaptVersion.AAPT);
Set<String> actionMnemonics =
ruleContext.getAnalysisEnvironment().getRegisteredActions().stream()
.map(ActionAnalysisMetadata::getMnemonic)
.collect(ImmutableSet.toImmutableSet());

// Inherited values should be equal
assertThat(merged).isEqualTo(new MergedAndroidResources(validated));
Expand All @@ -391,14 +385,12 @@ public void testValidateAapt() throws Exception {
/* inputs = */ ImmutableList.of(validated.getMergedResources(), validated.getManifest()),
/* outputs = */ ImmutableList.of(
validated.getRTxt(), validated.getJavaSourceJar(), validated.getApk()));
assertThat(actionMnemonics).contains("AndroidResourceValidator"); // aapt1 validation

// aapt2 artifacts should not be generated
assertThat(validated.getCompiledSymbols()).isNull();
assertThat(validated.getAapt2RTxt()).isNull();
assertThat(validated.getAapt2SourceJar()).isNull();
assertThat(validated.getStaticLibrary()).isNull();
assertThat(actionMnemonics).doesNotContain("AndroidResourceLink"); // aapt2 validation
}

@Test
Expand Down Expand Up @@ -439,24 +431,6 @@ public void testValidateAapt2() throws Exception {
validated.getAapt2RTxt(), validated.getAapt2SourceJar(), validated.getStaticLibrary()));
}

@Test
public void testValidateNoAapt1() throws Exception {
useConfiguration("--incompatible_prohibit_aapt1");
RuleContext ruleContext = getRuleContext();

makeMergedResources(ruleContext, AndroidAaptVersion.AAPT2)
.validate(AndroidDataContext.forNative(ruleContext), AndroidAaptVersion.AAPT2);

Set<String> actionMnemonics =
ruleContext.getAnalysisEnvironment().getRegisteredActions().stream()
.map(ActionAnalysisMetadata::getMnemonic)
.collect(ImmutableSet.toImmutableSet());
// These are unfortunately the mnemonics used in Bazel; these should be changed once aapt1 is
// removed.
assertThat(actionMnemonics).contains("AndroidResourceLink"); // aapt2 validation
assertThat(actionMnemonics).doesNotContain("AndroidResourceValidator"); // aapt1 validation
}

@Test
public void testGenerateRClass() throws Exception {
RuleContext ruleContext = getRuleContext();
Expand Down Expand Up @@ -533,32 +507,28 @@ public void test_incompatibleProhibitAapt1_targetsAapt2() throws Exception {
public void test_incompatibleProhibitAapt1_aaptVersionAapt_throwsAttributeError()
throws Exception {
useConfiguration("--incompatible_prohibit_aapt1");
AssertionError e =
MoreAsserts.assertThrows(
AssertionError.class,
() ->
getRuleContext(
"android_binary",
"aapt_version = 'aapt',",
"manifest = 'AndroidManifest.xml',"));
assertThat(e).hasMessageThat().contains("aapt_version");
assertThat(e).hasMessageThat().contains("Attribute is no longer supported");
try {
getRuleContext(
"android_binary", "aapt_version = 'aapt',", "manifest = 'AndroidManifest.xml',");
fail("Expected AssertionError");
} catch (AssertionError e) {
assertThat(e).hasMessageThat().contains("aapt_version");
assertThat(e).hasMessageThat().contains("Attribute is no longer supported");
}
}

@Test
public void test_incompatibleProhibitAapt1_aaptVersionAapt2_throwsAttributeError()
throws Exception {
useConfiguration("--incompatible_prohibit_aapt1");
AssertionError e =
MoreAsserts.assertThrows(
AssertionError.class,
() ->
getRuleContext(
"android_binary",
"aapt_version = 'aapt2',",
"manifest = 'AndroidManifest.xml',"));
assertThat(e).hasMessageThat().contains("aapt_version");
assertThat(e).hasMessageThat().contains("Attribute is no longer supported");
try {
getRuleContext(
"android_binary", "aapt_version = 'aapt2',", "manifest = 'AndroidManifest.xml',");
fail("Expected AssertionError");
} catch (AssertionError e) {
assertThat(e).hasMessageThat().contains("aapt_version");
assertThat(e).hasMessageThat().contains("Attribute is no longer supported");
}
}

/**
Expand Down

0 comments on commit a6001a7

Please sign in to comment.