Skip to content

Commit

Permalink
Add option to omit ResourcesInfo provider from android_binary rules.
Browse files Browse the repository at this point in the history
This usually has unintended side effects, e.g. when an android_test has an
android_binary in 'deps' for the purposes of automated install, resources from
the binary were pulled into the test APK.

RELNOTES: None
PiperOrigin-RevId: 250503231
  • Loading branch information
Googler authored and copybara-github committed May 29, 2019
1 parent c3b9ae6 commit 926cfbd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,18 @@ public RuleConfiguredTargetBuilder addTransitiveInfoProviders(
.setNeverlink(isNeverlink)
.build();

resourceApk.addToConfiguredTargetBuilder(
builder, ruleContext.getLabel(), /* includeSkylarkApiProvider = */ true, isLibrary);
if (ruleContext
.getFragment(AndroidConfiguration.class)
.omitResourcesInfoProviderFromAndroidBinary()
&& !isLibrary) {
// Binary rule; allow extracting merged manifest from Starlark via
// ctx.attr.android_binary.android.merged_manifest, but not much more.
builder.addSkylarkTransitiveInfo(
AndroidSkylarkApiProvider.NAME, new AndroidSkylarkApiProvider(/*resourceInfo=*/ null));
} else {
resourceApk.addToConfiguredTargetBuilder(
builder, ruleContext.getLabel(), /* includeSkylarkApiProvider = */ true, isLibrary);
}

return builder
.setFilesToBuild(filesToBuild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,16 @@ public static class Options extends FragmentOptions {
+ " and uses the output of the compile action instead for resource merging.")
public boolean skipParsingAction;

@Option(
name = "experimental_omit_resources_info_provider_from_android_binary",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
effectTags = {OptionEffectTag.AFFECTS_OUTPUTS},
help =
"Omit AndroidResourcesInfo provider from android_binary rules."
+ " Propagating resources out to other binaries is usually unintentional.")
public boolean omitResourcesInfoProviderFromAndroidBinary;

@Option(
name = "android_fixed_resource_neverlinking",
defaultValue = "false",
Expand Down Expand Up @@ -1072,6 +1082,7 @@ public ImmutableSet<Class<? extends FragmentOptions>> requiredOptions() {
private final boolean throwOnResourceConflict;
private final boolean useParallelDex2Oat;
private final boolean skipParsingAction;
private final boolean omitResourcesInfoProviderFromAndroidBinary;
private final boolean fixedResourceNeverlinking;
private final AndroidRobolectricTestDeprecationLevel robolectricTestDeprecationLevel;
private final boolean checkForMigrationTag;
Expand Down Expand Up @@ -1121,6 +1132,8 @@ private AndroidConfiguration(Options options) throws InvalidConfigurationExcepti
this.throwOnResourceConflict = options.throwOnResourceConflict;
this.useParallelDex2Oat = options.useParallelDex2Oat;
this.skipParsingAction = options.skipParsingAction;
this.omitResourcesInfoProviderFromAndroidBinary =
options.omitResourcesInfoProviderFromAndroidBinary;
this.fixedResourceNeverlinking = options.fixedResourceNeverlinking;
this.robolectricTestDeprecationLevel = options.robolectricTestDeprecationLevel;
// use --incompatible_disable_native_android_rules, and also the old flag for backwards
Expand Down Expand Up @@ -1328,6 +1341,11 @@ public boolean skipParsingAction() {
return this.skipParsingAction;
}

@Override
public boolean omitResourcesInfoProviderFromAndroidBinary() {
return this.omitResourcesInfoProviderFromAndroidBinary;
}

@Override
public boolean fixedResourceNeverlinking() {
return this.fixedResourceNeverlinking;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ public interface AndroidConfigurationApi {
@SkylarkCallable(name = "skip_parsing_action", structField = true, doc = "", documented = false)
boolean skipParsingAction();

@SkylarkCallable(
name = "omit_resources_info_provider_from_android_binary",
structField = true,
doc = "",
documented = false)
boolean omitResourcesInfoProviderFromAndroidBinary();

@SkylarkCallable(
name = "fixed_resource_neverlinking",
structField = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,38 @@ private void checkDexMainListOpts(String mainDexListOpts, String... expectedArgs
MoreAsserts.assertContainsSublist(args, expectedArgs);
}

@Test
public void omitResourcesInfoProviderFromAndroidBinary_enabled() throws Exception {
useConfiguration("--experimental_omit_resources_info_provider_from_android_binary");
ConfiguredTarget binary =
scratchConfiguredTarget(
"java/com/pkg/myapp",
"myapp",
"android_binary(",
" name = 'myapp',",
" manifest = 'AndroidManifest.xml',",
" resource_files = glob(['res/**/*']),",
")");

assertThat(binary.get(AndroidResourcesInfo.PROVIDER)).isNull();
}

@Test
public void omitResourcesInfoProviderFromAndroidBinary_disabled() throws Exception {
useConfiguration("--noexperimental_omit_resources_info_provider_from_android_binary");
ConfiguredTarget binary =
scratchConfiguredTarget(
"java/com/pkg/myapp",
"myapp",
"android_binary(",
" name = 'myapp',",
" manifest = 'AndroidManifest.xml',",
" resource_files = glob(['res/**/*']),",
")");

assertThat(binary.get(AndroidResourcesInfo.PROVIDER)).isNotNull();
}

@Test
public void testResourceConfigurationFilters() throws Exception {
scratch.file("java/com/google/android/BUILD",
Expand Down

0 comments on commit 926cfbd

Please sign in to comment.