diff options
author | corysmith <corysmith@google.com> | 2018-07-31 09:28:07 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-07-31 09:29:56 -0700 |
commit | 0a635c5236ce30ea84b765ce752267992733a649 (patch) | |
tree | 7d12075435331b820bfe88f4efbee69dbcbcd7c2 /src/tools/android/java/com/google/devtools/build/android/aapt2 | |
parent | a0f77be2887cc69ea43991dfb5a58934eb539c65 (diff) |
Automated rollback of commit 8fe0f45852a620a078013310989396caed273342.
*** Reason for rollback ***
Breaks a couple of builds due to a bad merge.
*** Original change description ***
Add apk converted to proto and all attributes from CompiledResources to ResourcesZip.
Add new proto format for tool attributes stored in the AndroidDataXml for storing them in the resources.zip.
RELNOTES:None
PiperOrigin-RevId: 206774364
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/aapt2')
-rw-r--r-- | src/tools/android/java/com/google/devtools/build/android/aapt2/PackagedResources.java | 45 | ||||
-rw-r--r-- | src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java | 171 |
2 files changed, 17 insertions, 199 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/aapt2/PackagedResources.java b/src/tools/android/java/com/google/devtools/build/android/aapt2/PackagedResources.java index 2d92b4e071..faad2cd868 100644 --- a/src/tools/android/java/com/google/devtools/build/android/aapt2/PackagedResources.java +++ b/src/tools/android/java/com/google/devtools/build/android/aapt2/PackagedResources.java @@ -14,7 +14,6 @@ package com.google.devtools.build.android.aapt2; import com.google.devtools.build.android.AndroidResourceOutputs; -import com.google.devtools.build.android.ResourcesZip; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -24,64 +23,47 @@ import javax.annotation.Nullable; public class PackagedResources { private final Path apk; - private final Path proto; private final Path rTxt; private final Path proguardConfig; private final Path mainDexProguard; private final Path javaSourceDirectory; private final Path resourceIds; - private final Path attributes; private PackagedResources( Path apk, - Path proto, Path rTxt, Path proguardConfig, Path mainDexProguard, Path javaSourceDirectory, - Path resourceIds, - Path attributes) { + Path resourceIds) { this.apk = apk; - this.proto = proto; this.rTxt = rTxt; this.proguardConfig = proguardConfig; this.mainDexProguard = mainDexProguard; this.javaSourceDirectory = javaSourceDirectory; this.resourceIds = resourceIds; - this.attributes = attributes; } public static PackagedResources of( Path outPath, - Path protoPath, Path rTxt, Path proguardConfig, Path mainDexProguard, Path javaSourceDirectory, - Path resourceIds, - Path attributes) + Path resourceIds) throws IOException { return new PackagedResources( - outPath, - protoPath, - rTxt, - proguardConfig, - mainDexProguard, - javaSourceDirectory, - resourceIds, - attributes); + outPath, rTxt, proguardConfig, mainDexProguard, javaSourceDirectory, resourceIds); } public PackagedResources copyPackageTo(Path packagePath) throws IOException { return of( copy(apk, packagePath), - proto, rTxt, proguardConfig, mainDexProguard, javaSourceDirectory, - resourceIds, - attributes); + resourceIds); } public PackagedResources copyRTxtTo(Path rOutput) throws IOException { @@ -90,13 +72,11 @@ public class PackagedResources { } return new PackagedResources( apk, - proto, copy(rTxt, rOutput), proguardConfig, mainDexProguard, javaSourceDirectory, - resourceIds, - attributes); + resourceIds); } private Path copy(Path from, Path out) throws IOException { @@ -111,13 +91,11 @@ public class PackagedResources { } return of( apk, - proto, rTxt, copy(proguardConfig, proguardOut), mainDexProguard, javaSourceDirectory, - resourceIds, - attributes); + resourceIds); } public PackagedResources copyMainDexProguardTo(Path mainDexProguardOut) throws IOException { @@ -126,13 +104,11 @@ public class PackagedResources { } return of( apk, - proto, rTxt, proguardConfig, copy(mainDexProguard, mainDexProguardOut), javaSourceDirectory, - resourceIds, - attributes); + resourceIds); } public PackagedResources createSourceJar(@Nullable Path sourceJarPath) throws IOException { @@ -140,12 +116,7 @@ public class PackagedResources { return this; } AndroidResourceOutputs.createSrcJar(javaSourceDirectory, sourceJarPath, false); - return of( - apk, proto, rTxt, proguardConfig, mainDexProguard, sourceJarPath, resourceIds, attributes); - } - - public ResourcesZip packageWith(Path resourceRoot) { - return ResourcesZip.fromApkWithProto(proto, attributes, resourceRoot, apk, resourceIds); + return of(apk, rTxt, proguardConfig, mainDexProguard, sourceJarPath, resourceIds); } public Path getResourceIds() { 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 9ea3703d42..3839a5d94b 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 @@ -21,39 +21,26 @@ import com.google.common.base.Joiner; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.base.Strings; -import com.google.common.collect.HashMultimap; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Multimap; import com.google.common.collect.Streams; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.devtools.build.android.AaptCommandBuilder; -import com.google.devtools.build.android.AndroidCompiledDataDeserializer; -import com.google.devtools.build.android.AndroidDataWritingVisitor; -import com.google.devtools.build.android.AndroidResourceMerger.MergingException; import com.google.devtools.build.android.AndroidResourceOutputs; -import com.google.devtools.build.android.FullyQualifiedName; import com.google.devtools.build.android.Profiler; import com.google.devtools.build.android.aapt2.ResourceCompiler.CompiledType; -import com.google.devtools.build.android.proto.SerializeFormat.ToolAttributes; -import com.google.devtools.build.android.xml.Namespaces; import com.google.devtools.build.android.ziputils.DirectoryEntry; import com.google.devtools.build.android.ziputils.ZipIn; import com.google.devtools.build.android.ziputils.ZipOut; -import java.io.BufferedOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.Collection; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; import java.util.Objects; import java.util.Optional; -import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.function.Function; @@ -276,7 +263,7 @@ public class ResourceLinker { } private List<String> compiledResourcesToPaths( - CompiledResources compiled, Predicate<DirectoryEntry> shouldKeep) { + CompiledResources compiled, Predicate<DirectoryEntry> shouldKeep) throws IOException { // Using sequential streams to maintain the overlay order for aapt2. return Stream.concat(include.stream(), Stream.of(compiled)) .sequential() @@ -336,61 +323,9 @@ public class ResourceLinker { R apply(T arg) throws Throwable; } - private String replaceExtension(String fileName, String newExtension) { - int lastIndex = fileName.lastIndexOf('.'); - if (lastIndex == -1) { - return fileName.concat(".").concat(newExtension); - } - return fileName.substring(0, lastIndex).concat(".").concat(newExtension); - } - - public Path convertToBinary(Path protoApk) { - try { - profiler.startTask("convertToBinary"); - final Path outPath = - workingDirectory.resolveSibling( - replaceExtension(protoApk.getFileName().toString(), "apk")); - logger.fine( - new AaptCommandBuilder(aapt2) - .add("convert") - .add("-o", outPath) - .add("--output-format", "binary") - .add(protoApk.toString()) - .execute("Converting " + protoApk)); - profiler.recordEndOf("convertToBinary"); - return outPath; - } catch (IOException e) { - throw new LinkError(e); - } - } - - public Path optimizeApk(Path apk) { - try { - profiler.startTask("optimizeApk"); - final Path outPath = - workingDirectory.resolveSibling( - replaceExtension(apk.getFileName().toString(), ".optimized.apk")); - logger.fine( - new AaptCommandBuilder(aapt2) - .forBuildToolsVersion(buildToolsVersion) - .forVariantType(VariantType.DEFAULT) - .add("optimize") - .when(Objects.equals(logger.getLevel(), Level.FINE)) - .thenAdd("-v") - .add("-o", outPath) - .add(apk.toString()) - .execute(String.format("Optimizing %s", apk))); - return outPath; - } catch (IOException e) { - throw new LinkError(e); - } finally { - profiler.recordEndOf("optimizeApk"); - } - } - public PackagedResources link(CompiledResources compiled) { try { - final Path outPath = workingDirectory.resolve("bin.pb"); + final Path outPath = workingDirectory.resolve("bin.apk"); Path rTxt = workingDirectory.resolve("R.txt"); Path proguardConfig = workingDirectory.resolve("proguard.cfg"); Path mainDexProguard = workingDirectory.resolve("proguard.maindex.cfg"); @@ -407,7 +342,8 @@ public class ResourceLinker { .thenAdd("--no-version-vectors") // Turn off namespaced resources .add("--no-static-lib-packages") - .add("--proto-format") + .when(outputAsProto) + .thenAdd("--proto-format") .when(Objects.equals(logger.getLevel(), Level.FINE)) .thenAdd("-v") .add("--manifest", compiled.getManifest()) @@ -451,35 +387,13 @@ public class ResourceLinker { .thenAdd("--proguard-conditional-keep-rules") .add("-o", outPath) .execute(String.format("Linking %s", compiled.getManifest()))); - profiler.recordEndOf("fulllink").startTask("attributes"); - - final Path attributes = workingDirectory.resolve("tool.attributes"); - // extract tool annotations from the compile resources. - final ToolProtoWriter writer = new ToolProtoWriter(attributes); - Stream.concat(include.stream(), Stream.of(compiled)) - .parallel() - .map(AndroidCompiledDataDeserializer.create()::readAttributes) - .map(Map::entrySet) - .flatMap(Set::stream) - .distinct() - .forEach(e -> e.getValue().writeResource((FullyQualifiedName) e.getKey(), writer)); - writer.flush(); - - profiler.recordEndOf("attributes"); + profiler.recordEndOf("fulllink"); + profiler.startTask("optimize"); if (densities.size() < 2) { return PackagedResources.of( - outputAsProto ? outPath : convertToBinary(outPath), // convert proto to apk - outPath, - rTxt, - proguardConfig, - mainDexProguard, - javaSourceDirectory, - resourceIds, - attributes); + outPath, rTxt, proguardConfig, mainDexProguard, javaSourceDirectory, resourceIds); } - - profiler.startTask("optimize"); - final Path optimized = workingDirectory.resolve("optimized.pb"); + final Path optimized = workingDirectory.resolve("optimized.apk"); logger.fine( new AaptCommandBuilder(aapt2) .forBuildToolsVersion(buildToolsVersion) @@ -492,16 +406,8 @@ public class ResourceLinker { .add(outPath.toString()) .execute(String.format("Optimizing %s", compiled.getManifest()))); profiler.recordEndOf("optimize"); - return PackagedResources.of( - outputAsProto ? optimized : convertToBinary(optimized), - optimized, - rTxt, - proguardConfig, - mainDexProguard, - javaSourceDirectory, - resourceIds, - attributes); + optimized, rTxt, proguardConfig, mainDexProguard, javaSourceDirectory, resourceIds); } catch (IOException e) { throw new LinkError(e); } @@ -536,63 +442,4 @@ public class ResourceLinker { .add("baseApk", baseApk) .toString(); } - - private static class ToolProtoWriter implements AndroidDataWritingVisitor { - - final Multimap<String, String> attributes = HashMultimap.create(); - private final Path out; - - ToolProtoWriter(Path out) { - this.out = out; - } - - @Override - public void flush() throws IOException { - ToolAttributes.Builder builder = ToolAttributes.newBuilder(); - for (Entry<String, Collection<String>> entry : attributes.asMap().entrySet()) { - builder.putAttributes( - entry.getKey(), - ToolAttributes.ToolAttributeValues.newBuilder().addAllValues(entry.getValue()).build()); - } - try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(out))) { - builder.build().writeTo(stream); - } - } - - @Override - public Path copyManifest(Path sourceManifest) { - throw new UnsupportedOperationException(); - } - - @Override - public void copyAsset(Path source, String relativeDestinationPath) { - throw new UnsupportedOperationException(); - } - - @Override - public void copyResource(Path source, String relativeDestinationPath) throws MergingException { - throw new UnsupportedOperationException(); - } - - @Override - public void defineAttribute(FullyQualifiedName fqn, String name, String value) { - attributes.put(removeNamespace(name), value); - } - - private String removeNamespace(String qualifiedName) { - int indexColon = qualifiedName.indexOf(':'); - if (indexColon == -1) { - return qualifiedName; - } - return qualifiedName.substring(indexColon); - } - - @Override - public void defineNamespacesFor(FullyQualifiedName fqn, Namespaces namespaces) {} - - @Override - public ValueResourceDefinitionMetadata define(FullyQualifiedName fqn) { - throw new UnsupportedOperationException(); - } - } } |