Search in sources :

Example 6 with ZipArchive

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);
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) NO_ALIGNMENT(com.android.zipflinger.Source.NO_ALIGNMENT) Comparator.naturalOrder(java.util.Comparator.naturalOrder) Map(java.util.Map) ZipArchive(com.android.zipflinger.ZipArchive) Version(com.android.tools.build.bundletool.model.version.Version) NO_COMPRESSION_EXTENSIONS(com.android.tools.build.bundletool.io.ApkSerializerHelper.NO_COMPRESSION_EXTENSIONS) Path(java.nio.file.Path) ImmutableSortedSet.toImmutableSortedSet(com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Aapt2Command(com.android.tools.build.bundletool.androidtools.Aapt2Command) Preconditions.checkState(com.google.common.base.Preconditions.checkState) SpecialModuleEntry(com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry) UncheckedIOException(java.io.UncheckedIOException) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Stream(java.util.stream.Stream) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) BundleConfig(com.android.bundle.Config.BundleConfig) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) UseBundleCompression(com.android.tools.build.bundletool.commands.BuildApksManagerComponent.UseBundleCompression) FilePreconditions.checkFileDoesNotExist(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileDoesNotExist) Optional(java.util.Optional) SparseEncoding(com.android.bundle.Config.ResourceOptimizations.SparseEncoding) Pattern(java.util.regex.Pattern) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) SAME_AS_SOURCE(com.android.tools.build.bundletool.model.CompressionLevel.SAME_AS_SOURCE) PathMatcher(com.android.tools.build.bundletool.model.utils.PathMatcher) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ZipPath(com.android.tools.build.bundletool.model.ZipPath) BytesSource(com.android.zipflinger.BytesSource) BEST_COMPRESSION(com.android.tools.build.bundletool.model.CompressionLevel.BEST_COMPRESSION) ApkListener(com.android.tools.build.bundletool.model.ApkListener) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ApkDescription(com.android.bundle.Commands.ApkDescription) CompressionLevel(com.android.tools.build.bundletool.model.CompressionLevel) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) MANIFEST_FILENAME(com.android.tools.build.bundletool.model.BundleModule.MANIFEST_FILENAME) Files(java.nio.file.Files) DEFAULT_COMPRESSION(com.android.tools.build.bundletool.model.CompressionLevel.DEFAULT_COMPRESSION) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ApkSerializerHelper.requiresAapt2Conversion(com.android.tools.build.bundletool.io.ApkSerializerHelper.requiresAapt2Conversion) IOException(java.io.IOException) FileUtils.createParentDirectories(com.android.tools.build.bundletool.model.utils.files.FileUtils.createParentDirectories) VerboseLogs(com.android.tools.build.bundletool.commands.BuildApksModule.VerboseLogs) Entry(com.android.zipflinger.Entry) Maps(com.google.common.collect.Maps) NO_COMPRESSION(com.android.tools.build.bundletool.model.CompressionLevel.NO_COMPRESSION) NO_DEFAULT_UNCOMPRESS_EXTENSIONS(com.android.tools.build.bundletool.model.version.VersionGuardedFeature.NO_DEFAULT_UNCOMPRESS_EXTENSIONS) FileUtils(com.android.tools.build.bundletool.model.utils.files.FileUtils) SpecialModuleEntry(com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) Entry(com.android.zipflinger.Entry) SpecialModuleEntry(com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipArchive(com.android.zipflinger.ZipArchive) ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Example 7 with ZipArchive

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));
            }
        }
    }
}
Also used : BytesSource(com.android.zipflinger.BytesSource) ZipArchive(com.android.zipflinger.ZipArchive) ByteSource(com.google.common.io.ByteSource) ZipPath(com.android.tools.build.bundletool.model.ZipPath) HashMap(java.util.HashMap) Map(java.util.Map) BundleModule(com.android.tools.build.bundletool.model.BundleModule)

Aggregations

ZipArchive (com.android.zipflinger.ZipArchive)7 Entry (com.android.zipflinger.Entry)5 IOException (java.io.IOException)5 ModuleEntry (com.android.tools.build.bundletool.model.ModuleEntry)4 ZipPath (com.android.tools.build.bundletool.model.ZipPath)4 BytesSource (com.android.zipflinger.BytesSource)4 UncheckedIOException (java.io.UncheckedIOException)4 ApkDescription (com.android.bundle.Commands.ApkDescription)3 BundleConfig (com.android.bundle.Config.BundleConfig)3 SpecialModuleEntry (com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 VerboseLogs (com.android.tools.build.bundletool.commands.BuildApksModule.VerboseLogs)2 ApkSerializerHelper.requiresAapt2Conversion (com.android.tools.build.bundletool.io.ApkSerializerHelper.requiresAapt2Conversion)2 ApkListener (com.android.tools.build.bundletool.model.ApkListener)2 CompressionLevel (com.android.tools.build.bundletool.model.CompressionLevel)2 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)2 PathMatcher (com.android.tools.build.bundletool.model.utils.PathMatcher)2 FileUtils (com.android.tools.build.bundletool.model.utils.files.FileUtils)2 Version (com.android.tools.build.bundletool.model.version.Version)2 NO_DEFAULT_UNCOMPRESS_EXTENSIONS (com.android.tools.build.bundletool.model.version.VersionGuardedFeature.NO_DEFAULT_UNCOMPRESS_EXTENSIONS)2