use of com.android.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode.SYSTEM in project bundletool by google.
the class BuildApksManagerTest method selectsRightModules_systemApks.
@Test
@Theory
public void selectsRightModules_systemApks() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", module -> module.addFile("assets/base.txt").setManifest(androidManifest("com.app")).setResourceTable(resourceTableWithTestLabel("Test feature"))).addModule("fused", module -> module.addFile("assets/fused.txt").setManifest(androidManifestForFeature("com.app", withFusingAttribute(true), withTitle("@string/test_label", TEST_LABEL_RESOURCE_ID)))).addModule("not_fused", module -> module.addFile("assets/not_fused.txt").setManifest(androidManifestForFeature("com.app", withFusingAttribute(false), withTitle("@string/test_label", TEST_LABEL_RESOURCE_ID)))).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).withApkBuildMode(SYSTEM).withDeviceSpec(mergeSpecs(sdkVersion(28), abis("x86"), density(DensityAlias.MDPI), locales("en-US"))).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
// System APKs: Only base and modules marked for fusing must be used.
assertThat(systemApkVariants(result)).hasSize(1);
ImmutableList<ApkDescription> systemApks = apkDescriptions(systemApkVariants(result));
assertThat(systemApks).containsExactly(ApkDescription.newBuilder().setPath("system/system.apk").setTargeting(ApkTargeting.getDefaultInstance()).setSystemApkMetadata(SystemApkMetadata.newBuilder().addAllFusedModuleName(ImmutableList.of("base", "fused"))).build(), ApkDescription.newBuilder().setPath("splits/not_fused-master.apk").setTargeting(ApkTargeting.getDefaultInstance()).setSplitApkMetadata(SplitApkMetadata.newBuilder().setSplitId("not_fused").setIsMasterSplit(true)).build());
File systemApkFile = extractFromApkSetFile(apkSetFile, "system/system.apk", outputDir);
// Validate that the system APK contains appropriate files.
ZipFile systemApkZip = openZipFile(systemApkFile);
assertThat(filesUnderPath(systemApkZip, ZipPath.create("assets"))).containsExactly("assets/base.txt", "assets/fused.txt");
}
use of com.android.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode.SYSTEM 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.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode.SYSTEM in project bundletool by google.
the class BuildApksManagerTest method multipleModules_systemApks_hasCorrectAdditionalLanguageSplits.
@Test
@Theory
public void multipleModules_systemApks_hasCorrectAdditionalLanguageSplits() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", module -> module.addFile("assets/base.txt").addFile("assets/languages#lang_es/image.jpg").addFile("assets/languages#lang_fr/image.jpg").setAssetsConfig(assets(targetedAssetsDirectory("assets/languages#lang_es", assetsDirectoryTargeting(languageTargeting("es"))), targetedAssetsDirectory("assets/languages#lang_fr", assetsDirectoryTargeting(languageTargeting("fr"))))).setManifest(androidManifest("com.app")).setResourceTable(resourceTableWithTestLabel("Test feature"))).addModule("fused", module -> module.addFile("assets/fused.txt").addFile("assets/fused/languages#lang_es/image.jpg").addFile("assets/fused/languages#lang_fr/image.jpg").setAssetsConfig(assets(targetedAssetsDirectory("assets/fused/languages#lang_es", assetsDirectoryTargeting(languageTargeting("es"))), targetedAssetsDirectory("assets/fused/languages#lang_fr", assetsDirectoryTargeting(languageTargeting("fr"))))).setManifest(androidManifestForFeature("com.app", withFusingAttribute(true), withTitle("@string/test_label", TEST_LABEL_RESOURCE_ID)))).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).withApkBuildMode(SYSTEM).withDeviceSpec(mergeSpecs(sdkVersion(28), abis("x86"), density(DensityAlias.MDPI), locales("es"))).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
assertThat(systemApkVariants(result)).hasSize(1);
Variant systemVariant = result.getVariant(0);
assertThat(systemVariant.getVariantNumber()).isEqualTo(0);
assertThat(systemVariant.getApkSetList()).hasSize(1);
ApkSet baseApkSet = Iterables.getOnlyElement(systemVariant.getApkSetList());
assertThat(baseApkSet.getModuleMetadata().getName()).isEqualTo("base");
assertThat(baseApkSet.getApkDescriptionList()).hasSize(2);
assertThat(baseApkSet.getApkDescriptionList().stream().map(ApkDescription::getPath).collect(toImmutableSet())).containsExactly("system/system.apk", "splits/base-fr.apk");
baseApkSet.getApkDescriptionList().forEach(apkDescription -> assertThat(apkSetFile).hasFile(apkDescription.getPath()));
}
use of com.android.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode.SYSTEM in project bundletool by google.
the class BuildApksManagerTest method systemMode_withModulesFlag_includesDependenciesOfModules.
@Test
public void systemMode_withModulesFlag_includesDependenciesOfModules() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.setManifest(androidManifest("com.test.app"))).addModule("feature1", builder -> builder.setManifest(androidManifest("com.test.app", withOnDemandAttribute(false), withFusingAttribute(true)))).addModule("feature2", builder -> builder.setManifest(androidManifest("com.test.app", withUsesSplit("feature1"), withOnDemandAttribute(false), withFusingAttribute(true)))).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).withApkBuildMode(SYSTEM).withModules("base", "feature2").withDeviceSpec(mergeSpecs(sdkVersion(28), abis("x86"), density(DensityAlias.MDPI), locales("en-US"))).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
ImmutableList<Variant> systemVariants = systemApkVariants(result);
// feature1 is automatically included because feature2 module depends on it.
assertThat(apkDescriptions(systemVariants)).containsExactly(ApkDescription.newBuilder().setPath("system/system.apk").setTargeting(ApkTargeting.getDefaultInstance()).setSystemApkMetadata(SystemApkMetadata.newBuilder().addAllFusedModuleName(ImmutableList.of("base", "feature1", "feature2"))).build());
}
use of com.android.tools.build.bundletool.commands.BuildApksCommand.ApkBuildMode.SYSTEM in project bundletool by google.
the class BuildApksDeviceSpecTest method deviceSpec_systemApkMode_partialDeviceSpecWithAbiAndScreenDensity_succeeds.
@Test
@Theory
public void deviceSpec_systemApkMode_partialDeviceSpecWithAbiAndScreenDensity_succeeds() throws Exception {
DeviceSpec deviceSpec = mergeSpecs(abis("arm64-v8a"), density(DensityAlias.MDPI));
AppBundle appBundle = new AppBundleBuilder().addModule("base", module -> module.setManifest(androidManifest("com.app"))).build();
bundleSerializer.writeToDisk(appBundle, bundlePath);
BuildApksCommand.Builder command = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).setDeviceSpec(deviceSpec).setApkBuildMode(SYSTEM);
command.build();
}
Aggregations