Search in sources :

Example 16 with ModuleEntry

use of com.android.tools.build.bundletool.model.ModuleEntry in project bundletool by google.

the class ApkSigner method signEmbeddedApks.

/**
 * Returns a new {@link ModuleSplit} with the same entries as the one given as parameter but with
 * embedded APKs signed.
 */
@CheckReturnValue
public ModuleSplit signEmbeddedApks(ModuleSplit split) {
    ImmutableSet<ZipPath> wear1ApkPaths = ImmutableSet.copyOf(WearApkLocator.findEmbeddedWearApkPaths(split));
    ImmutableList.Builder<ModuleEntry> newEntries = ImmutableList.builder();
    for (ModuleEntry entry : split.getEntries()) {
        ZipPath pathInApk = ApkSerializerHelper.toApkEntryPath(entry.getPath());
        if (entry.getShouldSign() || wear1ApkPaths.contains(pathInApk)) {
            newEntries.add(signModuleEntry(split, entry));
        } else {
            newEntries.add(entry);
        }
    }
    return split.toBuilder().setEntries(newEntries.build()).build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipPath(com.android.tools.build.bundletool.model.ZipPath) CheckReturnValue(com.google.errorprone.annotations.CheckReturnValue)

Example 17 with ModuleEntry

use of com.android.tools.build.bundletool.model.ModuleEntry in project bundletool by google.

the class AppBundleSerializer method writeToDisk.

/**
 * Writes the App Bundle on disk at the given location.
 */
public void writeToDisk(AppBundle bundle, Path pathOnDisk) throws IOException {
    ZipBuilder zipBuilder = new ZipBuilder();
    EntryOption[] compression = allEntriesUncompressed ? new EntryOption[] { EntryOption.UNCOMPRESSED } : new EntryOption[0];
    zipBuilder.addFileWithProtoContent(ZipPath.create(BUNDLE_CONFIG_FILE_NAME), bundle.getBundleConfig(), compression);
    // APEX bundles do not have metadata files.
    if (bundle.getFeatureModules().isEmpty() || !bundle.isApex()) {
        for (Entry<ZipPath, ByteSource> metadataEntry : bundle.getBundleMetadata().getFileContentMap().entrySet()) {
            zipBuilder.addFile(METADATA_DIRECTORY.resolve(metadataEntry.getKey()), metadataEntry.getValue(), compression);
        }
    }
    for (BundleModule module : bundle.getModules().values()) {
        ZipPath moduleDir = ZipPath.create(module.getName().toString());
        for (ModuleEntry entry : module.getEntries()) {
            ZipPath entryPath = moduleDir.resolve(entry.getPath());
            zipBuilder.addFile(entryPath, entry.getContent(), compression);
        }
        // Special module files are not represented as module entries (above).
        zipBuilder.addFileWithProtoContent(moduleDir.resolve(SpecialModuleEntry.ANDROID_MANIFEST.getPath()), module.getAndroidManifest().getManifestRoot().getProto(), compression);
        module.getAssetsConfig().ifPresent(assetsConfig -> zipBuilder.addFileWithProtoContent(moduleDir.resolve(SpecialModuleEntry.ASSETS_TABLE.getPath()), assetsConfig, compression));
        module.getNativeConfig().ifPresent(nativeConfig -> zipBuilder.addFileWithProtoContent(moduleDir.resolve(SpecialModuleEntry.NATIVE_LIBS_TABLE.getPath()), nativeConfig, compression));
        module.getResourceTable().ifPresent(resourceTable -> zipBuilder.addFileWithProtoContent(moduleDir.resolve(SpecialModuleEntry.RESOURCE_TABLE.getPath()), resourceTable, compression));
        module.getApexConfig().ifPresent(apexConfig -> zipBuilder.addFileWithProtoContent(moduleDir.resolve(SpecialModuleEntry.APEX_TABLE.getPath()), apexConfig, compression));
        module.getRuntimeEnabledSdkConfig().ifPresent(runtimeEnabledSdkConfig -> zipBuilder.addFileWithProtoContent(moduleDir.resolve(SpecialModuleEntry.RUNTIME_ENABLED_SDK_CONFIG.getPath()), runtimeEnabledSdkConfig, compression));
    }
    zipBuilder.writeTo(pathOnDisk);
}
Also used : SpecialModuleEntry(com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ByteSource(com.google.common.io.ByteSource) EntryOption(com.android.tools.build.bundletool.io.ZipBuilder.EntryOption) ZipPath(com.android.tools.build.bundletool.model.ZipPath) BundleModule(com.android.tools.build.bundletool.model.BundleModule)

Example 18 with ModuleEntry

use of com.android.tools.build.bundletool.model.ModuleEntry 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 19 with ModuleEntry

use of com.android.tools.build.bundletool.model.ModuleEntry in project bundletool by google.

the class ModuleEntriesPack method mergeWith.

/**
 * Merges two packs into a single one.
 *
 * <p>Prefers to merge smaller pack into bigger one. Removes zip file of unused pack.
 */
public ModuleEntriesPack mergeWith(ModuleEntriesPack anotherPack) {
    checkArgument(Collections.disjoint(namePrefixes, anotherPack.namePrefixes), "Both packs contain the same name prefix.");
    try {
        long thisSize = Files.size(zipMap.getPath());
        long anotherSize = Files.size(anotherPack.zipMap.getPath());
        ModuleEntriesPack to = thisSize > anotherSize ? this : anotherPack;
        ModuleEntriesPack from = thisSize > anotherSize ? anotherPack : this;
        try (ZipArchive archive = new ZipArchive(to.zipMap.getPath())) {
            archive.add(ZipSource.selectAll(from.zipMap.getPath()));
        }
        Files.delete(from.zipMap.getPath());
        IdentityHashMap<ModuleEntry, String> mergedNames = Streams.concat(entryNameByModuleEntry.entrySet().stream(), anotherPack.entryNameByModuleEntry.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b, Maps::newIdentityHashMap));
        return new ModuleEntriesPack(Sets.union(to.namePrefixes, from.namePrefixes).immutableCopy(), ZipMap.from(to.zipMap.getPath()), mergedNames);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) IdentityHashMap(java.util.IdentityHashMap) Files(java.nio.file.Files) IOException(java.io.IOException) Streams(com.google.common.collect.Streams) Entry(com.android.zipflinger.Entry) ZipSource(com.android.zipflinger.ZipSource) Maps(com.google.common.collect.Maps) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) UncheckedIOException(java.io.UncheckedIOException) ZipMap(com.android.zipflinger.ZipMap) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) Map(java.util.Map) ZipArchive(com.android.zipflinger.ZipArchive) ToLongFunction(java.util.function.ToLongFunction) Collections(java.util.Collections) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipArchive(com.android.zipflinger.ZipArchive) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) IdentityHashMap(java.util.IdentityHashMap) ZipMap(com.android.zipflinger.ZipMap) Map(java.util.Map)

