Skip to content

Commit

Permalink
Native changes required to support linking against resource apks.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Googler authored and copybara-github committed Mar 28, 2023
1 parent 0a8380b commit 98bd4ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,16 @@ 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 = {
@ParamType(type = Sequence.class, generic1 = FileProviderApi.class),
},
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path> additionalApksToLinkAgainst;
help = "List of reource only APK files to link against.")
public List<Path> resourceApks;
}

public static void main(String[] args) throws Exception {
Expand Down Expand Up @@ -214,15 +214,10 @@ public static void main(String[] args) throws Exception {
checkVisibilityOfResourceReferences(
/* androidManifest= */ XmlNode.getDefaultInstance(), resources, includes);

ImmutableList.Builder<StaticLibrary> 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<StaticLibrary> resourceApks = ImmutableList.of();
if (options.resourceApks != null) {
resourceApks =
options.resourceApks.stream().map(StaticLibrary::from).collect(toImmutableList());
}

profiler.recordEndOf("validate").startTask("link");
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public static LinkError of(Throwable e) {
private List<Path> assetDirs = ImmutableList.of();
private boolean conditionalKeepRules = false;
private boolean includeProguardLocationReferences = false;
private List<StaticLibrary> resourceApks = ImmutableList.of();

private ResourceLinker(
Path aapt2, ListeningExecutorService executorService, Path workingDirectory) {
Expand Down Expand Up @@ -244,6 +245,12 @@ public ResourceLinker outputAsProto(boolean outputAsProto) {
return this;
}

@CanIgnoreReturnValue
public ResourceLinker resourceApks(List<StaticLibrary> resourceApks) {
this.resourceApks = resourceApks;
return this;
}

/**
* Statically links the {@link CompiledResources} with the dependencies to produce a {@link
* StaticLibrary}.
Expand All @@ -256,6 +263,8 @@ public StaticLibrary linkStatically(CompiledResources compiled) {
Path javaSourceDirectory = workingDirectory.resolve("java");
profiler.startTask("linkstatic");
final Collection<String> pathsToLinkAgainst = StaticLibrary.toPathStrings(linkAgainst);
final Collection<String> resourceApkPathsToLinkAgainst =
StaticLibrary.toPathStrings(resourceApks);
logger.finer(
new AaptCommandBuilder(aapt2)
.forBuildToolsVersion(buildToolsVersion)
Expand All @@ -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")
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 98bd4ae

Please sign in to comment.