From 241d6cad572f4ba67eb9cdcfddd3ac109c557f0f Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 20 Dec 2017 09:29:45 -0800 Subject: Use assets from the resource APK for resource shrinking rather than from the merge actions. RELNOTES: none PiperOrigin-RevId: 179695515 --- .../devtools/build/android/ResourcesZip.java | 49 +++++++++++++++++++--- 1 file changed, 44 insertions(+), 5 deletions(-) (limited to 'src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java') 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 edb254434c..3b6b160f54 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 @@ -41,12 +41,18 @@ public class ResourcesZip { private final Path resourcesRoot; private final Path assetsRoot; + private final Optional apkWithAssets; private final Optional ids; - private ResourcesZip(Path resourcesRoot, Path assetsRoot, Optional ids) { + private ResourcesZip( + Path resourcesRoot, + Path assetsRoot, + Optional ids, + Optional apkWithAssets) { this.resourcesRoot = resourcesRoot; this.assetsRoot = assetsRoot; this.ids = ids; + this.apkWithAssets = apkWithAssets; } /** @@ -54,7 +60,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, Optional.empty()); + return new ResourcesZip(resourcesRoot, assetsRoot, Optional.empty(), Optional.empty()); } /** @@ -64,7 +70,23 @@ public class ResourcesZip { */ public static ResourcesZip from(Path resourcesRoot, Path assetsRoot, Path resourceIds) { return new ResourcesZip( - resourcesRoot, assetsRoot, Optional.of(resourceIds).filter(Files::exists)); + resourcesRoot, + assetsRoot, + Optional.of(resourceIds).filter(Files::exists), + Optional.empty()); + } + + /** + * @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 fromApk(Path resourcesRoot, Path apkWithAssets, Path resourceIds) { + return new ResourcesZip( + resourcesRoot, + null /* assetsRoot */, + Optional.of(resourceIds).filter(Files::exists), + Optional.of(apkWithAssets)); } /** Creates a ResourcesZip from an archive by expanding into the workingDirectory. */ @@ -116,7 +138,24 @@ public class ResourcesZip { } visitor.writeEntries(); } - if (Files.exists(assetsRoot)) { + + if (apkWithAssets.isPresent()){ + ZipFile apkZip = new ZipFile(apkWithAssets.get().toString()); + apkZip + .stream() + .filter(entry -> entry.getName().startsWith("assets/")) + .forEach( + entry -> { + try { + zip.addEntry( + entry, + ByteStreams.toByteArray(apkZip.getInputStream(entry))); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + zip.addEntry("assets/", new byte[0], compress ? ZipEntry.DEFLATED : ZipEntry.STORED); + } else if (Files.exists(assetsRoot)) { ZipBuilderVisitorWithDirectories visitor = new ZipBuilderVisitorWithDirectories(zip, assetsRoot, "assets"); visitor.setCompress(compress); @@ -150,7 +189,7 @@ public class ResourcesZip { packages, rTxt, classJar, manifest, proguardMapping, resourcesRoot, logFile) .shrink(workingDirectory); return ShrunkResources.of( - new ResourcesZip(workingDirectory, assetsRoot, ids), + new ResourcesZip(workingDirectory, assetsRoot, ids, Optional.empty()), new UnvalidatedAndroidData( ImmutableList.of(workingDirectory), ImmutableList.of(assetsRoot), manifest)); } -- cgit v1.2.3