From 98bd4ae81715742ae5aa6c91164cd37396cb7c91 Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 28 Mar 2023 11:32:34 -0700 Subject: [PATCH] Native changes required to support linking against resource apks. Note that this only wires up for validation in the native pipeline. The main implementation here is in the Starlark pipeline, but there is still one existing validation step in native (which we will need to clean up separately). PiperOrigin-RevId: 520086631 Change-Id: I0d99350e5b7c8f16bbcdae8f1db218281be5e80c --- .../android/ValidatedAndroidResources.java | 2 +- .../android/AndroidDataProcessingApi.java | 6 ++--- .../ValidateAndLinkResourcesAction.java | 23 ++++++++----------- .../build/android/aapt2/ResourceLinker.java | 12 ++++++++++ 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/ValidatedAndroidResources.java b/src/main/java/com/google/devtools/build/lib/rules/android/ValidatedAndroidResources.java index 7d11049b44a9da..dfc8c5964a6a24 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/ValidatedAndroidResources.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/ValidatedAndroidResources.java @@ -79,7 +79,7 @@ public static ValidatedAndroidResources validateFrom( // this allows android rules to generate resources outside of the java{,tests} tree. .maybeAddFlag("--packageForR", merged.getJavaPackage()) .addVectoredFlag( - "--additionalApksToLinkAgainst", + "--resourceApks", resApkDeps.stream().map(Artifact::getRootRelativePathString).collect(toImmutableList())) .addTransitiveVectoredInput( "--compiledDep", merged.getResourceDependencies().getTransitiveCompiledSymbols()) diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/android/AndroidDataProcessingApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/android/AndroidDataProcessingApi.java index c38831c5a95184..0955c93c7a5fa5 100644 --- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/android/AndroidDataProcessingApi.java +++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/android/AndroidDataProcessingApi.java @@ -289,7 +289,7 @@ AndroidAssetsInfoT mergeAssets( "Targets containing raw resources from dependencies. These resources will be merged" + " together with each other and this target's resources."), @Param( - name = "validation_res_apks", + name = "validation_resource_apks", positional = false, defaultValue = "[]", allowedTypes = { @@ -297,8 +297,8 @@ AndroidAssetsInfoT mergeAssets( }, named = true, doc = - "Resource APK deps to be used for validation only. Not fully supported in the" - + " native resource pipeline."), + "List of resource only APK files to be used for validation only. Not fully" + + " supported in the native resource pipeline."), @Param( name = "neverlink", positional = false, diff --git a/src/tools/android/java/com/google/devtools/build/android/ValidateAndLinkResourcesAction.java b/src/tools/android/java/com/google/devtools/build/android/ValidateAndLinkResourcesAction.java index 3fe4355632e743..2a8606e3d98edc 100644 --- a/src/tools/android/java/com/google/devtools/build/android/ValidateAndLinkResourcesAction.java +++ b/src/tools/android/java/com/google/devtools/build/android/ValidateAndLinkResourcesAction.java @@ -163,14 +163,14 @@ public static class Options extends OptionsBase { public Path sourceJarOut; @Option( - name = "additionalApksToLinkAgainst", + name = "resourceApks", defaultValue = "null", category = "input", converter = PathListConverter.class, documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, effectTags = {OptionEffectTag.UNKNOWN}, - help = "List of APKs used during linking.") - public List additionalApksToLinkAgainst; + help = "List of reource only APK files to link against.") + public List resourceApks; } public static void main(String[] args) throws Exception { @@ -214,15 +214,10 @@ public static void main(String[] args) throws Exception { checkVisibilityOfResourceReferences( /* androidManifest= */ XmlNode.getDefaultInstance(), resources, includes); - ImmutableList.Builder dependencies = ImmutableList.builder(); - dependencies.addAll( - Optional.ofNullable(options.deprecatedLibraries).orElse(options.libraries)); - - if (options.additionalApksToLinkAgainst != null) { - dependencies.addAll( - options.additionalApksToLinkAgainst.stream() - .map(StaticLibrary::from) - .collect(toImmutableList())); + ImmutableList resourceApks = ImmutableList.of(); + if (options.resourceApks != null) { + resourceApks = + options.resourceApks.stream().map(StaticLibrary::from).collect(toImmutableList()); } profiler.recordEndOf("validate").startTask("link"); @@ -231,8 +226,10 @@ public static void main(String[] args) throws Exception { // NB: these names are really confusing. // .dependencies is meant for linking in android.jar // .include is meant for regular dependencies - .dependencies(dependencies.build()) + // .resourceApks is meant for linking runtime resource only apks + .dependencies(Optional.ofNullable(options.deprecatedLibraries).orElse(options.libraries)) .include(includes) + .resourceApks(resourceApks) .buildVersion(aapt2Options.buildToolsVersion) .outputAsProto(aapt2Options.resourceTableAsProto) .linkStatically(resources) diff --git a/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java b/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java index 63363dac7a4b7c..3507d8340cb22f 100644 --- a/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java +++ b/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java @@ -156,6 +156,7 @@ public static LinkError of(Throwable e) { private List assetDirs = ImmutableList.of(); private boolean conditionalKeepRules = false; private boolean includeProguardLocationReferences = false; + private List resourceApks = ImmutableList.of(); private ResourceLinker( Path aapt2, ListeningExecutorService executorService, Path workingDirectory) { @@ -244,6 +245,12 @@ public ResourceLinker outputAsProto(boolean outputAsProto) { return this; } + @CanIgnoreReturnValue + public ResourceLinker resourceApks(List resourceApks) { + this.resourceApks = resourceApks; + return this; + } + /** * Statically links the {@link CompiledResources} with the dependencies to produce a {@link * StaticLibrary}. @@ -256,6 +263,8 @@ public StaticLibrary linkStatically(CompiledResources compiled) { Path javaSourceDirectory = workingDirectory.resolve("java"); profiler.startTask("linkstatic"); final Collection pathsToLinkAgainst = StaticLibrary.toPathStrings(linkAgainst); + final Collection resourceApkPathsToLinkAgainst = + StaticLibrary.toPathStrings(resourceApks); logger.finer( new AaptCommandBuilder(aapt2) .forBuildToolsVersion(buildToolsVersion) @@ -274,6 +283,7 @@ public StaticLibrary linkStatically(CompiledResources compiled) { .addParameterableRepeated( "-R", compiledResourcesToPaths(compiled, IS_FLAT_FILE), workingDirectory) .addRepeated("-I", pathsToLinkAgainst) + .addRepeated("-I", resourceApkPathsToLinkAgainst) .add("--auto-add-overlay") .when(OVERRIDE_STYLES_INSTEAD_OF_OVERLAYING) .thenAdd("--override-styles-instead-of-overlaying") @@ -300,6 +310,7 @@ public StaticLibrary linkStatically(CompiledResources compiled) { .thenAdd("--proto-format") // only link against jars .addRepeated("-I", pathsToLinkAgainst.stream().filter(IS_JAR).collect(toList())) + .addRepeated("-I", resourceApkPathsToLinkAgainst) .add("-R", outPath) // only include non-jars .addRepeated( @@ -443,6 +454,7 @@ private ProtoApk linkProtoApk( compiled.getAssetsStrings().stream()) .collect(toList())) .addRepeated("-I", StaticLibrary.toPathStrings(linkAgainst)) + .addRepeated("-I", StaticLibrary.toPathStrings(resourceApks)) .addParameterableRepeated( "-R", compiledResourcesToPaths(