Skip to content

Commit

Permalink
Add flag to turn Android resource merge conflicts from warnings into …
Browse files Browse the repository at this point in the history
…errors

RELNOTES: none
PiperOrigin-RevId: 161831232
  • Loading branch information
Googler authored and laszlocsomor committed Jul 14, 2017
1 parent fba07bb commit 5abf4ed
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class AarGeneratorBuilder {
private Artifact classes;

private Artifact aarOut;
private boolean throwOnResourceConflict;

private final RuleContext ruleContext;
private final SpawnAction.Builder builder;
Expand Down Expand Up @@ -79,6 +80,11 @@ public AarGeneratorBuilder setAAROut(Artifact aarOut) {
return this;
}

public AarGeneratorBuilder setThrowOnResourceConflict(boolean throwOnResourceConflict) {
this.throwOnResourceConflict = throwOnResourceConflict;
return this;
}

public void build(ActionConstructionContext context) {
List<Artifact> outs = new ArrayList<>();
List<Artifact> ins = new ArrayList<>();
Expand Down Expand Up @@ -115,6 +121,10 @@ public void build(ActionConstructionContext context) {
args.add(aarOut.getExecPathString());
outs.add(aarOut);

if (throwOnResourceConflict) {
args.add("--throwOnResourceConflict");
}

ruleContext.registerAction(
this.builder
.addInputs(ImmutableList.<Artifact>copyOf(ins))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,15 @@ public static class Options extends FragmentOptions {
)
public boolean generateRobolectricRClass;

@Option(
name = "experimental_android_throw_on_resource_conflict",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
effectTags = {OptionEffectTag.UNKNOWN},
help = "If passed, resource merge conflicts will be treated as errors instead of warnings"
)
public boolean throwOnResourceConflict;

@Override
public FragmentOptions getHost(boolean fallback) {
Options host = (Options) super.getHost(fallback);
Expand Down Expand Up @@ -725,6 +734,7 @@ public ImmutableSet<Class<? extends FragmentOptions>> requiredOptions() {
private final boolean exportsManifestDefault;
private final AndroidAaptVersion androidAaptVersion;
private final boolean generateRobolectricRClass;
private final boolean throwOnResourceConflict;
private final boolean useParallelDex2Oat;

AndroidConfiguration(Options options) throws InvalidConfigurationException {
Expand Down Expand Up @@ -760,6 +770,7 @@ public ImmutableSet<Class<? extends FragmentOptions>> requiredOptions() {
this.exportsManifestDefault = options.exportsManifestDefault;
this.androidAaptVersion = options.androidAaptVersion;
this.generateRobolectricRClass = options.generateRobolectricRClass;
this.throwOnResourceConflict = options.throwOnResourceConflict;
this.useParallelDex2Oat = options.useParallelDex2Oat;

if (!dexoptsSupportedInIncrementalDexing.contains("--no-locals")) {
Expand Down Expand Up @@ -889,6 +900,10 @@ boolean generateRobolectricRClass() {
return generateRobolectricRClass;
}

boolean throwOnResourceConflict() {
return throwOnResourceConflict;
}

@Override
public void addGlobalMakeVariables(ImmutableMap.Builder<String, String> globalMakeEnvBuilder) {
globalMakeEnvBuilder.put("ANDROID_CPU", cpu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ public ConfiguredTarget create(RuleContext ruleContext)
.withPrimary(resourceContainer)
.withDependencies(resourceApk.getResourceDependencies())
.setDebug(ruleContext.getConfiguration().getCompilationMode() != CompilationMode.OPT)
.setThrowOnResourceConflict(
ruleContext.getFragment(AndroidConfiguration.class).throwOnResourceConflict())
.build(ruleContext);
}

Expand All @@ -189,6 +191,8 @@ public ConfiguredTarget create(RuleContext ruleContext)
.withRtxt(primaryResources.getRTxt())
.withClasses(classesJar)
.setAAROut(aarOut)
.setThrowOnResourceConflict(
ruleContext.getFragment(AndroidConfiguration.class).throwOnResourceConflict())
.build(ruleContext);

RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class AndroidResourceMergingActionBuilder {

// Flags
private String customJavaPackage;
private boolean throwOnResourceConflict;

/** @param ruleContext The RuleContext that was used to create the SpawnAction.Builder. */
public AndroidResourceMergingActionBuilder(RuleContext ruleContext) {
Expand Down Expand Up @@ -116,6 +117,12 @@ public AndroidResourceMergingActionBuilder setJavaPackage(String customJavaPacka
return this;
}

public AndroidResourceMergingActionBuilder setThrowOnResourceConflict(
boolean throwOnResourceConflict) {
this.throwOnResourceConflict = throwOnResourceConflict;
return this;
}

public ResourceContainer build(ActionConstructionContext context) {
CustomCommandLine.Builder builder = new CustomCommandLine.Builder();

Expand Down Expand Up @@ -170,6 +177,10 @@ public ResourceContainer build(ActionConstructionContext context) {
outs.add(dataBindingInfoZip);
}

if (throwOnResourceConflict) {
builder.add("--throwOnResourceConflict");
}

SpawnAction.Builder spawnActionBuilder = new SpawnAction.Builder();
// Create the spawn action.
ruleContext.registerAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public class AndroidResourcesProcessorBuilder {
private Artifact featureOf;
private Artifact featureAfter;
private AndroidAaptVersion aaptVersion;
private boolean throwOnResourceConflict;

/**
* @param ruleContext The RuleContext that was used to create the SpawnAction.Builder.
Expand Down Expand Up @@ -232,6 +233,12 @@ public AndroidResourcesProcessorBuilder targetAaptVersion(AndroidAaptVersion aap
return this;
}

public AndroidResourcesProcessorBuilder setThrowOnResourceConflict(
boolean throwOnResourceConflict) {
this.throwOnResourceConflict = throwOnResourceConflict;
return this;
}

public ResourceContainer build(ActionConstructionContext context) {
if (aaptVersion == AndroidAaptVersion.AAPT2) {
return createAapt2ApkAction(context);
Expand Down Expand Up @@ -467,5 +474,9 @@ private void configureCommonFlags(
builder.addExecPath("--featureAfter", featureAfter);
inputs.add(featureAfter);
}

if (throwOnResourceConflict) {
builder.add("--throwOnResourceConflict");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,10 @@ private ResourceApk createApk(
.setMergedResourcesOut(mergedResources)
.setManifestOut(manifestOut)
.setClassJarOut(rJavaClassJar)
.setDataBindingInfoZip(dataBindingInfoZip);
.setDataBindingInfoZip(dataBindingInfoZip)
.setThrowOnResourceConflict(
ruleContext.getConfiguration()
.getFragment(AndroidConfiguration.class).throwOnResourceConflict());
ResourceContainer merged = resourcesMergerBuilder.build(ruleContext);

processed =
Expand Down Expand Up @@ -642,7 +645,10 @@ private ResourceApk createApk(
.setVersionCode(manifestValues.get("versionCode"))
.setVersionName(manifestValues.get("versionName"))
.setFeatureOf(featureOf)
.setFeatureAfter(featureAfter);
.setFeatureAfter(featureAfter)
.setThrowOnResourceConflict(
ruleContext.getConfiguration()
.getFragment(AndroidConfiguration.class).throwOnResourceConflict());
if (!incremental) {
builder
.targetAaptVersion(AndroidAaptVersion.AAPT)
Expand Down
Loading

0 comments on commit 5abf4ed

Please sign in to comment.