use of com.android.zipflinger.Entry in project bundletool by google.
the class ModuleSplitSerializer method serialize.
/**
* Serializes module splits on disk under {@code outputDirectory}.
*
* <p>Returns {@link ApkDescription} for each serialized split keyed by relative path of module
* split.
*/
public ImmutableMap<ZipPath, ApkDescription> serialize(Path outputDirectory, ImmutableMap<ZipPath, ModuleSplit> splitsByRelativePath) {
// Prepare original splits by:
// * signing embedded APKs
// * injecting manifest and resource table as module entries.
ImmutableList<ModuleSplit> preparedSplits = splitsByRelativePath.values().stream().map(apkSigner::signEmbeddedApks).map(ModuleSplitSerializer::injectManifestAndResourceTableAsEntries).collect(toImmutableList());
try (SerializationFilesManager filesManager = new SerializationFilesManager()) {
// Convert module splits to binary format and apply uncompressed globs specified in
// BundleConfig. We do it in this order because as specified in documentation the matching
// for uncompressed globs is done against paths in final APKs.
ImmutableList<ModuleSplit> binarySplits = aapt2ResourceConverter.convert(preparedSplits, filesManager).stream().map(this::applyUncompressedGlobsAndUncompressedNativeLibraries).collect(toImmutableList());
// Build a pack from entries which may be compressed inside final APKs. 'May be
// compressed' means that for these entries we will decide later should they be compressed
// or not based on whether we gain enough savings from compression.
ModuleEntriesPack maybeCompressedEntriesPack = buildCompressedEntriesPack(filesManager.getCompressedResourceEntriesPackPath(), filesManager.getCompressedEntriesPackPath(), binarySplits);
// Build a pack with entries that are uncompressed in final APKs: force uncompressed entries
// + entries that have very low compression ratio.
ModuleEntriesPack uncompressedEntriesPack = buildUncompressedEntriesPack(filesManager.getUncompressedEntriesPackPath(), binarySplits, maybeCompressedEntriesPack);
// Now content of all binary apks is already moved to compressed/uncompressed packs. Delete
// them to free space.
filesManager.closeAndRemoveBinaryApks();
// Merge two packs together, so we have all entries for final APKs inside one pack. If the
// same entry is in both packs we prefer uncompressed one, because it means this entry
// has very low compression ratio, it makes no sense to put it in compressed form.
ModuleEntriesPack allEntriesPack = maybeCompressedEntriesPack.mergeWith(uncompressedEntriesPack);
// Serialize and sign final APKs.
ImmutableList<ListenableFuture<ApkDescription>> apkDescriptions = Streams.zip(splitsByRelativePath.keySet().stream(), binarySplits.stream(), (relativePath, split) -> executorService.submit(() -> serializeAndSignSplit(outputDirectory, relativePath, split, allEntriesPack, uncompressedEntriesPack))).collect(toImmutableList());
return ConcurrencyUtils.waitForAll(apkDescriptions).stream().collect(toImmutableMap(apk -> ZipPath.create(apk.getPath()), identity()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of com.android.zipflinger.Entry in project bundletool by google.
the class ModuleSplitSerializer method shouldUncompressBecauseOfLowRatio.
/**
* Whether module entry should be put in uncompressed form because savings for the entry is low.
*/
private boolean shouldUncompressBecauseOfLowRatio(ModuleEntry moduleEntry, ModuleEntriesPack compressedPack) {
Entry entry = compressedPack.getZipEntry(moduleEntry);
long compressedSize = entry.getCompressedSize();
long uncompressedSize = entry.getUncompressedSize();
// Copying logic from aapt2: require at least 10% gains in savings.
if (moduleEntry.getPath().startsWith("res")) {
return compressedSize + compressedSize / 10 > uncompressedSize;
}
return compressedSize >= uncompressedSize;
}
use of com.android.zipflinger.Entry in project bundletool by google.
the class ModuleSplitSerializer method serializeSplit.
private void serializeSplit(Path outputPath, ModuleSplit split, ModuleEntriesPack allEntriesPack, ModuleEntriesPack uncompressedEntriesPack) {
FileUtils.createDirectories(outputPath.getParent());
try (ZipArchive archive = new ZipArchive(outputPath)) {
ImmutableMap<ZipPath, ModuleEntry> moduleEntriesByName = split.getEntries().stream().collect(toImmutableMap(entry -> toApkEntryPath(entry.getPath()), entry -> entry, // e.g. base/assets/foo and base/root/assets/foo.
(a, b) -> b));
// Sorting entries by name for determinism.
ImmutableList<ModuleEntry> sortedEntries = ImmutableList.sortedCopyOf(Comparator.comparing(e -> toApkEntryPath(e.getPath())), moduleEntriesByName.values());
ZipSource zipSource = allEntriesPack.select(sortedEntries, entry -> toApkEntryPath(entry.getPath(), /* binaryApk= */
true).toString(), entry -> alignmentForEntry(entry, uncompressedEntriesPack));
archive.add(zipSource);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of com.android.zipflinger.Entry in project bundletool by google.
the class ZipFlingerApkSerializer method writeProtoApk.
/**
* Writes an APK for aapt2 to convert to binary.
*
* <p>This APK contains only files that aapt2 will convert from proto to binary, and the files
* needed for the conversion to succeed (e.g. all resources referenced in the resource table).
*
* <p>All files are left uncompressed to save aapt2 from re-compressing all the entries it
* generated since we prefer having control over which compression is used. This is effectively a
* tradeoff between local storage and CPU.
*
* <p>No entry is 4-byte aligned since it's only a temporary APK for aapt2 conversion which won't
* be actually stored or served to any device.
*/
private void writeProtoApk(ModuleSplit split, Path protoApkPath, TempDirectory tempDir) throws IOException {
try (ZipArchive apkWriter = new ZipArchive(protoApkPath)) {
apkWriter.add(new BytesSource(split.getAndroidManifest().getManifestRoot().getProto().toByteArray(), MANIFEST_FILENAME, NO_COMPRESSION.getValue()));
if (split.getResourceTable().isPresent()) {
BytesSource bytesSource = new BytesSource(split.getResourceTable().get().toByteArray(), SpecialModuleEntry.RESOURCE_TABLE.getPath().toString(), NO_COMPRESSION.getValue());
bytesSource.align(4);
apkWriter.add(bytesSource);
}
Map<String, Entry> bundleEntriesByName = bundleZipReader.getEntries();
ZipEntrySourceFactory sourceFactory = new ZipEntrySourceFactory(bundleZipReader, tempDir);
for (ModuleEntry moduleEntry : split.getEntries()) {
ZipPath pathInApk = ApkSerializerHelper.toApkEntryPath(moduleEntry.getPath());
if (!requiresAapt2Conversion(pathInApk)) {
continue;
}
if (moduleEntry.getBundleLocation().isPresent()) {
ZipPath pathInBundle = moduleEntry.getBundleLocation().get().entryPathInBundle();
Entry entry = bundleEntriesByName.get(pathInBundle.toString());
checkNotNull(entry, "Could not find entry '%s'.", pathInBundle);
apkWriter.add(sourceFactory.create(entry, pathInApk, NO_COMPRESSION));
} else {
apkWriter.add(new BytesSource(moduleEntry.getContent().read(), pathInApk.toString(), NO_COMPRESSION.getValue()));
}
}
}
}
use of com.android.zipflinger.Entry in project bundletool by google.
the class AppBundleRecompressor method recompressAppBundle.
public void recompressAppBundle(File inputFile, File outputFile) {
try (ZipReader zipReader = ZipReader.createFromFile(inputFile.toPath());
ZipArchive newBundle = new ZipArchive(outputFile);
TempDirectory tempDirectory = new TempDirectory(getClass().getSimpleName())) {
ZipEntrySourceFactory sourceFactory = new ZipEntrySourceFactory(zipReader, tempDirectory);
List<ListenableFuture<ZipEntrySource>> sources = new ArrayList<>();
BundleConfig bundleConfig = extractBundleConfig(zipReader);
ImmutableSet<String> uncompressedAssetsModules = extractModulesWithUncompressedAssets(zipReader, bundleConfig);
CompressionManager compressionManager = new CompressionManager(bundleConfig, uncompressedAssetsModules);
for (Entry entry : zipReader.getEntries().values()) {
CompressionLevel compressionLevel = compressionManager.getCompressionLevel(entry);
// parallelization there either.
if (compressionLevel.equals(SAME_AS_SOURCE) || compressionLevel.equals(NO_COMPRESSION) || entry.getUncompressedSize() < LARGE_ENTRY_SIZE_THRESHOLD_BYTES) {
sources.add(immediateFuture(sourceFactory.create(entry, compressionLevel)));
} else {
sources.add(executor.submit(() -> sourceFactory.create(entry, compressionLevel)));
}
}
// as they're ready.
for (ListenableFuture<ZipEntrySource> sourceFuture : Futures.inCompletionOrder(sources)) {
ZipEntrySource source = Futures.getUnchecked(sourceFuture);
if (source.getCompressionLevel().isCompressed() && source.getCompressedSize() >= source.getUncompressedSize()) {
// No benefit in compressing, leave the file uncompressed.
newBundle.add(sourceFactory.create(source.getEntry(), NO_COMPRESSION));
} else {
newBundle.add(source);
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
Aggregations