diff options
author | 2018-05-07 10:29:47 -0700 | |
---|---|---|
committer | 2018-05-07 10:31:24 -0700 | |
commit | e3faeb73c352ff55f08164801c61784b91cf4e9b (patch) | |
tree | 6ae0fdc5987d39d4dd3d2833c2a2ddc2e5588358 /src/main/java/com/google/devtools/build/lib/rules/android | |
parent | 0b7f9f8a7be0fbfe84baf8e4d1dc652f81e20073 (diff) |
Properly expose R.class file to Skylark
Add the R.class to the provider, but keep it nullable in case no
resource/manifest attributes were specified, in which case the R.class would
not be generated.
A previous change tried to actually generate it in that case, but even though I
think that's probably the right strategy for the long term, it's substantially
different behavior and out of scope of this migration.
RELNOTES: none
PiperOrigin-RevId: 195682271
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/android')
3 files changed, 40 insertions, 26 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesInfo.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesInfo.java index 6f1d665be4..f08b2ceb91 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesInfo.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesInfo.java @@ -22,6 +22,7 @@ import com.google.devtools.build.lib.packages.NativeProvider; import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; +import javax.annotation.Nullable; /** A provider that supplies ResourceContainers from its transitive closure. */ @SkylarkModule( @@ -53,6 +54,23 @@ public class AndroidResourcesInfo extends NativeInfo { private final Artifact rTxt; /* + * An R.class file for this target that can be used to build Java source. + * + * This class file should not be used for any targets besides the one that exposes this provider + * instance - even similar targets can have very different R.class files. In particular, final + * R.class files (for android_binary and similar rules) are generated very differently than + * R.class files for android_library - the latter are meant to be thrown away after building local + * Java classes. + * + * An R.class file is exposed rather than an R.java file because generating the R.class from the + * R.txt file is quicker than compiling the R.java file. + * + * An R.class file will not be produced for android_library targets that do not specify a manifest + * or resource files. + */ + @Nullable private final Artifact rClassJar; + + /* * Transitive information used for resource processing */ @@ -71,6 +89,7 @@ public class AndroidResourcesInfo extends NativeInfo { Label label, ProcessedAndroidManifest manifest, Artifact rTxt, + @Nullable Artifact rClassJar, NestedSet<ValidatedAndroidData> transitiveAndroidResources, NestedSet<ValidatedAndroidData> directAndroidResources, NestedSet<Artifact> transitiveResources, @@ -85,6 +104,7 @@ public class AndroidResourcesInfo extends NativeInfo { this.label = label; this.manifest = manifest; this.rTxt = rTxt; + this.rClassJar = rClassJar; this.transitiveAndroidResources = transitiveAndroidResources; this.directAndroidResources = directAndroidResources; this.transitiveResources = transitiveResources; @@ -111,6 +131,19 @@ public class AndroidResourcesInfo extends NativeInfo { return rTxt; } + @SkylarkCallable( + name = "r_class_jar", + structField = true, + allowReturnNones = true, + doc = + "Returns a JAR of R.class files for this target, for use in compiling this target's Java" + + " code. This JAR is only accurate for this target, and should not be used in any" + + " way for other targets. The JAR will not be available for android_library targets" + + " that do not specify resources or manifest.") + public Artifact getRClassJar() { + return rClassJar; + } + /** Returns the transitive ResourceContainers for the label. */ @SkylarkCallable( name = "transitive_android_resources", 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 cc826a067c..d69254268b 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 @@ -219,7 +219,7 @@ public final class ResourceApk { */ AndroidResourcesInfo toResourceInfo(Label label) { if (validatedResources == null) { - return resourceDeps.toInfo(label, manifest, rTxt); + return resourceDeps.toInfo(label, manifest, rTxt, resourceJavaClassJar); } return resourceDeps.toInfo(validatedResources); } 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 09ac5afbb3..1631db5992 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 @@ -137,28 +137,6 @@ public final class ResourceDependencies { transitiveRTxt.build()); } - static ResourceDependencies wrapManifestInfo(Label label, AndroidManifestInfo manifestInfo) { - return new ResourceDependencies( - false, - NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER), - NestedSetBuilder.create( - Order.NAIVE_LINK_ORDER, - ResourceContainer.builder() - .setManifest(manifestInfo.getManifest()) - .setJavaPackage(manifestInfo.getPackage()) - .setManifestExported(manifestInfo.exportsManifest()) - .setLabel(label) - .build()), - NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER), - NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER), - NestedSetBuilder.create(Order.NAIVE_LINK_ORDER, manifestInfo.getManifest()), - NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER), - NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER), - NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER), - NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER), - NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER)); - } - @Override public String toString() { return MoreObjects.toStringHelper(this) @@ -276,12 +254,14 @@ public final class ResourceDependencies { .toInfo( newDirectResource.getLabel(), newDirectResource.getProcessedManifest(), - newDirectResource.getRTxt()); + newDirectResource.getRTxt(), + newDirectResource.getJavaClassJar()); } return new AndroidResourcesInfo( newDirectResource.getLabel(), newDirectResource.getProcessedManifest(), newDirectResource.getRTxt(), + newDirectResource.getJavaClassJar(), NestedSetBuilder.<ValidatedAndroidData>naiveLinkOrder() .addTransitive(transitiveResourceContainers) .addTransitive(directResourceContainers) @@ -314,14 +294,15 @@ public final class ResourceDependencies { * @return A provider with the current resources and label. */ public AndroidResourcesInfo toInfo( - Label label, ProcessedAndroidManifest manifest, Artifact rTxt) { + Label label, ProcessedAndroidManifest manifest, Artifact rTxt, @Nullable Artifact rClassJar) { if (neverlink) { - return ResourceDependencies.empty().toInfo(label, manifest, rTxt); + return ResourceDependencies.empty().toInfo(label, manifest, rTxt, rClassJar); } return new AndroidResourcesInfo( label, manifest, rTxt, + rClassJar, transitiveResourceContainers, directResourceContainers, transitiveResources, |