Skip to content

Commit

Permalink
Add --incompatible_use_aapt2_by_default incompatible change (IC) fla…
Browse files Browse the repository at this point in the history
…g for enabling aapt2 by default in Bazel.

    FR Ref: bazelbuild/bazel#4103
    IC Ref: bazelbuild/bazel#6907

    RELNOTES: New incompatible change flag for defaulting to aapt2 in Android builds: `--incompatible_use_aapt2_by_default`. To build with aapt2 today, pass the flag `--incompatible_use_aapt2_by_default=true` or `--android_aapt=aapt2`, or set the `aapt_version`  to `aapt2` on your `android_binary` or `android_local_test` target.
    PiperOrigin-RevId: 225593174
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent b95d87f commit 5e75f62
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -745,19 +745,6 @@ public static class Options extends FragmentOptions {
help = "Use android databinding v2")
public boolean dataBindingV2;

@Option(
name = "android_databinding_use_v3_4_args",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
effectTags = {
OptionEffectTag.AFFECTS_OUTPUTS,
OptionEffectTag.LOADING_AND_ANALYSIS,
OptionEffectTag.LOSES_INCREMENTAL_STATE,
},
metadataTags = OptionMetadataTag.EXPERIMENTAL,
help = "Use android databinding v2 with 3.4.0 argument")
public boolean dataBindingUpdatedArgs;

