aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/android
diff options
context:
space:
mode:
authorGravatar Michael Staib <mstaib@google.com>2017-01-04 19:18:12 +0000
committerGravatar John Cater <jcater@google.com>2017-01-04 20:40:17 +0000
commit0b06ac45882d8c23f4f53817a0b6e7b3dffc8957 (patch)
treedd7e0456fd86ec609bfbb5cd6f962c98671083c1 /src/main/java/com/google/devtools/build/lib/rules/android
parentf21d18e1b2643c5b9814b3f2803619ed15550beb (diff)
Refactor ResourceContainer: make top level and use AutoValue Builder support.
This merges the AndroidResourceContainerBuilder (which it's not even clear is related to the nested ResourceContainer!) into the newly generated ResourceContainer.Builder. It also seemed ridiculous for ResourceContainer to get so large and still be subordinate to AndroidResourcesProvider, especially when it's getting passed around in a lot of other places (look how many imports needed fixing!). This CL makes it its own top level class. This allows for easy modification of an existing instance: call toBuilder on it, set the properties you want set, and then call build. -- PiperOrigin-RevId: 143574468 MOS_MIGRATED_REVID=143574468
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/android')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/AarGeneratorBuilder.java4
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/AndroidAaptActionHelper.java11
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java5
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/AndroidLibrary.java27
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceContainerBuilder.java140
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceMergingActionBuilder.java24
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceValidatorActionBuilder.java22
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProcessorBuilder.java36
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProvider.java117
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/AndroidSkylarkApiProvider.java3
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/ApplicationManifest.java69
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/LocalResourceContainer.java2
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java1
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/ResourceApk.java8
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/ResourceContainer.java321
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/ResourceContainerConverter.java3
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/ResourceDependencies.java1
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/ResourceShrinkerActionBuilder.java2
18 files changed, 403 insertions, 393 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AarGeneratorBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/android/AarGeneratorBuilder.java
index 776f2152cd..4a42915da3 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AarGeneratorBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AarGeneratorBuilder.java
@@ -25,9 +25,7 @@ import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext;
import com.google.devtools.build.lib.analysis.actions.CommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceType;
-
+import com.google.devtools.build.lib.rules.android.ResourceContainer.ResourceType;
import java.util.ArrayList;
import java.util.List;
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAaptActionHelper.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAaptActionHelper.java
index acf004d3b0..55253a0296 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAaptActionHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidAaptActionHelper.java
@@ -25,16 +25,13 @@ import com.google.devtools.build.lib.analysis.actions.CommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.actions.SpawnAction.Builder;
import com.google.devtools.build.lib.analysis.config.CompilationMode;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceType;
+import com.google.devtools.build.lib.rules.android.ResourceContainer.ResourceType;
import com.google.devtools.build.lib.vfs.PathFragment;
-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
-
import javax.annotation.Nullable;
/**
@@ -70,7 +67,7 @@ public final class AndroidAaptActionHelper {
inputs.add(AndroidSdkProvider.fromRuleContext(ruleContext).getAndroidJar());
inputs.add(manifest);
Iterables.addAll(inputs, Iterables.concat(Iterables.transform(resourceContainers,
- new Function<AndroidResourcesProvider.ResourceContainer, Iterable<Artifact>>() {
+ new Function<ResourceContainer, Iterable<Artifact>>() {
@Override
public Iterable<Artifact> apply(ResourceContainer container) {
return container.getArtifacts();
@@ -207,7 +204,7 @@ public final class AndroidAaptActionHelper {
args.add(outputPath.getPathString());
// First make sure path elements are unique
Collection<String> paths = new LinkedHashSet<>();
- for (AndroidResourcesProvider.ResourceContainer container : resourceContainers) {
+ for (ResourceContainer container : resourceContainers) {
for (Artifact artifact : container.getArtifacts(resourceType)) {
paths.add(artifact.getExecPathString());
}
@@ -238,7 +235,7 @@ public final class AndroidAaptActionHelper {
List<String> dirArgs = new ArrayList<>();
Collection<String> paths = new LinkedHashSet<>();
// First make sure roots are unique
- for (AndroidResourcesProvider.ResourceContainer container : resourceContainers) {
+ for (ResourceContainer container : resourceContainers) {
for (PathFragment root : container.getRoots(resourceType)) {
paths.add(outputPath.getRelative(root).getPathString());
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java
index a3fda94283..2b483800f9 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java
@@ -40,9 +40,8 @@ import com.google.devtools.build.lib.packages.AggregatingAttributeMapper;
import com.google.devtools.build.lib.packages.AttributeMap;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.Rule;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceType;
import com.google.devtools.build.lib.rules.android.AndroidRuleClasses.MultidexMode;
+import com.google.devtools.build.lib.rules.android.ResourceContainer.ResourceType;
import com.google.devtools.build.lib.rules.cpp.CcLinkParams;
import com.google.devtools.build.lib.rules.cpp.CcLinkParamsProvider;
import com.google.devtools.build.lib.rules.cpp.CcLinkParamsStore;
@@ -821,7 +820,7 @@ public class AndroidCommon {
public static PathFragment getAssetDir(RuleContext ruleContext) {
return new PathFragment(ruleContext.attributes().get(
- AndroidResourcesProvider.ResourceType.ASSETS.getAttribute() + "_dir",
+ ResourceType.ASSETS.getAttribute() + "_dir",
Type.STRING));
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLibrary.java
index 8d37d2d12c..c3e0fab4c7 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLibrary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLibrary.java
@@ -27,8 +27,7 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
import com.google.devtools.build.lib.rules.android.AndroidLibraryAarProvider.Aar;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceType;
+import com.google.devtools.build.lib.rules.android.ResourceContainer.ResourceType;
import com.google.devtools.build.lib.rules.cpp.LinkerInput;
import com.google.devtools.build.lib.rules.java.JavaCommon;
import com.google.devtools.build.lib.rules.java.JavaNeverlinkInfoProvider;
@@ -39,7 +38,6 @@ import com.google.devtools.build.lib.rules.java.JavaTargetAttributes;
import com.google.devtools.build.lib.rules.java.ProguardLibrary;
import com.google.devtools.build.lib.rules.java.ProguardSpecProvider;
import com.google.devtools.build.lib.syntax.Type;
-import com.google.devtools.build.lib.vfs.PathFragment;
/**
* An implementation for the "android_library" rule.
@@ -153,20 +151,15 @@ public abstract class AndroidLibrary implements RuleConfiguredTargetFactory {
String javaPackage = AndroidCommon.getJavaPackage(ruleContext);
- ResourceContainer resourceContainer = ResourceContainer.create(ruleContext.getLabel(),
- javaPackage,
- null /* renameManifestPackage */,
- false /* inlinedConstants */,
- null /* resourceApk -- not needed for library */,
- applicationManifest.getManifest(),
- ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_JAVA_SOURCE_JAR),
- null /* javaClassJar -- compiled separately from resource processing */,
- ImmutableList.<Artifact>of(),
- ImmutableList.<Artifact>of(),
- ImmutableList.<PathFragment>of(),
- ImmutableList.<PathFragment>of(),
- ruleContext.attributes().get("exports_manifest", Type.BOOLEAN),
- ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_R_TXT), null);
+ ResourceContainer resourceContainer = ResourceContainer.builder()
+ .setLabel(ruleContext.getLabel())
+ .setJavaPackageFromString(javaPackage)
+ .setManifest(applicationManifest.getManifest())
+ .setJavaSourceJar(
+ ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_JAVA_SOURCE_JAR))
+ .setManifestExported(ruleContext.attributes().get("exports_manifest", Type.BOOLEAN))
+ .setRTxt(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_R_TXT))
+ .build();
primaryResources = new AndroidResourcesProcessorBuilder(ruleContext)
.setLibrary(true)
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceContainerBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceContainerBuilder.java
deleted file mode 100644
index a148311b18..0000000000
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceContainerBuilder.java
+++ /dev/null
@@ -1,140 +0,0 @@
-// Copyright 2015 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-package com.google.devtools.build.lib.rules.android;
-
-import com.google.devtools.build.lib.actions.Artifact;
-import com.google.devtools.build.lib.analysis.RuleContext;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-import com.google.devtools.build.lib.rules.java.JavaUtil;
-import com.google.devtools.build.lib.syntax.Type;
-import com.google.devtools.build.lib.util.Preconditions;
-
-import javax.annotation.Nullable;
-
-/**
- * Encapsulates the process of building {@link ResourceContainer}.
- */
-public final class AndroidResourceContainerBuilder {
- private LocalResourceContainer data;
- private Artifact manifest;
- private Artifact rOutput;
- private boolean inlineConstants = false;
- private Artifact symbolsFile;
- private boolean useJavaPackageFromManifest = false;
-
- /** Provides the resources and assets for the ResourceContainer. */
- public AndroidResourceContainerBuilder withData(LocalResourceContainer data) {
- this.data = data;
- return this;
- }
-
- public AndroidResourceContainerBuilder withManifest(Artifact manifest) {
- this.manifest = manifest;
- return this;
- }
-
- public AndroidResourceContainerBuilder withROutput(Artifact rOutput) {
- this.rOutput = rOutput;
- return this;
- }
-
- public AndroidResourceContainerBuilder withInlinedConstants(boolean inlineContants) {
- this.inlineConstants = inlineContants;
- return this;
- }
-
- public AndroidResourceContainerBuilder useJavaPackageFromManifest(
- boolean useJavaPackageFromManifest) {
- this.useJavaPackageFromManifest = useJavaPackageFromManifest;
- return this;
- }
-
- /** Creates a {@link ResourceContainer} from a {@link RuleContext}.
- * @throws InterruptedException */
- public ResourceContainer buildFromRule(RuleContext ruleContext, @Nullable Artifact apk)
- throws InterruptedException {
- return buildFromRule(ruleContext, apk, false);
- }
-
- /** Creates a {@link ResourceContainer} from a {@link RuleContext}.
- * @throws InterruptedException */
- public ResourceContainer buildFromRule(RuleContext ruleContext, @Nullable Artifact apk,
- boolean alwaysExportManifest)
- throws InterruptedException {
- Preconditions.checkNotNull(this.manifest);
- Preconditions.checkNotNull(this.data);
- Artifact rJavaSrcJar = ruleContext.getImplicitOutputArtifact(
- AndroidRuleClasses.ANDROID_JAVA_SOURCE_JAR);
- return AndroidResourcesProvider.ResourceContainer.create(
- ruleContext.getLabel(),
- getJavaPackage(ruleContext, rJavaSrcJar),
- getRenameManifestPackage(ruleContext),
- inlineConstants,
- apk,
- manifest,
- rJavaSrcJar,
- null, /* javaClassJar -- compile from source jar unless generated directly by a tool */
- data.getAssets(),
- data.getResources(),
- data.getAssetRoots(),
- data.getResourceRoots(),
- alwaysExportManifest || getExportsManifest(ruleContext),
- rOutput,
- symbolsFile);
- }
-
- private String getJavaPackage(RuleContext ruleContext, Artifact rJavaSrcJar) {
- if (useJavaPackageFromManifest) {
- return null;
- }
- if (hasCustomPackage(ruleContext)) {
- return ruleContext.attributes().get("custom_package", Type.STRING);
- }
- // TODO(bazel-team): JavaUtil.getJavaPackageName does not check to see if the path is valid.
- // So we need to check for the JavaRoot.
- if (JavaUtil.getJavaRoot(rJavaSrcJar.getExecPath()) == null) {
- ruleContext.ruleError("The location of your BUILD file determines the Java package used for "
- + "Android resource processing. A directory named \"java\" or \"javatests\" will be used "
- + "as your Java source root and the path of your BUILD file relative to the Java source "
- + "root will be used as the package for Android resource processing. The Java source "
- + "root could not be determined for \"" + ruleContext.getPackageDirectory() + "\". "
- + "Move your BUILD file under a java or javatests directory, or set the 'custom_package' "
- + "attribute.");
- }
- return JavaUtil.getJavaPackageName(rJavaSrcJar.getExecPath());
- }
-
- private boolean hasCustomPackage(RuleContext ruleContext) {
- return ruleContext.attributes().isAttributeValueExplicitlySpecified("custom_package");
- }
-
- private boolean getExportsManifest(RuleContext ruleContext) {
- // AndroidLibraryBaseRule has exports_manifest but AndroidBinaryBaseRule does not.
- // ResourceContainers are built for both, so we must check if exports_manifest is present.
- return ruleContext.attributes().has("exports_manifest", Type.BOOLEAN)
- && ruleContext.attributes().get("exports_manifest", Type.BOOLEAN);
- }
-
- @Nullable
- private String getRenameManifestPackage(RuleContext ruleContext) {
- return ruleContext.attributes().isAttributeValueExplicitlySpecified("rename_manifest_package")
- ? ruleContext.attributes().get("rename_manifest_package", Type.STRING)
- : null;
- }
-
- public AndroidResourceContainerBuilder withSymbolsFile(Artifact symbolsFile) {
- this.symbolsFile = symbolsFile;
- return this;
- }
-}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceMergingActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceMergingActionBuilder.java
index c79fd21629..5c737851a4 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceMergingActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceMergingActionBuilder.java
@@ -23,8 +23,6 @@ import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceType;
import com.google.devtools.build.lib.rules.android.ResourceContainerConverter.Builder.SeparatorType;
import java.util.ArrayList;
import java.util.List;
@@ -168,21 +166,11 @@ class AndroidResourceMergingActionBuilder {
.build(context));
// Return the full set of processed transitive dependencies.
- return ResourceContainer.create(
- primary.getLabel(),
- primary.getJavaPackage(),
- primary.getRenameManifestPackage(),
- primary.getConstantsInlined(),
- primary.getApk(),
- manifestOut != null ? manifestOut : primary.getManifest(),
- primary.getJavaSourceJar(),
- classJarOut,
- primary.getArtifacts(ResourceType.ASSETS),
- primary.getArtifacts(ResourceType.RESOURCES),
- primary.getRoots(ResourceType.ASSETS),
- primary.getRoots(ResourceType.RESOURCES),
- primary.isManifestExported(),
- primary.getRTxt(),
- primary.getSymbolsTxt());
+ ResourceContainer.Builder result = primary.toBuilder()
+ .setJavaClassJar(classJarOut);
+ if (manifestOut != null) {
+ result.setManifest(manifestOut);
+ }
+ return result.build();
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceValidatorActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceValidatorActionBuilder.java
index 5a822b4168..3419076a2c 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceValidatorActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceValidatorActionBuilder.java
@@ -23,8 +23,6 @@ import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceType;
import java.util.ArrayList;
import java.util.List;
@@ -152,21 +150,9 @@ class AndroidResourceValidatorActionBuilder {
.build(context));
// Return the full set of validated transitive dependencies.
- return ResourceContainer.create(
- primary.getLabel(),
- primary.getJavaPackage(),
- primary.getRenameManifestPackage(),
- primary.getConstantsInlined(),
- primary.getApk(),
- primary.getManifest(),
- sourceJarOut,
- primary.getJavaClassJar(),
- primary.getArtifacts(ResourceType.ASSETS),
- primary.getArtifacts(ResourceType.RESOURCES),
- primary.getRoots(ResourceType.ASSETS),
- primary.getRoots(ResourceType.RESOURCES),
- primary.isManifestExported(),
- rTxtOut,
- primary.getSymbolsTxt());
+ return primary.toBuilder()
+ .setJavaSourceJar(sourceJarOut)
+ .setRTxt(rTxtOut)
+ .build();
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProcessorBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProcessorBuilder.java
index bbbc9b052a..0ef11f0877 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProcessorBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProcessorBuilder.java
@@ -22,8 +22,6 @@ import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceType;
import com.google.devtools.build.lib.rules.android.ResourceContainerConverter.Builder.SeparatorType;
import java.util.ArrayList;
import java.util.Collections;
@@ -323,25 +321,21 @@ public class AndroidResourcesProcessorBuilder {
.build(context));
// Return the full set of processed transitive dependencies.
- return ResourceContainer.create(primary.getLabel(),
- primary.getJavaPackage(),
- primary.getRenameManifestPackage(),
- primary.getConstantsInlined(),
- // If there is no apk to be generated, use the apk from the primary resources.
- // All android_binary ResourceContainers have to have an apk, but if a new one is not
- // requested to be built for this resource processing action (in case of just creating an
- // R.txt or proguard merging), reuse the primary resource from the dependencies.
- apkOut != null ? apkOut : primary.getApk(),
- manifestOut != null ? manifestOut : primary.getManifest(),
- sourceJarOut,
- primary.getJavaClassJar(),
- primary.getArtifacts(ResourceType.ASSETS),
- primary.getArtifacts(ResourceType.RESOURCES),
- primary.getRoots(ResourceType.ASSETS),
- primary.getRoots(ResourceType.RESOURCES),
- primary.isManifestExported(),
- rTxtOut,
- symbolsTxt);
+ ResourceContainer.Builder result = primary.toBuilder()
+ .setJavaSourceJar(sourceJarOut)
+ .setRTxt(rTxtOut)
+ .setSymbolsTxt(symbolsTxt);
+ // If there is an apk to be generated, use it, else reuse the apk from the primary resources.
+ // All android_binary ResourceContainers have to have an apk, but if a new one is not
+ // requested to be built for this resource processing action (in case of just creating an
+ // R.txt or proguard merging), reuse the primary resource from the dependencies.
+ if (apkOut != null) {
+ result.setApk(apkOut);
+ }
+ if (manifestOut != null) {
+ result.setManifest(manifestOut);
+ }
+ return result.build();
}
public AndroidResourcesProcessorBuilder setJavaPackage(String customJavaPackage) {
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProvider.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProvider.java
index dd117d9697..0bc2391640 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProvider.java
@@ -14,16 +14,10 @@
package com.google.devtools.build.lib.rules.android;
import com.google.auto.value.AutoValue;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
-import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
-import com.google.devtools.build.lib.vfs.PathFragment;
-import java.util.Objects;
-import javax.annotation.Nullable;
/** A provider that supplies ResourceContainers from its transitive closure. */
@AutoValue
@@ -49,116 +43,5 @@ public abstract class AndroidResourcesProvider implements TransitiveInfoProvider
/** Returns the immediate ResourceContainers for the label. */
public abstract NestedSet<ResourceContainer> getDirectAndroidResources();
-
- /**
- * The type of resource in question: either asset or a resource.
- */
- public enum ResourceType {
- ASSETS("assets"),
- RESOURCES("resources");
-
- private final String attribute;
-
- private ResourceType(String attribute) {
- this.attribute = attribute;
- }
-
- public String getAttribute() {
- return attribute;
- }
- }
-
- /**
- * The resources contributed by a single target.
- */
- @AutoValue
- @Immutable
- public abstract static class ResourceContainer {
-
- public static ResourceContainer create(
- Label label,
- @Nullable String javaPackage,
- @Nullable String renameManifestPackage,
- boolean constantsInlined,
- @Nullable Artifact apk,
- Artifact manifest,
- @Nullable Artifact javaSourceJar,
- @Nullable Artifact javaClassJar,
- ImmutableList<Artifact> assets,
- ImmutableList<Artifact> resources,
- ImmutableList<PathFragment> assetsRoots,
- ImmutableList<PathFragment> resourcesRoots,
- boolean manifestExported,
- @Nullable Artifact rTxt,
- @Nullable Artifact symbolsTxt) {
- return new AutoValue_AndroidResourcesProvider_ResourceContainer(
- label,
- javaPackage,
- renameManifestPackage,
- constantsInlined,
- apk,
- manifest,
- javaSourceJar,
- javaClassJar,
- assets,
- resources,
- assetsRoots,
- resourcesRoots,
- manifestExported,
- rTxt,
- symbolsTxt);
- }
-
- public abstract Label getLabel();
- @Nullable public abstract String getJavaPackage();
- @Nullable public abstract String getRenameManifestPackage();
- public abstract boolean getConstantsInlined();
- @Nullable public abstract Artifact getApk();
- public abstract Artifact getManifest();
- @Nullable public abstract Artifact getJavaSourceJar();
- @Nullable public abstract Artifact getJavaClassJar();
-
- abstract ImmutableList<Artifact> getAssets();
- abstract ImmutableList<Artifact> getResources();
-
- public ImmutableList<Artifact> getArtifacts(ResourceType resourceType) {
- return resourceType == ResourceType.ASSETS ? getAssets() : getResources();
- }
-
- public Iterable<Artifact> getArtifacts() {
- return Iterables.concat(getAssets(), getResources());
- }
-
- abstract ImmutableList<PathFragment> getAssetsRoots();
- abstract ImmutableList<PathFragment> getResourcesRoots();
- public ImmutableList<PathFragment> getRoots(ResourceType resourceType) {
- return resourceType == ResourceType.ASSETS ? getAssetsRoots() : getResourcesRoots();
- }
-
- public abstract boolean isManifestExported();
- @Nullable public abstract Artifact getRTxt();
- @Nullable public abstract Artifact getSymbolsTxt();
-
- // TODO(somebody) evaluate if we can just use hashCode and equals from AutoValue
- @Override
- public int hashCode() {
- return Objects.hash(getLabel(), getRTxt(), getSymbolsTxt());
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof ResourceContainer)) {
- return false;
- }
- ResourceContainer other = (ResourceContainer) obj;
- return Objects.equals(getLabel(), other.getLabel())
- && Objects.equals(getRTxt(), other.getRTxt())
- && Objects.equals(getSymbolsTxt(), other.getSymbolsTxt());
- }
- }
-
AndroidResourcesProvider() {}
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSkylarkApiProvider.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSkylarkApiProvider.java
index ed7ee96853..29fc66407c 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSkylarkApiProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSkylarkApiProvider.java
@@ -21,8 +21,7 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.rules.SkylarkApiProvider;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceType;
+import com.google.devtools.build.lib.rules.android.ResourceContainer.ResourceType;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/ApplicationManifest.java b/src/main/java/com/google/devtools/build/lib/rules/android/ApplicationManifest.java
index 3922c544e7..808611041e 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/ApplicationManifest.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/ApplicationManifest.java
@@ -32,8 +32,8 @@ import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.config.CompilationMode;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidManifestMerger;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceType;
+import com.google.devtools.build.lib.rules.android.ResourceContainer.Builder.JavaPackageSource;
+import com.google.devtools.build.lib.rules.android.ResourceContainer.ResourceType;
import com.google.devtools.build.lib.syntax.Type;
import com.google.devtools.build.lib.util.Preconditions;
import com.google.devtools.build.lib.vfs.PathFragment;
@@ -305,11 +305,12 @@ public final class ApplicationManifest {
true, /* crunchPng */
ImmutableList.<String>of(), /* densities */
incremental,
- new AndroidResourceContainerBuilder()
- .withData(data)
- .withManifest(getManifest())
- .withROutput(rTxt)
- .buildFromRule(ruleContext, resourceApk),
+ ResourceContainer.builderFromRule(ruleContext)
+ .setAssetsAndResourcesFrom(data)
+ .setManifest(getManifest())
+ .setRTxt(rTxt)
+ .setApk(resourceApk)
+ .build(),
data,
proguardCfg,
null, /* Artifact mainDexProguardCfg */
@@ -331,6 +332,16 @@ public final class ApplicationManifest {
if (ruleContext.hasErrors()) {
return null;
}
+ ResourceContainer.Builder resourceContainer =
+ ResourceContainer.builderFromRule(ruleContext)
+ .setAssetsAndResourcesFrom(data)
+ .setManifest(getManifest())
+ .setRTxt(rTxt)
+ .setSymbolsTxt(symbolsTxt)
+ .setJavaPackageFrom(JavaPackageSource.MANIFEST);
+ if (alwaysExportManifest) {
+ resourceContainer.setManifestExported(true);
+ }
return createApk(
ruleContext,
true, /* isLibrary */
@@ -340,13 +351,7 @@ public final class ApplicationManifest {
false, /* crunchPng */
ImmutableList.<String>of(), /* List<String> densities */
false, /* incremental */
- new AndroidResourceContainerBuilder()
- .withData(data)
- .withManifest(getManifest())
- .withROutput(rTxt)
- .withSymbolsFile(symbolsTxt)
- .useJavaPackageFromManifest(true)
- .buildFromRule(ruleContext, null, alwaysExportManifest),
+ resourceContainer.build(),
data,
null, /* Artifact proguardCfg */
null, /* Artifact mainDexProguardCfg */
@@ -398,12 +403,13 @@ public final class ApplicationManifest {
crunchPng,
densities,
incremental,
- new AndroidResourceContainerBuilder()
- .withData(data)
- .withManifest(getManifest())
- .withROutput(rTxt)
- .withSymbolsFile(symbolsTxt)
- .buildFromRule(ruleContext, resourceApk),
+ ResourceContainer.builderFromRule(ruleContext)
+ .setAssetsAndResourcesFrom(data)
+ .setManifest(getManifest())
+ .setRTxt(rTxt)
+ .setSymbolsTxt(symbolsTxt)
+ .setApk(resourceApk)
+ .build(),
data,
proguardCfg,
mainDexProguardCfg,
@@ -618,21 +624,14 @@ public final class ApplicationManifest {
aaptActionHelper.createGenerateApkAction(resourceApk,
resourceContainer.getRenameManifestPackage(), additionalAaptOpts.build(), densities);
- ResourceContainer updatedResources = ResourceContainer.create(
- ruleContext.getLabel(),
- resourceContainer.getJavaPackage(),
- resourceContainer.getRenameManifestPackage(),
- resourceContainer.getConstantsInlined(),
- resourceApk,
- getManifest(),
- javaSourcesJar,
- null, /* javaClassJar */
- resourceContainer.getArtifacts(ResourceType.ASSETS),
- resourceContainer.getArtifacts(ResourceType.RESOURCES),
- resourceContainer.getRoots(ResourceType.ASSETS),
- resourceContainer.getRoots(ResourceType.RESOURCES),
- resourceContainer.isManifestExported(),
- resourceContainer.getRTxt(), null);
+ ResourceContainer updatedResources = resourceContainer.toBuilder()
+ .setLabel(ruleContext.getLabel())
+ .setApk(resourceApk)
+ .setManifest(getManifest())
+ .setJavaSourceJar(javaSourcesJar)
+ .setJavaClassJar(null)
+ .setSymbolsTxt(null)
+ .build();
aaptActionHelper.createGenerateProguardAction(proguardCfg, mainDexProguardCfg);
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/LocalResourceContainer.java b/src/main/java/com/google/devtools/build/lib/rules/android/LocalResourceContainer.java
index 05d60a8eb4..d4a15834e8 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/LocalResourceContainer.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/LocalResourceContainer.java
@@ -25,7 +25,7 @@ import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.AttributeMap;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceType;
+import com.google.devtools.build.lib.rules.android.ResourceContainer.ResourceType;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.Collection;
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java
index 933790a9c0..4d56a06f11 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java
@@ -27,7 +27,6 @@ import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/ResourceApk.java b/src/main/java/com/google/devtools/build/lib/rules/android/ResourceApk.java
index 8ba6ca6838..136c404bf4 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/ResourceApk.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/ResourceApk.java
@@ -16,8 +16,6 @@ package com.google.devtools.build.lib.rules.android;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-
import javax.annotation.Nullable;
/**
@@ -103,14 +101,14 @@ public final class ResourceApk {
/**
* Creates an provider from the resources in the ResourceApk.
- *
+ *
* <p>If the ResourceApk was created from transitive resources, the provider will effectively
* contain the "forwarded" resources: The merged transitive and merged direct dependencies of this
* library.
- *
+ *
* <p>If the ResourceApk was generated from a "resources" attribute, it will contain the
* "resources" container in the direct dependencies and the rest as transitive.
- *
+ *
* <p>If the ResourceApk was generated from local resources, that will be the direct dependencies and
* the rest will be transitive.
*/
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/ResourceContainer.java b/src/main/java/com/google/devtools/build/lib/rules/android/ResourceContainer.java
new file mode 100644
index 0000000000..0dca5ac607
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/ResourceContainer.java
@@ -0,0 +1,321 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.android;
+
+import com.google.auto.value.AutoValue;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.analysis.RuleContext;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
+import com.google.devtools.build.lib.rules.java.JavaUtil;
+import com.google.devtools.build.lib.syntax.Type;
+import com.google.devtools.build.lib.util.Preconditions;
+import com.google.devtools.build.lib.vfs.PathFragment;
+import java.util.Objects;
+import javax.annotation.Nullable;
+
+/** The resources contributed by a single target. */
+@AutoValue
+@Immutable
+public abstract class ResourceContainer {
+ /** The type of resource in question: either asset or a resource. */
+ public enum ResourceType {
+ ASSETS("assets"),
+ RESOURCES("resources");
+
+ private final String attribute;
+
+ private ResourceType(String attribute) {
+ this.attribute = attribute;
+ }
+
+ public String getAttribute() {
+ return attribute;
+ }
+ }
+
+ public abstract Label getLabel();
+
+ @Nullable
+ public abstract String getJavaPackage();
+
+ @Nullable
+ public abstract String getRenameManifestPackage();
+
+ public abstract boolean getConstantsInlined();
+
+ @Nullable
+ public abstract Artifact getApk();
+
+ public abstract Artifact getManifest();
+
+ @Nullable
+ public abstract Artifact getJavaSourceJar();
+
+ @Nullable
+ public abstract Artifact getJavaClassJar();
+
+ abstract ImmutableList<Artifact> getAssets();
+
+ abstract ImmutableList<Artifact> getResources();
+
+ public ImmutableList<Artifact> getArtifacts(ResourceType resourceType) {
+ return resourceType == ResourceType.ASSETS ? getAssets() : getResources();
+ }
+
+ public Iterable<Artifact> getArtifacts() {
+ return Iterables.concat(getAssets(), getResources());
+ }
+
+ abstract ImmutableList<PathFragment> getAssetsRoots();
+
+ abstract ImmutableList<PathFragment> getResourcesRoots();
+
+ public ImmutableList<PathFragment> getRoots(ResourceType resourceType) {
+ return resourceType == ResourceType.ASSETS ? getAssetsRoots() : getResourcesRoots();
+ }
+
+ public abstract boolean isManifestExported();
+
+ @Nullable
+ public abstract Artifact getRTxt();
+
+ @Nullable
+ public abstract Artifact getSymbolsTxt();
+
+ // The limited hashCode and equals behavior is necessary to avoid duplication when building with
+ // fat_apk_cpu set. Artifacts generated in different configurations will naturally be different
+ // and non-equal objects, causing the ResourceContainer not to be automatically deduplicated at
+ // the android_binary level.
+ // TODO(bazel-team): deduplicate explicitly and remove hashCode and equals overrides to avoid
+ // breaking "equals means interchangeable"
+ @Override
+ public int hashCode() {
+ return Objects.hash(getLabel(), getRTxt(), getSymbolsTxt());
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof ResourceContainer)) {
+ return false;
+ }
+ ResourceContainer other = (ResourceContainer) obj;
+ return Objects.equals(getLabel(), other.getLabel())
+ && Objects.equals(getRTxt(), other.getRTxt())
+ && Objects.equals(getSymbolsTxt(), other.getSymbolsTxt());
+ }
+
+ /** Converts this container back into a builder to create a modified copy. */
+ public abstract Builder toBuilder();
+
+ /** Creates a new builder with default values. */
+ public static Builder builder() {
+ return new AutoValue_ResourceContainer.Builder()
+ .setJavaPackageFrom(Builder.JavaPackageSource.MANIFEST)
+ .setConstantsInlined(false)
+ .setAssets(ImmutableList.<Artifact>of())
+ .setResources(ImmutableList.<Artifact>of())
+ .setAssetsRoots(ImmutableList.<PathFragment>of())
+ .setResourcesRoots(ImmutableList.<PathFragment>of());
+ }
+
+ /**
+ * Creates a new builder with the label, Java package, manifest package override, Java source jar,
+ * and manifest-export switch set according to the given rule.
+ */
+ public static Builder builderFromRule(RuleContext ruleContext) throws InterruptedException {
+ return builder().forRuleContext(ruleContext);
+ }
+
+ /** Builder to construct resource containers. */
+ @AutoValue.Builder
+ public abstract static class Builder {
+ /** Enum to determine what to do if a package hasn't been manually set. */
+ public enum JavaPackageSource {
+ /**
+ * Uses the package from the manifest, i.e., the generated ResourceContainer will return null
+ * from {@link ResourceContainer#getJavaPackage()}.
+ */
+ MANIFEST,
+ /**
+ * Uses the package from the path to the source jar (or, if the rule context has it set,
+ * the {@code custom_package} attribute). If the source jar is not under a valid Java root,
+ * this will result in an error being added to the rule context. This can only be used if the
+ * builder was created by {@link ResourceContainer#builderFromRule(RuleContext)}.
+ */
+ SOURCE_JAR_PATH
+ }
+
+ @Nullable private RuleContext ruleContext;
+ @Nullable private JavaPackageSource javaPackageSource;
+
+ private Builder forRuleContext(RuleContext ruleContext) throws InterruptedException {
+ Preconditions.checkNotNull(ruleContext);
+ this.ruleContext = ruleContext;
+ return this.setLabel(ruleContext.getLabel())
+ .setRenameManifestPackage(getRenameManifestPackage(ruleContext))
+ .setJavaSourceJar(
+ ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_JAVA_SOURCE_JAR))
+ .setJavaPackageFrom(JavaPackageSource.SOURCE_JAR_PATH)
+ .setManifestExported(getExportsManifest(ruleContext));
+ }
+
+ /**
+ * Sets the Java package from the given source. Overrides earlier calls to
+ * {@link #setJavaPackageFrom(JavaPackageSource)} or {@link #setJavaPackageFromString(String)}.
+ *
+ * <p>To set the package from {@link JavaPackageSource#SOURCE_JAR_PATH}, this instance must have
+ * been created using {@link ResourceContainer#builderFromRule(RuleContext)}. Also in this case,
+ * the source jar must be set non-{@code null} when the {@link #build()} method is called.
+ * It defaults to the source jar implicit output when creating a builder out of a rule context.
+ */
+ public Builder setJavaPackageFrom(JavaPackageSource javaPackageSource) {
+ Preconditions.checkNotNull(javaPackageSource);
+ Preconditions.checkArgument(
+ !(javaPackageSource == JavaPackageSource.SOURCE_JAR_PATH && ruleContext == null),
+ "setJavaPackageFrom(SOURCE_JAR_PATH) is only permitted when using builderFromRule.");
+ this.javaPackageSource = javaPackageSource;
+ return this.setJavaPackage(null);
+ }
+
+ /**
+ * Sets the Java package from the given string. Overrides earlier calls to
+ * {@link #setJavaPackageFrom(JavaPackageSource)} or {@link #setJavaPackageFromString(String)}.
+ *
+ * <p>To make {@link ResourceContainer#getJavaPackage()} return {@code null}, call
+ * {@code setJavaPackageFrom(MANIFEST)} instead.
+ */
+ public Builder setJavaPackageFromString(String javaPackageOverride) {
+ Preconditions.checkNotNull(javaPackageOverride);
+ this.javaPackageSource = null;
+ return this.setJavaPackage(javaPackageOverride);
+ }
+
+ /**
+ * Sets the assets, resources, asset roots, and resource roots from the given local resource
+ * container.
+ *
+ * <p>This will override any of these values which were previously set directly.
+ */
+ public Builder setAssetsAndResourcesFrom(LocalResourceContainer data) {
+ return this.setAssets(data.getAssets())
+ .setResources(data.getResources())
+ .setAssetsRoots(data.getAssetRoots())
+ .setResourcesRoots(data.getResourceRoots());
+ }
+
+ public abstract Builder setLabel(Label label);
+
+ abstract Builder setJavaPackage(@Nullable String javaPackage);
+
+ public abstract Builder setRenameManifestPackage(@Nullable String renameManifestPackage);
+
+ public abstract Builder setConstantsInlined(boolean constantsInlined);
+
+ public abstract Builder setApk(@Nullable Artifact apk);
+
+ public abstract Builder setManifest(Artifact manifest);
+
+ @Nullable
+ abstract Artifact getJavaSourceJar();
+
+ public abstract Builder setJavaSourceJar(@Nullable Artifact javaSourceJar);
+
+ public abstract Builder setJavaClassJar(@Nullable Artifact javaClassJar);
+
+ public abstract Builder setAssets(ImmutableList<Artifact> assets);
+
+ public abstract Builder setResources(ImmutableList<Artifact> resources);
+
+ public abstract Builder setAssetsRoots(ImmutableList<PathFragment> assetsRoots);
+
+ public abstract Builder setResourcesRoots(ImmutableList<PathFragment> resourcesRoots);
+
+ public abstract Builder setManifestExported(boolean manifestExported);
+
+ public abstract Builder setRTxt(@Nullable Artifact rTxt);
+
+ public abstract Builder setSymbolsTxt(@Nullable Artifact symbolsTxt);
+
+ abstract ResourceContainer autoBuild();
+
+ /**
+ * Builds and returns the ResourceContainer.
+ *
+ * <p>May result in the rule context adding a rule error if the Java package was to be set from
+ * the source jar path, but the source jar does not have an acceptable Java package.
+ */
+ public ResourceContainer build() {
+ if (javaPackageSource == JavaPackageSource.SOURCE_JAR_PATH) {
+ Preconditions.checkState(
+ !(javaPackageSource == JavaPackageSource.SOURCE_JAR_PATH && ruleContext == null),
+ "setJavaPackageFrom(SOURCE_JAR_PATH) is only permitted when using builderFromRule.");
+ Preconditions.checkState(
+ getJavaSourceJar() != null,
+ "setJavaPackageFrom(SOURCE_JAR_PATH) was called, but no source jar was set.");
+ setJavaPackage(getJavaPackageFromSourceJarPath());
+ }
+ return autoBuild();
+ }
+
+ @Nullable
+ private String getJavaPackageFromSourceJarPath() {
+ if (javaPackageSource != JavaPackageSource.SOURCE_JAR_PATH) {
+ return null;
+ }
+ if (hasCustomPackage(ruleContext)) {
+ return ruleContext.attributes().get("custom_package", Type.STRING);
+ }
+ Artifact rJavaSrcJar = getJavaSourceJar();
+ // TODO(bazel-team): JavaUtil.getJavaPackageName does not check to see if the path is valid.
+ // So we need to check for the JavaRoot.
+ if (JavaUtil.getJavaRoot(rJavaSrcJar.getExecPath()) == null) {
+ ruleContext.ruleError(
+ "The location of your BUILD file determines the Java package used for "
+ + "Android resource processing. A directory named \"java\" or \"javatests\" will "
+ + "be used as your Java source root and the path of your BUILD file relative to "
+ + "the Java source root will be used as the package for Android resource "
+ + "processing. The Java source root could not be determined for \""
+ + ruleContext.getPackageDirectory()
+ + "\". Move your BUILD file under a java or javatests directory, or set the "
+ + "'custom_package' attribute.");
+ }
+ return JavaUtil.getJavaPackageName(rJavaSrcJar.getExecPath());
+ }
+
+ private static boolean hasCustomPackage(RuleContext ruleContext) {
+ return ruleContext.attributes().isAttributeValueExplicitlySpecified("custom_package");
+ }
+
+ private static boolean getExportsManifest(RuleContext ruleContext) {
+ // AndroidLibraryBaseRule has exports_manifest but AndroidBinaryBaseRule does not.
+ // ResourceContainers are built for both, so we must check if exports_manifest is present.
+ return ruleContext.attributes().has("exports_manifest", Type.BOOLEAN)
+ && ruleContext.attributes().get("exports_manifest", Type.BOOLEAN);
+ }
+
+ @Nullable
+ private static String getRenameManifestPackage(RuleContext ruleContext) {
+ return ruleContext.attributes().isAttributeValueExplicitlySpecified("rename_manifest_package")
+ ? ruleContext.attributes().get("rename_manifest_package", Type.STRING)
+ : null;
+ }
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/ResourceContainerConverter.java b/src/main/java/com/google/devtools/build/lib/rules/android/ResourceContainerConverter.java
index d86ada91c5..4e1f9a8624 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/ResourceContainerConverter.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/ResourceContainerConverter.java
@@ -27,8 +27,7 @@ import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceType;
+import com.google.devtools.build.lib.rules.android.ResourceContainer.ResourceType;
import javax.annotation.Nullable;
/**
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/ResourceDependencies.java b/src/main/java/com/google/devtools/build/lib/rules/android/ResourceDependencies.java
index 269769a6b1..b64570a97c 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/ResourceDependencies.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/ResourceDependencies.java
@@ -23,7 +23,6 @@ import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.AttributeMap;
import com.google.devtools.build.lib.packages.BuildType;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
/**
* Represents a container for the {@link ResourceContainer}s for a given library. This is
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/ResourceShrinkerActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/android/ResourceShrinkerActionBuilder.java
index 63525e97e3..beb7370d5a 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/ResourceShrinkerActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/ResourceShrinkerActionBuilder.java
@@ -22,8 +22,6 @@ import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.config.CompilationMode;
-import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-
import java.util.Collections;
import java.util.List;