Search in sources :

Example 1 with ZipSource

use of com.android.zipflinger.ZipSource 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);
    }
}
Also used : PathMatcher(com.android.tools.build.bundletool.model.utils.PathMatcher) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ZipPath(com.android.tools.build.bundletool.model.ZipPath) ApkListener(com.android.tools.build.bundletool.model.ApkListener) Inject(javax.inject.Inject) ApkSerializerHelper.toApkEntryPath(com.android.tools.build.bundletool.io.ApkSerializerHelper.toApkEntryPath) ImmutableList(com.google.common.collect.ImmutableList) ApkDescription(com.android.bundle.Commands.ApkDescription) ZipArchive(com.android.zipflinger.ZipArchive) Version(com.android.tools.build.bundletool.model.version.Version) ByteSource(com.google.common.io.ByteSource) Path(java.nio.file.Path) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) ApkSerializerHelper.requiresAapt2Conversion(com.android.tools.build.bundletool.io.ApkSerializerHelper.requiresAapt2Conversion) IOException(java.io.IOException) Deflater(java.util.zip.Deflater) VerboseLogs(com.android.tools.build.bundletool.commands.BuildApksModule.VerboseLogs) Streams(com.google.common.collect.Streams) Entry(com.android.zipflinger.Entry) ZipSource(com.android.zipflinger.ZipSource) SpecialModuleEntry(com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry) UncheckedIOException(java.io.UncheckedIOException) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) BundleConfig(com.android.bundle.Config.BundleConfig) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) NO_DEFAULT_UNCOMPRESS_EXTENSIONS(com.android.tools.build.bundletool.model.version.VersionGuardedFeature.NO_DEFAULT_UNCOMPRESS_EXTENSIONS) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) FileUtils(com.android.tools.build.bundletool.model.utils.files.FileUtils) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ZipSource(com.android.zipflinger.ZipSource) SpecialModuleEntry(com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipArchive(com.android.zipflinger.ZipArchive) UncheckedIOException(java.io.UncheckedIOException) ZipPath(com.android.tools.build.bundletool.model.ZipPath) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 2 with ZipSource

use of com.android.zipflinger.ZipSource in project bundletool by google.

the class ModuleEntriesPack method select.

/**
 * Selects module entries as a {@link ZipSource} which next can be added into a new {@link
 * ZipArchive}.
 *
 * <p>Requires to provide {@code nameFunction} which assigns final names for each {@link
 * ModuleEntry} and {@code alignmentFunction} which assigns alignment in the final {@link
 * ZipSource}.
 */
ZipSource select(ImmutableList<ModuleEntry> moduleEntries, Function<ModuleEntry, String> nameFunction, ToLongFunction<ModuleEntry> alignmentFunction) {
    ZipSource source = new ZipSource(zipMap);
    for (ModuleEntry entry : moduleEntries) {
        checkArgument(entryNameByModuleEntry.containsKey(entry), "Module entry %s is not available in the pack.", entry);
        source.select(entryNameByModuleEntry.get(entry), nameFunction.apply(entry), ZipSource.COMPRESSION_NO_CHANGE, alignmentFunction.applyAsLong(entry));
    }
    return source;
}
Also used : ZipSource(com.android.zipflinger.ZipSource) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry)

Example 3 with ZipSource

use of com.android.zipflinger.ZipSource in project bundletool by google.

the class ZipFlingerAppBundleSerializer method addEntriesFromSourceBundles.

/**
 * Adds umodified entries to an archive, sourcing them from their original on-disk location.
 */
private static void addEntriesFromSourceBundles(ZipArchive archive, ImmutableListMultimap<BundleModule, ModuleEntry> entries) throws IOException {
    Map<Path, ZipSource> bundleSources = new HashMap<>();
    for (Map.Entry<BundleModule, ModuleEntry> moduleAndEntry : entries.entries()) {
        BundleModule module = moduleAndEntry.getKey();
        ModuleEntry moduleEntry = moduleAndEntry.getValue();
        ModuleEntryBundleLocation location = moduleEntry.getBundleLocation().orElseThrow(IllegalStateException::new);
        ZipPath entryFullPathInSourceBundle = location.entryPathInBundle();
        ZipPath moduleDir = ZipPath.create(module.getName().toString());
        ZipPath entryFullPathInDestBundle = moduleDir.resolve(moduleEntry.getPath());
        Path pathToBundle = location.pathToBundle();
        // We cannot use computeIfAbstent because new ZipSource may throw.
        ZipSource entrySource = bundleSources.containsKey(pathToBundle) ? bundleSources.get(pathToBundle) : new ZipSource(pathToBundle);
        bundleSources.putIfAbsent(pathToBundle, entrySource);
        entrySource.select(entryFullPathInSourceBundle.toString(), /* newName= */
        entryFullPathInDestBundle.toString());
    }
    for (ZipSource source : bundleSources.values()) {
        archive.add(source);
    }
}
Also used : ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) ZipSource(com.android.zipflinger.ZipSource) HashMap(java.util.HashMap) SpecialModuleEntry(com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipPath(com.android.tools.build.bundletool.model.ZipPath) ModuleEntryBundleLocation(com.android.tools.build.bundletool.model.ModuleEntry.ModuleEntryBundleLocation) HashMap(java.util.HashMap) Map(java.util.Map) BundleModule(com.android.tools.build.bundletool.model.BundleModule)

Aggregations

ModuleEntry (com.android.tools.build.bundletool.model.ModuleEntry)3 ZipSource (com.android.zipflinger.ZipSource)3 SpecialModuleEntry (com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry)2 ZipPath (com.android.tools.build.bundletool.model.ZipPath)2 Path (java.nio.file.Path)2 ApkDescription (com.android.bundle.Commands.ApkDescription)1 BundleConfig (com.android.bundle.Config.BundleConfig)1 VerboseLogs (com.android.tools.build.bundletool.commands.BuildApksModule.VerboseLogs)1 ApkSerializerHelper.requiresAapt2Conversion (com.android.tools.build.bundletool.io.ApkSerializerHelper.requiresAapt2Conversion)1 ApkSerializerHelper.toApkEntryPath (com.android.tools.build.bundletool.io.ApkSerializerHelper.toApkEntryPath)1 ApkListener (com.android.tools.build.bundletool.model.ApkListener)1 BundleModule (com.android.tools.build.bundletool.model.BundleModule)1 ModuleEntryBundleLocation (com.android.tools.build.bundletool.model.ModuleEntry.ModuleEntryBundleLocation)1 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)1 PathMatcher (com.android.tools.build.bundletool.model.utils.PathMatcher)1 FileUtils (com.android.tools.build.bundletool.model.utils.files.FileUtils)1 Version (com.android.tools.build.bundletool.model.version.Version)1 NO_DEFAULT_UNCOMPRESS_EXTENSIONS (com.android.tools.build.bundletool.model.version.VersionGuardedFeature.NO_DEFAULT_UNCOMPRESS_EXTENSIONS)1 Entry (com.android.zipflinger.Entry)1 ZipArchive (com.android.zipflinger.ZipArchive)1