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();
}
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);
}
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);
}
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);
}
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);
}
Aggregations