Example 20 with ModuleEntry

use of com.android.tools.build.bundletool.model.ModuleEntry in project bundletool by google.

the class ModuleEntriesPacker method pack.

/**
 * Packs all entries which were previously added with provided {@link Zipper} and returns {@link
 * ModuleEntriesPack} pointing to this pack.
 */
public ModuleEntriesPack pack(Zipper zipper) {
    try {
        zipper.zip(outputZip, ImmutableMap.copyOf(contentByEntryName));
        IdentityHashMap<ModuleEntry, String> copyOfEntryNameByModuleEntry = Maps.newIdentityHashMap();
        copyOfEntryNameByModuleEntry.putAll(entryNameByModuleEntry);
        return new ModuleEntriesPack(ImmutableSet.of(namePrefix), ZipMap.from(outputZip), copyOfEntryNameByModuleEntry);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Aggregations

ModuleEntry (com.android.tools.build.bundletool.model.ModuleEntry)41 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)22 ZipPath (com.android.tools.build.bundletool.model.ZipPath)18 ImmutableList (com.google.common.collect.ImmutableList)18 Test (org.junit.Test)13 BundleModule (com.android.tools.build.bundletool.model.BundleModule)12 ImmutableSet (com.google.common.collect.ImmutableSet)12 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)10 SpecialModuleEntry (com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry)8 BundleModuleBuilder (com.android.tools.build.bundletool.testing.BundleModuleBuilder)6 HashMap (java.util.HashMap)6 Optional (java.util.Optional)6 TargetedAssetsDirectory (com.android.bundle.Files.TargetedAssetsDirectory)5 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)5 IOException (java.io.IOException)5 Map (java.util.Map)5 ResourceTable (com.android.aapt.Resources.ResourceTable)4 ApkTargeting (com.android.bundle.Targeting.ApkTargeting)4 BundleModuleName (com.android.tools.build.bundletool.model.BundleModuleName)4 ApkOptimizations (com.android.tools.build.bundletool.optimizations.ApkOptimizations)4