use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.
the class LanguageResourcesSplitterTest method removingResourceTableFileEntry_removesReferencedFilesFromSplit.
@Test
public void removingResourceTableFileEntry_removesReferencedFilesFromSplit() throws Exception {
ResourceTable resourceTable = new ResourceTableBuilder().addPackage("com.test.app").addFileResourceForMultipleConfigs("drawable", "image", ImmutableMap.of(HDPI, "res/drawable-hdpi/image.jpg", mergeConfigs(HDPI, locale("fr")), "res/drawable-fr-hdpi/image.jpg")).build();
BundleModule testModule = new BundleModuleBuilder("testModule").addFile("res/drawable-hdpi/image.jpg").addFile("res/drawable-fr-hdpi/image.jpg").setResourceTable(resourceTable).setManifest(androidManifest("com.test.app")).build();
ModuleSplit baseSplit = ModuleSplit.forModule(testModule);
Collection<ModuleSplit> languageSplits = languageSplitter.split(baseSplit);
assertThat(languageSplits).hasSize(2);
assertThat(languageSplits.stream().map(ModuleSplit::getSplitType).distinct().collect(toImmutableSet())).containsExactly(SplitType.SPLIT);
boolean hasFrSplit = false;
boolean hasDefaultSplit = false;
for (ModuleSplit languageSplit : languageSplits) {
if (languageSplit.getApkTargeting().equals(ApkTargeting.getDefaultInstance())) {
assertThat(extractPaths(languageSplit.getEntries())).containsExactly("res/drawable-hdpi/image.jpg");
hasDefaultSplit = true;
} else if (languageSplit.getApkTargeting().equals(apkLanguageTargeting("fr"))) {
assertThat(extractPaths(languageSplit.getEntries())).containsExactly("res/drawable-fr-hdpi/image.jpg");
hasFrSplit = true;
} else {
fail(String.format("Unexpected split targeting: %s", languageSplit.getApkTargeting()));
}
}
assertThat(hasDefaultSplit).isTrue();
assertThat(hasFrSplit).isTrue();
}
use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.
the class LanguageResourcesSplitterTest method noEmptySplits_whenNoResourcesLeft.
@Test
public void noEmptySplits_whenNoResourcesLeft() throws Exception {
ResourceTable resourceTable = new ResourceTableBuilder().addPackage("com.test.app").addFileResourceForMultipleConfigs("drawable", "image", ImmutableMap.of(mergeConfigs(HDPI, locale("en-GB")), "res/drawable-en-hdpi/image.jpg", mergeConfigs(HDPI, locale("fr")), "res/drawable-fr-hdpi/image.jpg")).build();
BundleModule testModule = new BundleModuleBuilder("testModule").addFile("res/drawable-en-hdpi/image.jpg").addFile("res/drawable-fr-hdpi/image.jpg").setResourceTable(resourceTable).setManifest(androidManifest("com.test.app")).build();
ModuleSplit resourcesSplit = ModuleSplit.forModule(testModule);
Collection<ModuleSplit> languageSplits = languageSplitter.split(resourcesSplit);
assertThat(languageSplits).hasSize(2);
assertThat(languageSplits.stream().map(ModuleSplit::getSplitType).distinct().collect(toImmutableSet())).containsExactly(SplitType.SPLIT);
Map<ApkTargeting, ModuleSplit> targetingMap = Maps.uniqueIndex(languageSplits, ModuleSplit::getApkTargeting);
ApkTargeting enTargeting = apkLanguageTargeting("en");
assertThat(targetingMap).containsKey(enTargeting);
ModuleSplit enSplit = targetingMap.get(enTargeting);
assertThat(extractPaths(enSplit.getEntries())).containsExactly("res/drawable-en-hdpi/image.jpg");
ApkTargeting frTargeting = apkLanguageTargeting("fr");
assertThat(targetingMap).containsKey(frTargeting);
ModuleSplit frSplit = targetingMap.get(frTargeting);
assertThat(extractPaths(frSplit.getEntries())).containsExactly("res/drawable-fr-hdpi/image.jpg");
}
use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.
the class BuildApksManagerTest method explicitMdpiPreferredOverDefault_enabledSince_0_9_1.
@Test
@Theory
public void explicitMdpiPreferredOverDefault_enabledSince_0_9_1(@FromDataPoints("bundleFeatureEnabled") boolean bundleFeatureEnabled) throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.addFile("res/drawable/image.jpg").addFile("res/drawable-mdpi/image.jpg").setManifest(androidManifest("com.test.app")).setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addDrawableResourceForMultipleDensities("image", ImmutableMap.of(/* default */
0, "res/drawable/image.jpg", /* mdpi */
160, "res/drawable-mdpi/image.jpg")).build())).setBundleConfig(BundleConfigBuilder.create().setVersion(bundleFeatureEnabled ? "0.9.1" : "0.9.0").build()).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
ImmutableList<Variant> splitApkVariants = splitApkVariants(result);
ImmutableList<ApkDescription> mdpiSplits = apkDescriptions(splitApkVariants).stream().filter(apkDesc -> {
List<ScreenDensity> targetDensities = apkDesc.getTargeting().getScreenDensityTargeting().getValueList();
return targetDensities.stream().anyMatch(density -> density.getDensityAlias() == DensityAlias.MDPI) || targetDensities.stream().anyMatch(density -> density.getDensityDpi() == MDPI_VALUE);
}).collect(toImmutableList());
String fileToBePresent;
String fileToBeAbsent;
if (bundleFeatureEnabled) {
fileToBePresent = "res/drawable-mdpi/image.jpg";
fileToBeAbsent = "res/drawable/image.jpg";
} else {
fileToBeAbsent = "res/drawable-mdpi/image.jpg";
fileToBePresent = "res/drawable/image.jpg";
}
assertThat(mdpiSplits).isNotEmpty();
for (ApkDescription mdpiSplit : mdpiSplits) {
assertThat(filesInApk(mdpiSplit, apkSetFile)).contains(fileToBePresent);
assertThat(filesInApk(mdpiSplit, apkSetFile)).doesNotContain(fileToBeAbsent);
}
}
use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.
the class BuildApksManagerTest method buildApksCommand_universal_generatesSingleApkWithNoOptimizations.
@Test
public void buildApksCommand_universal_generatesSingleApkWithNoOptimizations() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.addFile("lib/x86/libsome.so").addFile("lib/x86_64/libsome.so").setNativeConfig(nativeLibraries(targetedNativeDirectory("lib/x86", nativeDirectoryTargeting(X86)), targetedNativeDirectory("lib/x86_64", nativeDirectoryTargeting(X86_64)))).addFile("res/drawable-ldpi/image.jpg").addFile("res/drawable-mdpi/image.jpg").setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addFileResourceForMultipleConfigs("drawable", "image", ImmutableMap.of(LDPI, "res/drawable-ldpi/image.jpg", MDPI, "res/drawable-mdpi/image.jpg")).build()).setManifest(androidManifest("com.test.app"))).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).withApkBuildMode(UNIVERSAL).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
// Should not shard by any dimension and generate single APK with default targeting.
assertThat(result.getVariantList()).hasSize(1);
assertThat(splitApkVariants(result)).isEmpty();
assertThat(standaloneApkVariants(result)).hasSize(1);
Variant universalVariant = standaloneApkVariants(result).get(0);
assertThat(universalVariant.getTargeting()).isEqualTo(UNRESTRICTED_VARIANT_TARGETING);
assertThat(apkDescriptions(universalVariant)).hasSize(1);
ApkDescription universalApk = apkDescriptions(universalVariant).get(0);
assertThat(universalApk.getTargeting()).isEqualToDefaultInstance();
// No ABI or density sharding.
File universalApkFile = extractFromApkSetFile(apkSetFile, universalApk.getPath(), outputDir);
try (ZipFile universalApkZipFile = new ZipFile(universalApkFile)) {
assertThat(filesUnderPath(universalApkZipFile, ZipPath.create("lib"))).containsExactly("lib/x86/libsome.so", "lib/x86_64/libsome.so");
// "res/xml/splits0.xml" is created by bundletool with list of generated splits.
assertThat(filesUnderPath(universalApkZipFile, ZipPath.create("res"))).containsExactly("res/drawable-ldpi/image.jpg", "res/drawable-mdpi/image.jpg", "res/xml/splits0.xml");
}
}
use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.
the class BuildApksManagerTest method buildApksCommand_system_uncompressedOptions.
@Test
public void buildApksCommand_system_uncompressedOptions() throws Exception {
byte[] data = TestData.readBytes("testdata/dex/classes.dex");
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.addFile("dex/classes.dex", data).addFile("lib/x86/libsome.so", data).addFile("res/raw/some.bin", data).setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addFileResource("raw", "some", "res/raw/some.bin").build()).setNativeConfig(nativeLibraries(targetedNativeDirectory("lib/x86", nativeDirectoryTargeting(X86)))).setManifest(androidManifest("com.test.app"))).setBundleConfig(BundleConfigBuilder.create().addSplitDimension(Value.ABI).setUncompressDexFiles(true).setUncompressNativeLibraries(true).build()).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).withApkBuildMode(SYSTEM).withDeviceSpec(mergeSpecs(sdkVersion(28), abis("x86"), density(DensityAlias.MDPI), locales("en-US"))).withCustomBuildApksCommandSetter(builder -> builder.setSystemApkOptions(ImmutableSet.of(SystemApkOption.UNCOMPRESSED_DEX_FILES, SystemApkOption.UNCOMPRESSED_NATIVE_LIBRARIES))).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
assertThat(result.getVariantList()).hasSize(1);
assertThat(systemApkVariants(result)).hasSize(1);
Variant systemVariant = systemApkVariants(result).get(0);
try (ZipFile systemApkFile = new ZipFile(extractFromApkSetFile(apkSetFile, systemVariant.getApkSet(0).getApkDescriptionList().get(0).getPath(), outputDir))) {
assertThat(systemApkFile).hasFile("lib/x86/libsome.so").thatIsUncompressed();
assertThat(systemApkFile).hasFile("classes.dex").thatIsUncompressed();
assertThat(systemApkFile).hasFile("res/raw/some.bin").thatIsCompressed();
assertThat(systemApkFile).hasFile("AndroidManifest.xml").thatIsCompressed();
}
}
Aggregations