use of com.android.zipflinger.ZipArchive in project bundletool by google.
the class ZipFlingerApkSerializer method writeToZipFile.
private void writeToZipFile(ModuleSplit split, Path outputPath, TempDirectory tempDir) throws IOException {
checkFileDoesNotExist(outputPath);
createParentDirectories(outputPath);
split = apkSigner.signEmbeddedApks(split);
// Write a Proto-APK with only files that aapt2 requires as part of the convert command.
Path partialProtoApk = tempDir.getPath().resolve("proto.apk");
writeProtoApk(split, partialProtoApk, tempDir);
// Invoke aapt2 to convert files from proto to binary format.
Path binaryApkPath = tempDir.getPath().resolve("binary.apk");
if (enableSparseEncoding && split.getResourceTable().isPresent()) {
Path interimApk = tempDir.getPath().resolve("interim.apk");
aapt2.convertApkProtoToBinary(partialProtoApk, interimApk);
aapt2.optimizeToSparseResourceTables(interimApk, binaryApkPath);
} else {
aapt2.convertApkProtoToBinary(partialProtoApk, binaryApkPath);
}
checkState(Files.exists(binaryApkPath), "No APK created by aapt2 convert command.");
try (ZipArchive apkWriter = new ZipArchive(outputPath);
ZipReader aapt2ApkReader = ZipReader.createFromFile(binaryApkPath)) {
ImmutableMap<ZipPath, ModuleEntry> moduleEntriesByName = split.getEntries().stream().collect(toImmutableMap(entry -> ApkSerializerHelper.toApkEntryPath(entry.getPath()), entry -> entry, // e.g. base/assets/foo and base/root/assets/foo.
(a, b) -> b));
// Sorting entries by name for determinism.
ImmutableSortedSet<ZipPath> sortedEntryNames = Stream.concat(aapt2ApkReader.getEntries().keySet().stream().map(ZipPath::create), moduleEntriesByName.keySet().stream()).collect(toImmutableSortedSet(naturalOrder()));
ApkEntrySerializer apkEntrySerializer = new ApkEntrySerializer(apkWriter, aapt2ApkReader, split, tempDir);
for (ZipPath pathInApk : sortedEntryNames) {
Optional<Entry> aapt2Entry = aapt2ApkReader.getEntry(pathInApk.toString());
if (aapt2Entry.isPresent()) {
apkEntrySerializer.addAapt2Entry(pathInApk, aapt2Entry.get());
} else {
ModuleEntry moduleEntry = checkNotNull(moduleEntriesByName.get(pathInApk));
apkEntrySerializer.addRegularEntry(pathInApk, moduleEntry);
}
}
}
apkSigner.signApk(outputPath, split);
}
use of com.android.zipflinger.ZipArchive in project bundletool by google.
the class ZipFlingerAppBundleSerializer method writeToDisk.
/**
* Writes the App Bundle on disk at the given location.
*/
public void writeToDisk(AppBundle bundle, Path destBundlePath) throws IOException {
try (ZipArchive zipArchive = new ZipArchive(destBundlePath)) {
zipArchive.add(protoToSource(ZipPath.create(BUNDLE_CONFIG_FILE_NAME), bundle.getBundleConfig(), DEFAULT_COMPRESSION_LEVEL));
// APEX bundles do not have metadata files.
if (bundle.getFeatureModules().isEmpty() || !bundle.isApex()) {
for (Map.Entry<ZipPath, ByteSource> metadataEntry : bundle.getBundleMetadata().getFileContentMap().entrySet()) {
zipArchive.add(new BytesSource(metadataEntry.getValue().read(), METADATA_DIRECTORY.resolve(metadataEntry.getKey()).toString(), DEFAULT_COMPRESSION_LEVEL));
}
}
addEntriesFromSourceBundles(zipArchive, getUnmodifiedModuleEntries(bundle));
addNewEntries(zipArchive, getNewOrModifiedModuleEntries(bundle));
// Special module files are not represented as module entries (above).
for (BundleModule module : bundle.getModules().values()) {
ZipPath moduleDir = ZipPath.create(module.getName().toString());
zipArchive.add(protoToSource(moduleDir.resolve(SpecialModuleEntry.ANDROID_MANIFEST.getPath()), module.getAndroidManifest().getManifestRoot().getProto(), DEFAULT_COMPRESSION_LEVEL));
if (module.getAssetsConfig().isPresent()) {
zipArchive.add(protoToSource(moduleDir.resolve(SpecialModuleEntry.ASSETS_TABLE.getPath()), module.getAssetsConfig().get(), DEFAULT_COMPRESSION_LEVEL));
}
if (module.getNativeConfig().isPresent()) {
zipArchive.add(protoToSource(moduleDir.resolve(SpecialModuleEntry.NATIVE_LIBS_TABLE.getPath()), module.getNativeConfig().get(), DEFAULT_COMPRESSION_LEVEL));
}
if (module.getResourceTable().isPresent()) {
zipArchive.add(protoToSource(moduleDir.resolve(SpecialModuleEntry.RESOURCE_TABLE.getPath()), module.getResourceTable().get(), DEFAULT_COMPRESSION_LEVEL));
}
if (module.getApexConfig().isPresent()) {
zipArchive.add(protoToSource(moduleDir.resolve(SpecialModuleEntry.APEX_TABLE.getPath()), module.getApexConfig().get(), DEFAULT_COMPRESSION_LEVEL));
}
}
}
}
Aggregations