@Option(
name = "experimental_android_library_exports_manifest_default",
defaultValue = "false",
Expand Down Expand Up @@ -1009,7 +996,6 @@ public ImmutableSet<Class<? extends FragmentOptions>> requiredOptions() {
private final boolean checkForMigrationTag;
private final boolean oneVersionEnforcementUseTransitiveJarsForBinaryUnderTest;
private final boolean dataBindingV2;
private final boolean dataBindingUpdatedArgs;
private final boolean persistentBusyboxTools;
private final boolean filterRJarsFromAndroidTest;

Expand Down Expand Up @@ -1057,7 +1043,6 @@ private AndroidConfiguration(Options options) throws InvalidConfigurationExcepti
this.oneVersionEnforcementUseTransitiveJarsForBinaryUnderTest =
options.oneVersionEnforcementUseTransitiveJarsForBinaryUnderTest;
this.dataBindingV2 = options.dataBindingV2;
this.dataBindingUpdatedArgs = options.dataBindingUpdatedArgs;
this.persistentBusyboxTools = options.persistentBusyboxTools;
this.filterRJarsFromAndroidTest = options.filterRJarsFromAndroidTest;
this.incompatibleUseAapt2ByDefault = options.incompatibleUseAapt2ByDefault;
Expand Down Expand Up @@ -1267,11 +1252,6 @@ public boolean useDataBindingV2() {
return dataBindingV2;
}

@Override
public boolean useDataBindingUpdatedArgs() {
return dataBindingUpdatedArgs;
}

@Override
public boolean persistentBusyboxTools() {
return persistentBusyboxTools;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public class AndroidResourcesTest extends ResourceTestBase {
private static final ImmutableList<PathFragment> RESOURCES_ROOTS =
ImmutableList.of(DEFAULT_RESOURCE_ROOT);

@Before
public void setupCcToolchain() throws Exception {
getAnalysisMock().ccSupport().setupCcToolchainConfigForCpu(mockToolsConfig, "armeabi-v7a");
}

@Before
@Test
public void testGetResourceRootsNoResources() throws Exception {
Expand Down Expand Up @@ -230,14 +225,15 @@ private Optional<? extends AndroidResources> assertFilter(

@Test
public void testParseNoCompile() throws Exception {
useConfiguration("--android_aapt=aapt");

RuleContext ruleContext = getRuleContext();
ParsedAndroidResources parsed =
assertParse(
ruleContext,
DataBinding.contextFrom(
ruleContext,
ruleContext.getConfiguration().getFragment(AndroidConfiguration.class)),
AndroidAaptVersion.AAPT);
ruleContext.getConfiguration().getFragment(AndroidConfiguration.class)));

// Since we are not using aapt2, there should be no compiled symbols
assertThat(parsed.getCompiledSymbols()).isNull();
Expand All @@ -251,8 +247,11 @@ public void testParseNoCompile() throws Exception {

@Test
public void testParseAndCompile() throws Exception {
mockAndroidSdkWithAapt2();
useConfiguration("--android_sdk=//sdk:sdk", "--android_aapt=aapt2");

RuleContext ruleContext = getRuleContext();
ParsedAndroidResources parsed = assertParse(ruleContext, AndroidAaptVersion.AAPT2);
ParsedAndroidResources parsed = assertParse(ruleContext);

assertThat(parsed.getCompiledSymbols()).isNotNull();

Expand All @@ -272,9 +271,12 @@ public void testParseAndCompile() throws Exception {

@Test
public void testParseWithDataBinding() throws Exception {
mockAndroidSdkWithAapt2();
useConfiguration("--android_sdk=//sdk:sdk", "--android_aapt=aapt2");

RuleContext ruleContext = getRuleContextWithDataBinding();

ParsedAndroidResources parsed = assertParse(ruleContext, AndroidAaptVersion.AAPT2);
ParsedAndroidResources parsed = assertParse(ruleContext);

// The parse action should take resources and busybox artifacts in and output symbols
assertActionArtifacts(
Expand All @@ -297,13 +299,15 @@ public void testParseWithDataBinding() throws Exception {

@Test
public void testMergeDataBinding() throws Exception {
useConfiguration("--android_aapt=aapt");

RuleContext ruleContext = getRuleContextWithDataBinding();
ParsedAndroidResources parsed = assertParse(ruleContext, AndroidAaptVersion.AAPT);
ParsedAndroidResources parsed = assertParse(ruleContext);
MergedAndroidResources merged =
parsed.merge(
AndroidDataContext.forNative(ruleContext),
ResourceDependencies.empty(),
AndroidAaptVersion.AAPT);
AndroidAaptVersion.chooseTargetAaptVersion(ruleContext));

// Besides processed manifest, inherited values should be equal
assertThat(parsed).isEqualTo(new ParsedAndroidResources(merged, parsed.getStampedManifest()));
Expand All @@ -329,8 +333,12 @@ public void testMergeDataBinding() throws Exception {

@Test
public void testMergeCompiled() throws Exception {
mockAndroidSdkWithAapt2();
useConfiguration(
"--android_sdk=//sdk:sdk", "--android_aapt=aapt2", "--experimental_skip_parsing_action");

RuleContext ruleContext = getRuleContext();
ParsedAndroidResources parsed = assertParse(ruleContext, AndroidAaptVersion.AAPT2);
ParsedAndroidResources parsed = assertParse(ruleContext);
MergedAndroidResources merged =
parsed.merge(
AndroidDataContext.forNative(ruleContext),
Expand Down Expand Up @@ -369,11 +377,14 @@ public void testMergeCompiled() throws Exception {

@Test
public void testValidateAapt() throws Exception {
useConfiguration("--android_aapt=aapt");
RuleContext ruleContext = getRuleContext();

MergedAndroidResources merged = makeMergedResources(ruleContext, AndroidAaptVersion.AAPT);
MergedAndroidResources merged = makeMergedResources(ruleContext);
ValidatedAndroidResources validated =
merged.validate(AndroidDataContext.forNative(ruleContext), AndroidAaptVersion.AAPT);
merged.validate(
AndroidDataContext.forNative(ruleContext),
AndroidAaptVersion.chooseTargetAaptVersion(ruleContext));

// Inherited values should be equal
assertThat(merged).isEqualTo(new MergedAndroidResources(validated));
Expand All @@ -394,11 +405,15 @@ public void testValidateAapt() throws Exception {

@Test
public void testValidateAapt2() throws Exception {
mockAndroidSdkWithAapt2();
useConfiguration("--android_sdk=//sdk:sdk", "--android_aapt=aapt2");
RuleContext ruleContext = getRuleContext();

MergedAndroidResources merged = makeMergedResources(ruleContext, AndroidAaptVersion.AAPT2);
MergedAndroidResources merged = makeMergedResources(ruleContext);
ValidatedAndroidResources validated =
merged.validate(AndroidDataContext.forNative(ruleContext), AndroidAaptVersion.AAPT2);
merged.validate(
AndroidDataContext.forNative(ruleContext),
AndroidAaptVersion.chooseTargetAaptVersion(ruleContext));

// Inherited values should be equal
assertThat(merged).isEqualTo(new MergedAndroidResources(validated));
Expand Down Expand Up @@ -438,12 +453,12 @@ public void testGenerateRClass() throws Exception {

ProcessedAndroidData processedData =
ProcessedAndroidData.of(
makeParsedResources(ruleContext, AndroidAaptVersion.AAPT),
makeParsedResources(ruleContext),
AndroidAssets.from(ruleContext)
.process(
AndroidDataContext.forNative(ruleContext),
AssetDependencies.empty(),
AndroidAaptVersion.AAPT),
AndroidAaptVersion.chooseTargetAaptVersion(ruleContext)),
manifest,
rTxt,
ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_JAVA_SOURCE_JAR),
Expand Down Expand Up @@ -498,32 +513,39 @@ public void testProcessBinaryDataGeneratesProguardOutput() throws Exception {

@Test
public void test_incompatibleUseAapt2ByDefaultEnabled_targetsAapt2() throws Exception {
useConfiguration("--incompatible_use_aapt2_by_default");
mockAndroidSdkWithAapt2();
useConfiguration("--android_sdk=//sdk:sdk", "--incompatible_use_aapt2_by_default");
RuleContext ruleContext =
getRuleContext(
"android_binary", "aapt_version = 'auto',", "manifest = 'AndroidManifest.xml',");
assertThat(AndroidAaptVersion.chooseTargetAaptVersion(ruleContext))
.isEqualTo(AndroidAaptVersion.AAPT2);
}

@Test
public void test_incompatibleUseAapt2ByDefaultDisabled_targetsAapt() throws Exception {
mockAndroidSdkWithAapt2();
useConfiguration("--android_sdk=//sdk:sdk", "--noincompatible_use_aapt2_by_default");
RuleContext ruleContext =
getRuleContext(
"android_binary", "aapt_version = 'auto',", "manifest = 'AndroidManifest.xml',");
assertThat(AndroidAaptVersion.chooseTargetAaptVersion(ruleContext))
.isEqualTo(AndroidAaptVersion.AAPT);
}

/**
* Validates that a parse action was invoked correctly. Returns the {@link ParsedAndroidResources}
* for further validation.
*/
private ParsedAndroidResources assertParse(
RuleContext ruleContext, AndroidAaptVersion aaptVersion) throws Exception {
private ParsedAndroidResources assertParse(RuleContext ruleContext) throws Exception {
return assertParse(
ruleContext,
DataBinding.contextFrom(
ruleContext, ruleContext.getConfiguration().getFragment(AndroidConfiguration.class)),
aaptVersion);
ruleContext, ruleContext.getConfiguration().getFragment(AndroidConfiguration.class)));
}

private ParsedAndroidResources assertParse(
RuleContext ruleContext,
DataBindingContext dataBindingContext,
AndroidAaptVersion aaptVersion)
throws Exception {
RuleContext ruleContext, DataBindingContext dataBindingContext) throws Exception {
ImmutableList<Artifact> resources = getResources("values-en/foo.xml", "drawable-hdpi/bar.png");
AndroidResources raw =
new AndroidResources(
Expand All @@ -532,7 +554,10 @@ private ParsedAndroidResources assertParse(

ParsedAndroidResources parsed =
raw.parse(
AndroidDataContext.forNative(ruleContext), manifest, aaptVersion, dataBindingContext);
AndroidDataContext.forNative(ruleContext),
manifest,
AndroidAaptVersion.chooseTargetAaptVersion(ruleContext),
dataBindingContext);

// Inherited values should be equal
assertThat(raw).isEqualTo(new AndroidResources(parsed));
Expand All @@ -543,37 +568,33 @@ private ParsedAndroidResources assertParse(
return parsed;
}

private MergedAndroidResources makeMergedResources(
RuleContext ruleContext, AndroidAaptVersion aaptVersion)
private MergedAndroidResources makeMergedResources(RuleContext ruleContext)
throws RuleErrorException, InterruptedException {
return makeParsedResources(ruleContext, aaptVersion)
return makeParsedResources(ruleContext)
.merge(
AndroidDataContext.forNative(ruleContext),
ResourceDependencies.fromRuleDeps(ruleContext, /* neverlink = */ false),
aaptVersion);
AndroidAaptVersion.chooseTargetAaptVersion(ruleContext));
}

private ParsedAndroidResources makeParsedResources(
RuleContext ruleContext, AndroidAaptVersion aaptVersion)
private ParsedAndroidResources makeParsedResources(RuleContext ruleContext)
throws RuleErrorException, InterruptedException {
DataBindingContext dataBindingContext =
DataBinding.contextFrom(
ruleContext, ruleContext.getConfiguration().getFragment(AndroidConfiguration.class));
return makeParsedResources(ruleContext, dataBindingContext, aaptVersion);
DataBinding.contextFrom(ruleContext,
ruleContext.getConfiguration().getFragment(AndroidConfiguration.class));
return makeParsedResources(ruleContext, dataBindingContext);
}

private ParsedAndroidResources makeParsedResources(
RuleContext ruleContext,
DataBindingContext dataBindingContext,
AndroidAaptVersion aaptVersion)
RuleContext ruleContext, DataBindingContext dataBindingContext)
throws RuleErrorException, InterruptedException {
ImmutableList<Artifact> resources = getResources("values-en/foo.xml", "drawable-hdpi/bar.png");
return new AndroidResources(
resources, AndroidResources.getResourceRoots(ruleContext, resources, "resource_files"))
.parse(
AndroidDataContext.forNative(ruleContext),
getManifest(),
aaptVersion,
AndroidAaptVersion.chooseTargetAaptVersion(ruleContext),
dataBindingContext);
}

Expand Down

0 comments on commit 5e75f62

Please sign in to comment.