Skip to content

Commit

Permalink
Add the ability to output the resource table as a protocol buffer to …
Browse files Browse the repository at this point in the history
…the Aapt2ResourcePackagingAction

Ensure that the custom package is respected in aapt2. This only impact the srcjar, as the classes are generated via another method.

RELNOTES: None
PiperOrigin-RevId: 183260369
  • Loading branch information
corbinrsmith-work authored and Copybara-Service committed Jan 25, 2018
1 parent 15bf148 commit ffaef29
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ public static void main(String[] args) throws Exception {
final PackagedResources packagedResources =
ResourceLinker.create(aaptConfigOptions.aapt2, linkedOut)
.profileUsing(profiler)
.customPackage(options.packageForR)
.outputAsProto(aaptConfigOptions.resourceTableAsProto)
.dependencies(ImmutableList.of(StaticLibrary.from(aaptConfigOptions.androidJar)))
.include(compiledResourceDeps)
.withAssets(assetDirs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,14 @@ public class Aapt2ConfigOptions extends OptionsBase {
help = "Use compiled resources for merging rather than parsed symbols binary."
)
public boolean useCompiledResourcesForMerge;

@Option(
name = "resourceTableAsProto",
defaultValue = "false",
category = "config",
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
effectTags = {OptionEffectTag.UNKNOWN},
help = "Generate the resource table as a protocol buffer."
)
public boolean resourceTableAsProto;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
/** Performs linking of {@link CompiledResources} using aapt2. */
public class ResourceLinker {

private String customPackage;
private boolean outputAsProto;

/** Represents errors thrown during linking. */
public static class LinkError extends Aapt2Exception {

Expand Down Expand Up @@ -116,11 +119,21 @@ public ResourceLinker baseApkToLinkAgainst(Path baseApk) {
return this;
}

public ResourceLinker customPackage(String customPackage) {
this.customPackage = customPackage;
return this;
}

public ResourceLinker filterToDensity(List<String> densities) {
this.densities = densities;
return this;
}

public ResourceLinker outputAsProto(boolean outputAsProto) {
this.outputAsProto = outputAsProto;
return this;
}

/**
* Statically links the {@link CompiledResources} with the dependencies to produce a {@link
* StaticLibrary}.
Expand Down Expand Up @@ -149,8 +162,11 @@ public StaticLibrary linkStatically(CompiledResources resources) {
.add("--manifest", resources.getManifest())
.add("--static-lib")
.add("--no-static-lib-packages")
.add("--custom-package", customPackage)
.whenVersionIsAtLeast(new Revision(23))
.thenAdd("--no-version-vectors")
.when(outputAsProto)
.thenAdd("--proto-format")
.add("-R", resources.getZip())
.addRepeated("-R", compiledResourcePaths)
.addRepeated("-I", pathsToLinkAgainst)
Expand All @@ -165,7 +181,7 @@ public StaticLibrary linkStatically(CompiledResources resources) {
// working around aapt2 not producing transitive R.txt and R.java
if (linkAgainst.size() > 1) {
profiler.startTask("rfix");
logger.fine(
logger.info(
new AaptCommandBuilder(aapt2)
.forBuildToolsVersion(buildToolsVersion)
.forVariantType(VariantType.LIBRARY)
Expand All @@ -174,6 +190,8 @@ public StaticLibrary linkStatically(CompiledResources resources) {
.add("--no-static-lib-packages")
.whenVersionIsAtLeast(new Revision(23))
.thenAdd("--no-version-vectors")
.when(outputAsProto)
.thenAdd("--proto-format")
// only link against jars
.addRepeated(
"-I",
Expand Down Expand Up @@ -219,11 +237,14 @@ public PackagedResources link(CompiledResources compiled) {
.thenAdd("--no-version-vectors")
// Turn off namespaced resources
.add("--no-static-lib-packages")
.when(outputAsProto)
.thenAdd("--proto-format")
.when(Objects.equals(logger.getLevel(), Level.FINE))
.thenAdd("-v")
.add("--manifest", compiled.getManifest())
// Enables resource redefinition and merging
.add("--auto-add-overlay")
.add("--custom-package", customPackage)
.when(densities.size() == 1)
.thenAddRepeated("--preferred-density", densities)
.add("--stable-ids", compiled.getStableIds())
Expand Down

0 comments on commit ffaef29

Please sign in to comment.