diff options
author | Googler <noreply@google.com> | 2017-12-20 09:29:45 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2017-12-20 10:39:56 -0800 |
commit | 241d6cad572f4ba67eb9cdcfddd3ac109c557f0f (patch) | |
tree | a3ef5a7b03a195752ccf602fefb524735f303cde /src/tools/android/java/com/google/devtools/build/android/ResourcesZip.java | |
parent | 53d568de5ddd3b529c92d37c9112d15dd6844baa (diff) |
Use assets from the resource APK for resource shrinking rather than from the merge actions.
RELNOTES: none
PiperOrigin-RevId: 179695515
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.java | 49 |
1 files changed, 44 insertions, 5 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 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<Path> apkWithAssets; private final Optional<Path> ids; - private ResourcesZip(Path resourcesRoot, Path assetsRoot, Optional<Path> ids) { + private ResourcesZip( + Path resourcesRoot, + Path assetsRoot, + Optional<Path> ids, + Optional<Path> 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)); } |