Search in sources :

Example 86 with Variant

use of com.android.bundle.Commands.Variant in project bundletool by google.

the class ApkSerializerManager method serializeApks.

@VisibleForTesting
ImmutableList<Variant> serializeApks(Path outputDirectory, GeneratedApks generatedApks, Optional<DeviceSpec> deviceSpec) {
    validateInput(generatedApks, apkBuildMode);
    // Running with system APK mode generates a fused APK and additional unmatched language splits.
    // To avoid filtering of unmatched language splits we skip device filtering for system mode.
    Predicate<ModuleSplit> deviceFilter = deviceSpec.isPresent() && !apkBuildMode.equals(SYSTEM) ? new ApkMatcher(addDefaultDeviceTierIfNecessary(deviceSpec.get()))::matchesModuleSplitByTargeting : alwaysTrue();
    ImmutableListMultimap<VariantKey, ModuleSplit> splitsByVariant = generatedApks.getAllApksGroupedByOrderedVariants();
    // Assign the variant numbers to each variant present.
    AtomicInteger variantNumberCounter = new AtomicInteger(firstVariantNumber);
    ImmutableMap<VariantKey, Integer> variantNumberByVariantKey = splitsByVariant.keySet().stream().collect(toImmutableMap(identity(), unused -> variantNumberCounter.getAndIncrement()));
    // 1. Remove APKs not matching the device spec.
    // 2. Modify the APKs based on the ApkModifier.
    // 3. Serialize all APKs in parallel.
    // Modifies the APK using APK modifier, then returns a map by extracting the variant
    // of APK first and later clearing out its variant targeting.
    ImmutableListMultimap<VariantKey, ModuleSplit> finalSplitsByVariant = splitsByVariant.entries().stream().filter(keyModuleSplitEntry -> deviceFilter.test(keyModuleSplitEntry.getValue())).collect(groupingBySortedKeys(Entry::getKey, entry -> clearVariantTargeting(modifyApk(entry.getValue(), variantNumberByVariantKey.get(entry.getKey())))));
    // After variant targeting of APKs are cleared, there might be duplicate APKs
    // which are removed and the distinct APKs are then serialized in parallel.
    ImmutableBiMap<ZipPath, ModuleSplit> splitsByRelativePath = finalSplitsByVariant.values().stream().distinct().collect(toImmutableBiMap(apkPathManager::getApkPath, identity()));
    ImmutableMap<ZipPath, ApkDescription> apkDescriptionsByRelativePath = apkSerializer.serialize(outputDirectory, splitsByRelativePath);
    // Build the result proto.
    ImmutableList.Builder<Variant> variants = ImmutableList.builder();
    for (VariantKey variantKey : finalSplitsByVariant.keySet()) {
        Variant.Builder variant = Variant.newBuilder().setVariantNumber(variantNumberByVariantKey.get(variantKey)).setTargeting(variantKey.getVariantTargeting());
        Multimap<BundleModuleName, ModuleSplit> splitsByModuleName = finalSplitsByVariant.get(variantKey).stream().collect(groupingBySortedKeys(ModuleSplit::getModuleName));
        for (BundleModuleName moduleName : splitsByModuleName.keySet()) {
            variant.addApkSet(ApkSet.newBuilder().setModuleMetadata(bundle.getModule(moduleName).getModuleMetadata()).addAllApkDescription(splitsByModuleName.get(moduleName).stream().map(split -> splitsByRelativePath.inverse().get(split)).map(apkDescriptionsByRelativePath::get).collect(toImmutableList())));
        }
        variants.add(variant.build());
    }
    return variants.build();
}
Also used : SYSTEM(com.android.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode.SYSTEM) FirstVariantNumber(com.android.tools.build.bundletool.commands.BuildApksModule.FirstVariantNumber) Variant(com.android.bundle.Commands.Variant) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) DeviceSpec(com.android.bundle.Devices.DeviceSpec) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Predicates.alwaysTrue(com.google.common.base.Predicates.alwaysTrue) DeliveryType(com.android.bundle.Commands.DeliveryType) Bundletool(com.android.bundle.Config.Bundletool) SdkBundle(com.android.tools.build.bundletool.model.SdkBundle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssetModulesConfig(com.android.bundle.Config.AssetModulesConfig) Path(java.nio.file.Path) BundleToolVersion(com.android.tools.build.bundletool.model.version.BundleToolVersion) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ManifestDeliveryElement(com.android.tools.build.bundletool.model.ManifestDeliveryElement) ApkOptimizations(com.android.tools.build.bundletool.optimizations.ApkOptimizations) ApkType(com.android.tools.build.bundletool.model.ApkModifier.ApkDescription.ApkType) InstantMetadata(com.android.bundle.Commands.InstantMetadata) SplitType(com.android.tools.build.bundletool.model.ModuleSplit.SplitType) GeneratedApks(com.android.tools.build.bundletool.model.GeneratedApks) UncheckedIOException(java.io.UncheckedIOException) DefaultTargetingValue(com.android.bundle.Commands.DefaultTargetingValue) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ApkSet(com.android.bundle.Commands.ApkSet) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) BundleConfig(com.android.bundle.Config.BundleConfig) PermanentlyFusedModule(com.android.bundle.Commands.PermanentlyFusedModule) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Function.identity(java.util.function.Function.identity) Entry(java.util.Map.Entry) Optional(java.util.Optional) Bundle(com.android.tools.build.bundletool.model.Bundle) BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) ZipPath(com.android.tools.build.bundletool.model.ZipPath) OptimizationDimension(com.android.tools.build.bundletool.model.OptimizationDimension) VariantKey(com.android.tools.build.bundletool.model.VariantKey) CollectorUtils.groupingBySortedKeys(com.android.tools.build.bundletool.model.utils.CollectorUtils.groupingBySortedKeys) ApkModifier(com.android.tools.build.bundletool.model.ApkModifier) BuildApksResult(com.android.bundle.Commands.BuildApksResult) Multimap(com.google.common.collect.Multimap) CollectorUtils.groupingByDeterministic(com.android.tools.build.bundletool.model.utils.CollectorUtils.groupingByDeterministic) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) Collectors.mapping(java.util.stream.Collectors.mapping) SdkVersionInformation(com.android.bundle.Commands.SdkVersionInformation) ApkDescription(com.android.bundle.Commands.ApkDescription) BuildSdkApksResult(com.android.bundle.Commands.BuildSdkApksResult) ApkMatcher(com.android.tools.build.bundletool.device.ApkMatcher) VariantTargeting(com.android.bundle.Targeting.VariantTargeting) SuffixStripping(com.android.bundle.Config.SuffixStripping) Int32Value(com.google.protobuf.Int32Value) AssetSliceSet(com.android.bundle.Commands.AssetSliceSet) IOException(java.io.IOException) AssetModuleMetadata(com.android.bundle.Commands.AssetModuleMetadata) VerboseLogs(com.android.tools.build.bundletool.commands.BuildApksModule.VerboseLogs) AssetModulesInfo(com.android.bundle.Commands.AssetModulesInfo) LocalTestingInfo(com.android.bundle.Commands.LocalTestingInfo) ApkBuildMode(com.android.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode) GeneratedAssetSlices(com.android.tools.build.bundletool.model.GeneratedAssetSlices) SplitDimension(com.android.bundle.Config.SplitDimension) ImmutableBiMap.toImmutableBiMap(com.google.common.collect.ImmutableBiMap.toImmutableBiMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BundleModule(com.android.tools.build.bundletool.model.BundleModule) VariantKey(com.android.tools.build.bundletool.model.VariantKey) ApkDescription(com.android.bundle.Commands.ApkDescription) BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ZipPath(com.android.tools.build.bundletool.model.ZipPath) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Variant(com.android.bundle.Commands.Variant) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApkMatcher(com.android.tools.build.bundletool.device.ApkMatcher) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 87 with Variant

use of com.android.bundle.Commands.Variant in project bundletool by google.

the class ApkSizeUtilsTest method multipleVariants_selectOneVariant.

@Test
public void multipleVariants_selectOneVariant() throws Exception {
    ZipPath apkOne = ZipPath.create("apk_one.apk");
    ZipPath apkTwo = ZipPath.create("apk_two.apk");
    Variant lVariant = createVariantForSingleSplitApk(variantSdkTargeting(sdkVersionFrom(21), ImmutableSet.of(sdkVersionFrom(23))), ApkTargeting.getDefaultInstance(), apkOne);
    Variant mVariant = createVariantForSingleSplitApk(variantSdkTargeting(sdkVersionFrom(23), ImmutableSet.of(sdkVersionFrom(21))), ApkTargeting.getDefaultInstance(), apkTwo);
    ImmutableList<Variant> variants = ImmutableList.of(lVariant, mVariant);
    Path apksArchiveFile = createApksArchiveFile(BuildApksResult.newBuilder().addAllVariant(variants).build(), tmpDir.resolve("bundle.apks"));
    ImmutableMap<String, Long> sizeByApkPaths = getVariantCompressedSizeByApkPaths(ImmutableList.of(lVariant), apksArchiveFile);
    assertThat(sizeByApkPaths.keySet()).containsExactly("apk_one.apk");
    assertThat(sizeByApkPaths.get("apk_one.apk")).isAtLeast(1L);
}
Also used : ApksArchiveHelpers.createVariant(com.android.tools.build.bundletool.testing.ApksArchiveHelpers.createVariant) Variant(com.android.bundle.Commands.Variant) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Test(org.junit.Test)

Example 88 with Variant

use of com.android.bundle.Commands.Variant in project bundletool by google.

the class ApkSizeUtilsTest method oneLVariant_multipleModules.

@Test
public void oneLVariant_multipleModules() throws Exception {
    ZipPath baseApk = ZipPath.create("base.apk");
    ZipPath baseX86Apk = ZipPath.create("base-x86.apk");
    ZipPath featureApk = ZipPath.create("feature.apk");
    ZipPath featureX86Apk = ZipPath.create("feature-x86.apk");
    ImmutableList<Variant> variants = ImmutableList.of(createVariant(variantSdkTargeting(sdkVersionFrom(21)), createSplitApkSet("base", createMasterApkDescription(ApkTargeting.getDefaultInstance(), baseApk), createApkDescription(apkAbiTargeting(X86), baseX86Apk, false)), createSplitApkSet("feature", createMasterApkDescription(ApkTargeting.getDefaultInstance(), featureApk), createApkDescription(apkAbiTargeting(X86), featureX86Apk, false))));
    Path apksArchiveFile = createApksArchiveFile(BuildApksResult.newBuilder().addAllVariant(variants).build(), tmpDir.resolve("bundle.apks"));
    ImmutableMap<String, Long> sizeByApkPaths = getVariantCompressedSizeByApkPaths(variants, apksArchiveFile);
    assertThat(sizeByApkPaths.keySet()).containsExactly("base.apk", "base-x86.apk", "feature.apk", "feature-x86.apk");
    assertThat(sizeByApkPaths.get("base.apk")).isAtLeast(1L);
    assertThat(sizeByApkPaths.get("base-x86.apk")).isAtLeast(1L);
    assertThat(sizeByApkPaths.get("feature.apk")).isAtLeast(1L);
    assertThat(sizeByApkPaths.get("feature-x86.apk")).isAtLeast(1L);
}
Also used : ApksArchiveHelpers.createVariant(com.android.tools.build.bundletool.testing.ApksArchiveHelpers.createVariant) Variant(com.android.bundle.Commands.Variant) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Test(org.junit.Test)

Example 89 with Variant

use of com.android.bundle.Commands.Variant in project bundletool by google.

the class ApkSizeUtilsTest method multipleVariants.

@Test
public void multipleVariants() throws Exception {
    ZipPath apkOne = ZipPath.create("apk_one.apk");
    ZipPath apkTwo = ZipPath.create("apk_two.apk");
    ImmutableList<Variant> variants = ImmutableList.of(createVariantForSingleSplitApk(variantSdkTargeting(sdkVersionFrom(21), ImmutableSet.of(sdkVersionFrom(23))), ApkTargeting.getDefaultInstance(), apkOne), createVariantForSingleSplitApk(variantSdkTargeting(sdkVersionFrom(23), ImmutableSet.of(sdkVersionFrom(21))), ApkTargeting.getDefaultInstance(), apkTwo));
    Path apksArchiveFile = createApksArchiveFile(BuildApksResult.newBuilder().addAllVariant(variants).build(), tmpDir.resolve("bundle.apks"));
    ImmutableMap<String, Long> sizeByApkPaths = getVariantCompressedSizeByApkPaths(variants, apksArchiveFile);
    assertThat(sizeByApkPaths.keySet()).containsExactly("apk_one.apk", "apk_two.apk");
    long apkOneSize = sizeByApkPaths.get("apk_one.apk");
    long apkTwoSize = sizeByApkPaths.get("apk_two.apk");
    assertThat(apkOneSize).isAtLeast(1L);
    assertThat(apkTwoSize).isAtLeast(1L);
}
Also used : ApksArchiveHelpers.createVariant(com.android.tools.build.bundletool.testing.ApksArchiveHelpers.createVariant) Variant(com.android.bundle.Commands.Variant) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) Test(org.junit.Test)

