aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java
diff options
context:
space:
mode:
authorGravatar corysmith <corysmith@google.com>2018-07-31 10:39:15 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-07-31 10:41:13 -0700
commit3852edd46cb777191728c42e62873e39fd9fe4c4 (patch)
tree8acda6f07d4ff6d0e65337c0bbffd5040220fec0 /src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java
parentab8a844240cf6ccb4ee46e6bb8795fcc2b3fe6be (diff)
Automated rollback of commit 0a635c5236ce30ea84b765ce752267992733a649.
*** Reason for rollback *** Rolling forward with the correct attribute handling. *** Original change description *** 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: 206786645
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java')
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java96
1 files changed, 90 insertions, 6 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java b/src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java
index dd3cb099f3..cf482ff030 100644
--- a/src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java
+++ b/src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java
@@ -22,13 +22,16 @@ import com.google.devtools.build.android.AndroidResourceOutputs.ZipBuilder;
import com.google.devtools.build.android.AndroidResourceOutputs.ZipBuilderVisitorWithDirectories;
import com.google.devtools.build.android.aapt2.CompiledResources;
import com.google.devtools.build.android.aapt2.ResourceCompiler;
+import com.google.devtools.build.android.aapt2.ResourceLinker;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
import java.util.Set;
import java.util.concurrent.ExecutionException;
+import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.annotation.Nullable;
@@ -38,17 +41,28 @@ import org.xml.sax.SAXException;
/** Represents a collection of raw, merged resources with an optional id list. */
public class ResourcesZip {
- private final Path resourcesRoot;
- private final Path assetsRoot;
+ static final Logger logger = Logger.getLogger(ResourcesZip.class.toString());
+
+ @Nullable private final Path resourcesRoot;
+ @Nullable private final Path assetsRoot;
@Nullable private final Path apkWithAssets;
+ @Nullable private final Path proto;
+ @Nullable private final Path attributes;
@Nullable private final Path ids;
private ResourcesZip(
- Path resourcesRoot, Path assetsRoot, @Nullable Path ids, @Nullable Path apkWithAssets) {
+ @Nullable Path resourcesRoot,
+ @Nullable Path assetsRoot,
+ @Nullable Path ids,
+ @Nullable Path apkWithAssets,
+ @Nullable Path proto,
+ @Nullable Path attributes) {
this.resourcesRoot = resourcesRoot;
this.assetsRoot = assetsRoot;
this.ids = ids;
this.apkWithAssets = apkWithAssets;
+ this.proto = proto;
+ this.attributes = attributes;
}
/**
@@ -56,7 +70,7 @@ public class ResourcesZip {
* @param assetsRoot The root of the raw assets.
*/
public static ResourcesZip from(Path resourcesRoot, Path assetsRoot) {
- return new ResourcesZip(resourcesRoot, assetsRoot, null, null);
+ return new ResourcesZip(resourcesRoot, assetsRoot, null, null, null, null);
}
/**
@@ -69,6 +83,8 @@ public class ResourcesZip {
resourcesRoot,
assetsRoot,
resourceIds != null && Files.exists(resourceIds) ? resourceIds : null,
+ null,
+ null,
null);
}
@@ -82,7 +98,27 @@ public class ResourcesZip {
resourcesRoot,
/* assetsRoot= */ null,
resourceIds != null && Files.exists(resourceIds) ? resourceIds : null,
- apkWithAssets);
+ apkWithAssets,
+ null,
+ null);
+ }
+
+ /**
+ * @param proto apk in proto format.
+ * @param attributes Tooling attributes.
+ * @param resourcesRoot The root of the raw resources.
+ * @param apkWithAssets The apk containing assets.
+ * @param resourceIds Optional path to a file containing the resource ids.
+ */
+ public static ResourcesZip fromApkWithProto(
+ Path proto, Path attributes, Path resourcesRoot, Path apkWithAssets, Path resourceIds) {
+ return new ResourcesZip(
+ resourcesRoot,
+ /* assetsRoot= */ null,
+ resourceIds != null && Files.exists(resourceIds) ? resourceIds : null,
+ apkWithAssets,
+ proto,
+ attributes);
}
/** Creates a ResourcesZip from an archive by expanding into the workingDirectory. */
@@ -160,6 +196,15 @@ public class ResourcesZip {
if (ids != null) {
zip.addEntry("ids.txt", Files.readAllBytes(ids), ZipEntry.STORED);
}
+
+ if (proto != null && Files.exists(proto)) {
+ zip.addEntry("apk.pb", Files.readAllBytes(proto), ZipEntry.STORED);
+ }
+
+ if (attributes != null && Files.exists(attributes)) {
+ zip.addEntry("tools.attributes.pb", Files.readAllBytes(attributes), ZipEntry.STORED);
+ }
+
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -181,11 +226,50 @@ public class ResourcesZip {
packages, rTxt, classJar, manifest, proguardMapping, resourcesRoot, logFile)
.shrink(workingDirectory);
return ShrunkResources.of(
- new ResourcesZip(workingDirectory, assetsRoot, ids, null),
+ new ResourcesZip(workingDirectory, assetsRoot, ids, null, null, attributes),
new UnvalidatedAndroidData(
ImmutableList.of(workingDirectory), ImmutableList.of(assetsRoot), manifest));
}
+ public ShrunkProtoApk shrinkUsingProto(
+ Set<String> packages,
+ Path rTxt,
+ Path classJar,
+ Path primaryManifest,
+ Path proguardMapping,
+ Path logFile,
+ Path workingDirectory)
+ throws ParserConfigurationException {
+ throw new UnsupportedOperationException();
+ }
+
+ static class ShrunkProtoApk {
+ private final Path apk;
+ private final Path report;
+
+ ShrunkProtoApk(Path apk, Path report) {
+ this.apk = apk;
+ this.report = report;
+ }
+
+ ShrunkProtoApk writeBinaryTo(ResourceLinker linker, Path binaryOut) throws IOException {
+ Files.copy(linker.convertToBinary(apk), binaryOut, StandardCopyOption.REPLACE_EXISTING);
+ return this;
+ }
+
+ ShrunkProtoApk writeReportTo(Path reportOut) throws IOException {
+ Files.copy(report, reportOut);
+ return this;
+ }
+
+ ShrunkProtoApk writeResourceToZip(Path resourcesZip) throws IOException {
+ try (final ZipBuilder zip = ZipBuilder.createFor(resourcesZip)) {
+ zip.addEntry("apk.pb", Files.readAllBytes(apk), ZipEntry.STORED);
+ }
+ return this;
+ }
+ }
+
static class ShrunkResources {
private ResourcesZip resourcesZip;