diff options
author | 2017-09-29 23:25:39 +0200 | |
---|---|---|
committer | 2017-10-02 10:31:47 +0200 | |
commit | 9a54b435b44c00d5a70e3d6a2a9a3949e3d00548 (patch) | |
tree | adaeb035ae2b39d8bc0b7f026cd82cedee0a40f1 /src/tools/android/java/com/google/devtools/build/android | |
parent | c8098d0e089d84268877bd30d4b1c0eae110deb3 (diff) |
Fix aapt2 actions to use the compiled intermediate resource files for linking. Also include assets in the aapt2 packaging action.
RELNOTES: none
PiperOrigin-RevId: 170532322
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android')
5 files changed, 71 insertions, 44 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java b/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java index 2ddcdc9acc..5242aae0f4 100644 --- a/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java +++ b/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java @@ -147,18 +147,25 @@ public class Aapt2ResourcePackagingAction { AndroidResourceOutputs.copyManifestToOutput(compiled, options.manifestOutput); } - List<StaticLibrary> dependencies = + List<CompiledResources> compiledResourceDeps = // Last defined dependencies will overwrite previous one, so always place direct // after transitive. concat(options.transitiveData.stream(), options.directData.stream()) - .map(DependencyAndroidData::getStaticLibrary) + .map(DependencyAndroidData::getCompiledSymbols) .collect(toList()); + List<Path> assetDirs = + concat(options.transitiveData.stream(), options.directData.stream()) + .flatMap(dep -> dep.assetDirs.stream()) + .collect(toList()); + assetDirs.addAll(options.primaryData.assetDirs); + final PackagedResources packagedResources = ResourceLinker.create(aaptConfigOptions.aapt2, linkedOut) .profileUsing(profiler) .dependencies(ImmutableList.of(StaticLibrary.from(aaptConfigOptions.androidJar))) - .include(dependencies) + .include(compiledResourceDeps) + .withAssets(assetDirs) .buildVersion(aaptConfigOptions.buildToolsVersion) .filterToDensity(densitiesToFilter) .link(compiled) diff --git a/src/tools/android/java/com/google/devtools/build/android/DependencyAndroidData.java b/src/tools/android/java/com/google/devtools/build/android/DependencyAndroidData.java index 72f0ab5db6..d2c72cd644 100644 --- a/src/tools/android/java/com/google/devtools/build/android/DependencyAndroidData.java +++ b/src/tools/android/java/com/google/devtools/build/android/DependencyAndroidData.java @@ -16,7 +16,7 @@ package com.google.devtools.build.android; import com.android.builder.dependency.SymbolFileProvider; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; -import com.google.devtools.build.android.aapt2.StaticLibrary; +import com.google.devtools.build.android.aapt2.CompiledResources; import java.io.File; import java.nio.file.FileSystem; import java.nio.file.FileSystems; @@ -55,11 +55,11 @@ class DependencyAndroidData extends SerializedAndroidData { Path rTxt = exists(fileSystem.getPath(parts[3])); ImmutableList<Path> assetDirs = parts[1].length() == 0 ? ImmutableList.<Path>of() : splitPaths(parts[1], fileSystem); - StaticLibrary staticLibrary = null; + CompiledResources compiledSymbols = null; Path symbolsBin = null; - if (parts.length == 6) { // contains symbols bin and static library - staticLibrary = StaticLibrary.from(exists(fileSystem.getPath(parts[4])), rTxt, assetDirs); + if (parts.length == 6) { // contains symbols bin and compiled symbols + compiledSymbols = CompiledResources.from(exists(fileSystem.getPath(parts[4]))); symbolsBin = exists(fileSystem.getPath(parts[5])); } else if (parts.length == 5) { // contains symbols bin symbolsBin = exists(fileSystem.getPath(parts[4])); @@ -72,12 +72,12 @@ class DependencyAndroidData extends SerializedAndroidData { exists(fileSystem.getPath(parts[2])), rTxt, symbolsBin, - staticLibrary); + compiledSymbols); } private final Path manifest; private final Path rTxt; - private final StaticLibrary staticLibrary; + private final CompiledResources compiledSymbols; public DependencyAndroidData( ImmutableList<Path> resourceDirs, @@ -85,12 +85,12 @@ class DependencyAndroidData extends SerializedAndroidData { Path manifest, Path rTxt, Path symbols, - StaticLibrary staticLibrary) { + CompiledResources compiledSymbols) { // Use the manifest as a label for now. super(resourceDirs, assetDirs, manifest.toString(), symbols); this.manifest = manifest; this.rTxt = rTxt; - this.staticLibrary = staticLibrary; + this.compiledSymbols = compiledSymbols; } public SymbolFileProvider asSymbolFileProvider() { @@ -127,8 +127,8 @@ class DependencyAndroidData extends SerializedAndroidData { }; } - public StaticLibrary getStaticLibrary() { - return staticLibrary; + public CompiledResources getCompiledSymbols() { + return compiledSymbols; } @Override 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 9574e96827..b572dd5993 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 @@ -28,6 +28,7 @@ import java.nio.file.FileSystems; import java.nio.file.Path; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; /** Performs resource validation and static linking for compiled android resources. */ public class ValidateAndLinkResourcesAction { @@ -49,6 +50,19 @@ public class ValidateAndLinkResourcesAction { public Path compiled; @Option( + name = "compiledDep", + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + effectTags = {OptionEffectTag.UNKNOWN}, + defaultValue = "", + converter = Converters.PathListConverter.class, + category = "input", + allowMultiple = true, + help = "Compiled resource dependencies to link." + ) + public List<Path> compiledDeps; + + + @Option( name = "manifest", documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, effectTags = {OptionEffectTag.UNKNOWN}, @@ -172,6 +186,9 @@ public class ValidateAndLinkResourcesAction { ResourceLinker.create(aapt2Options.aapt2, scopedTmp.getPath()) .profileUsing(profiler) .dependencies(Optional.ofNullable(options.deprecatedLibraries).orElse(options.libraries)) + .include(options.compiledDeps.stream() + .map(CompiledResources::from) + .collect(Collectors.toList())) .buildVersion(aapt2Options.buildToolsVersion) .linkStatically(resources) .copyLibraryTo(options.staticLibraryOut) diff --git a/src/tools/android/java/com/google/devtools/build/android/aapt2/CompiledResources.java b/src/tools/android/java/com/google/devtools/build/android/aapt2/CompiledResources.java index 21eb59dd15..2e96319a81 100644 --- a/src/tools/android/java/com/google/devtools/build/android/aapt2/CompiledResources.java +++ b/src/tools/android/java/com/google/devtools/build/android/aapt2/CompiledResources.java @@ -37,6 +37,12 @@ public class CompiledResources implements ManifestContainer { private final List<Path> assetsDirs; private final Optional<Path> stableIds; + /** + * @param resources The path to the zip file containing the compiled resource result of aapt2 + * @param manifest The path to the manifest file for this set of resources + * @param assetsDirs The list of asset directors for the target of this set of resources + * @param stableIds The path to the file containing resource ID's to keep stable + */ private CompiledResources( Path resources, Path manifest, List<Path> assetsDirs, Optional<Path> stableIds) { this.resources = resources; @@ -45,16 +51,23 @@ public class CompiledResources implements ManifestContainer { this.stableIds = stableIds; } + public static CompiledResources from(Path resources) { + return from(resources, null, ImmutableList.of()); + } + public static CompiledResources from(Path resources, Path manifest) { return from(resources, manifest, ImmutableList.of()); } public static CompiledResources from( - Path resources, Path manifest, @Nullable List<Path> assetDirs) { + Path resources, @Nullable Path manifest, @Nullable List<Path> assetDirs) { return new CompiledResources( resources, manifest, assetDirs != null ? assetDirs : ImmutableList.of(), Optional.empty()); } + /** + * This zip file contains resource flat files that are the result of aapt2 compile + */ public Path getZip() { return 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 e15ee6670f..b1b625e993 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 @@ -19,7 +19,6 @@ import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; -import com.google.common.io.ByteStreams; import com.google.devtools.build.android.AaptCommandBuilder; import com.google.devtools.build.android.AndroidResourceOutputs; import com.google.devtools.build.android.Profiler; @@ -31,7 +30,6 @@ import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; -import java.util.zip.ZipFile; /** Performs linking of {@link CompiledResources} using aapt2. */ public class ResourceLinker { @@ -59,7 +57,8 @@ public class ResourceLinker { private List<String> uncompressedExtensions = ImmutableList.of(); private List<String> resourceConfigs = ImmutableList.of(); private Path baseApk; - private List<StaticLibrary> include = ImmutableList.of(); + private List<CompiledResources> include = ImmutableList.of(); + private List<Path> assetDirs = ImmutableList.of(); private ResourceLinker(Path aapt2, Path workingDirectory) { this.aapt2 = aapt2; @@ -82,11 +81,15 @@ public class ResourceLinker { return this; } - /** Dependent static libraries to be included in the binary. */ - public ResourceLinker include(List<StaticLibrary> include) { + /** Dependent compiled resources to be included in the binary. */ + public ResourceLinker include(List<CompiledResources> include) { this.include = include; return this; } + public ResourceLinker withAssets(List<Path> assetDirs) { + this.assetDirs = assetDirs; + return this; + } public ResourceLinker buildVersion(Revision buildToolsVersion) { this.buildToolsVersion = buildToolsVersion; @@ -127,9 +130,12 @@ public class ResourceLinker { .add("--no-static-lib-packages") .whenVersionIsAtLeast(new Revision(23)) .thenAdd("--no-version-vectors") - .addRepeated("-R", unzipCompiledResources(resources.getZip())) + .add("-R", resources.getZip()) + .addRepeated("-R", + include.stream() + .map(compiledResources -> compiledResources.getZip().toString()) + .collect(Collectors.toList())) .addRepeated("-I", StaticLibrary.toPathStrings(linkAgainst)) - .add("--java", javaSourceDirectory) .add("--auto-add-overlay") .add("-o", outPath) .add("--java", javaSourceDirectory) @@ -171,16 +177,18 @@ public class ResourceLinker { .when(densities.size() == 1) .thenAddRepeated("--preferred-density", densities) .add("--stable-ids", compiled.getStableIds()) - .addRepeated("-A", compiled.getAssetsStrings()) + .addRepeated("-A", + assetDirs.stream().map(Path::toString).collect(Collectors.toList())) .addRepeated("-I", StaticLibrary.toPathStrings(linkAgainst)) - .addRepeated("-R", StaticLibrary.toPathStrings(include)) - .addParameterableRepeated( - "-R", unzipCompiledResources(compiled.getZip()), workingDirectory) + .addRepeated("-R", + include.stream() + .map(compiledResources -> compiledResources.getZip().toString()) + .collect(Collectors.toList())) + .add("-R", compiled.getZip()) // Never compress apks. .add("-0", "apk") // Add custom no-compress extensions. .addRepeated("-0", uncompressedExtensions) - .addRepeated("-A", StaticLibrary.toAssetPaths(include)) // Filter by resource configuration type. .when(!resourceConfigs.isEmpty()) .thenAdd("-c", Joiner.on(',').join(resourceConfigs)) @@ -215,24 +223,6 @@ public class ResourceLinker { } } - private List<String> unzipCompiledResources(Path resourceZip) throws IOException { - final ZipFile zipFile = new ZipFile(resourceZip.toFile()); - return zipFile - .stream() - .map( - entry -> { - final Path resolve = workingDirectory.resolve(entry.getName()); - try { - Files.createDirectories(resolve.getParent()); - return Files.write(resolve, ByteStreams.toByteArray(zipFile.getInputStream(entry))); - } catch (IOException e) { - throw new RuntimeException(e); - } - }) - .map(Path::toString) - .collect(Collectors.toList()); - } - public ResourceLinker storeUncompressed(List<String> uncompressedExtensions) { this.uncompressedExtensions = uncompressedExtensions; return this; |