Example 90 with Variant

use of com.android.bundle.Commands.Variant in project bundletool by google.

the class ResultUtilsTest method filterInstantApkVariant.

@Test
public void filterInstantApkVariant() throws Exception {
    Variant standaloneVariant = createStandaloneVariant();
    Variant splitVariant = createSplitVariant();
    Variant instantVariant = createInstantVariant();
    BuildApksResult apksResult = BuildApksResult.newBuilder().addVariant(standaloneVariant).addVariant(splitVariant).addVariant(instantVariant).build();
    assertThat(ResultUtils.instantApkVariants(apksResult)).containsExactly(instantVariant);
}
Also used : ApksArchiveHelpers.createVariant(com.android.tools.build.bundletool.testing.ApksArchiveHelpers.createVariant) Variant(com.android.bundle.Commands.Variant) BuildApksResult(com.android.bundle.Commands.BuildApksResult) Test(org.junit.Test)

Aggregations

Variant (com.android.bundle.Commands.Variant)134 Test (org.junit.Test)130 BuildApksResult (com.android.bundle.Commands.BuildApksResult)100 ZipPath (com.android.tools.build.bundletool.model.ZipPath)82 Path (java.nio.file.Path)80 ZipFile (java.util.zip.ZipFile)79 AppBundle (com.android.tools.build.bundletool.model.AppBundle)66 AppBundleBuilder (com.android.tools.build.bundletool.testing.AppBundleBuilder)66 ApkSet (com.android.bundle.Commands.ApkSet)63 ApkDescription (com.android.bundle.Commands.ApkDescription)60 ApkSetUtils.extractFromApkSetFile (com.android.tools.build.bundletool.testing.ApkSetUtils.extractFromApkSetFile)59 File (java.io.File)59 AndroidManifest (com.android.tools.build.bundletool.model.AndroidManifest)56 AssetSliceSet (com.android.bundle.Commands.AssetSliceSet)55 ApkSetUtils.extractTocFromApkSetFile (com.android.tools.build.bundletool.testing.ApkSetUtils.extractTocFromApkSetFile)55 CodeRelatedFile (com.android.bundle.CodeTransparencyOuterClass.CodeRelatedFile)54 ApkSetUtils.parseTocFromFile (com.android.tools.build.bundletool.testing.ApkSetUtils.parseTocFromFile)54 ResourceTableBuilder (com.android.tools.build.bundletool.testing.ResourceTableBuilder)54 ImmutableSet (com.google.common.collect.ImmutableSet)54 ApkVerifier (com.android.apksig.ApkVerifier)53