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-08-02 11:16:06 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-08-02 11:17:38 -0700
commit272c93ce4faece38e7f95508393d1dc15bf4b031 (patch)
treee28933ad7f3def55c9a673e94d18c37c2a244c2c /src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java
parentee9653458fb11be2ca8c9001c81bdffe6e593ce8 (diff)
Expose AndroidResourceUsageAnalyzer methods to enable proto-based resource shrinking without tool attributes.
RELNOTES: PiperOrigin-RevId: 207132534
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.java31
1 files changed, 22 insertions, 9 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 cf482ff030..ee8e57df8f 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
@@ -21,6 +21,8 @@ import com.google.common.io.ByteStreams;
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.ProtoApk;
+import com.google.devtools.build.android.aapt2.ProtoResourceUsageAnalyzer;
import com.google.devtools.build.android.aapt2.ResourceCompiler;
import com.google.devtools.build.android.aapt2.ResourceLinker;
import java.io.FileOutputStream;
@@ -142,10 +144,13 @@ public class ResourcesZip {
throw new RuntimeException(e);
}
});
- return from(
+ return new ResourcesZip(
Files.createDirectories(workingDirectory.resolve("res")),
Files.createDirectories(workingDirectory.resolve("assets")),
- workingDirectory.resolve("ids.txt"));
+ workingDirectory.resolve("ids.txt"),
+ null,
+ workingDirectory.resolve("apk.pb"),
+ workingDirectory.resolve("tools.attributes.pb"));
}
/**
@@ -233,14 +238,18 @@ public class ResourcesZip {
public ShrunkProtoApk shrinkUsingProto(
Set<String> packages,
- Path rTxt,
Path classJar,
- Path primaryManifest,
Path proguardMapping,
Path logFile,
Path workingDirectory)
- throws ParserConfigurationException {
- throw new UnsupportedOperationException();
+ throws ParserConfigurationException, IOException, SAXException {
+ final Path shrunkApkProto = workingDirectory.resolve("shrunk.apk.pb");
+ try (final ProtoApk apk = ProtoApk.readFrom(proto)) {
+ // record resources and manifest
+ new ProtoResourceUsageAnalyzer(packages, proguardMapping, logFile)
+ .shrink(apk, classJar, shrunkApkProto);
+ return new ShrunkProtoApk(shrunkApkProto, logFile);
+ }
}
static class ShrunkProtoApk {
@@ -252,8 +261,12 @@ public class ResourcesZip {
this.report = report;
}
- ShrunkProtoApk writeBinaryTo(ResourceLinker linker, Path binaryOut) throws IOException {
- Files.copy(linker.convertToBinary(apk), binaryOut, StandardCopyOption.REPLACE_EXISTING);
+ ShrunkProtoApk writeBinaryTo(ResourceLinker linker, Path binaryOut, boolean writeAsProto)
+ throws IOException {
+ Files.copy(
+ writeAsProto ? apk : linker.optimizeApk(linker.convertToBinary(apk)),
+ binaryOut,
+ StandardCopyOption.REPLACE_EXISTING);
return this;
}
@@ -262,7 +275,7 @@ public class ResourcesZip {
return this;
}
- ShrunkProtoApk writeResourceToZip(Path resourcesZip) throws IOException {
+ ShrunkProtoApk writeResourcesToZip(Path resourcesZip) throws IOException {
try (final ZipBuilder zip = ZipBuilder.createFor(resourcesZip)) {
zip.addEntry("apk.pb", Files.readAllBytes(apk), ZipEntry.STORED);
}