Search in sources :

Example 16 with ResourceTableBuilder

use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.

the class SystemApksGeneratorTest method shardByAbiAndDensity_havingOneAbiAndSomeDensityResourceWithDeviceSpec_producesSingleApk.

@Test
public void shardByAbiAndDensity_havingOneAbiAndSomeDensityResourceWithDeviceSpec_producesSingleApk() throws Exception {
    BundleModule bundleModule = new BundleModuleBuilder("base").addFile("assets/file.txt").addFile("dex/classes.dex").addFile("lib/x86/libtest.so").addFile("res/drawable-ldpi/image.jpg").addFile("res/drawable-hdpi/image.jpg").addFile("root/license.dat").setManifest(androidManifest("com.test.app")).setNativeConfig(nativeLibraries(targetedNativeDirectory("lib/x86", nativeDirectoryTargeting(X86)))).setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addDrawableResourceForMultipleDensities("image", ImmutableMap.of(LDPI_VALUE, "res/drawable-ldpi/image.jpg", HDPI_VALUE, "res/drawable-hdpi/image.jpg")).build()).build();
    ImmutableList<ModuleSplit> shards = systemApksGenerator.generateSystemApks(/* modules= */
    ImmutableList.of(bundleModule), /* modulesToFuse= */
    ImmutableSet.of(BASE_MODULE_NAME), splitOptimizations(OptimizationDimension.ABI, OptimizationDimension.SCREEN_DENSITY));
    assertThat(shards).hasSize(1);
    // 1 shards: {x86} x {MDPI}.
    ModuleSplit fatShard = shards.get(0);
    assertThat(fatShard.getApkTargeting().getAbiTargeting()).isEqualTo(abiTargeting(X86));
    assertThat(fatShard.getApkTargeting().getScreenDensityTargeting().getValueList()).containsExactly(toScreenDensity(DensityAlias.MDPI));
    assertThat(fatShard.getVariantTargeting().getAbiTargeting()).isEqualTo(abiTargeting(X86));
    assertThat(fatShard.getVariantTargeting().getScreenDensityTargeting().getValueList()).containsExactly(toScreenDensity(DensityAlias.MDPI));
    assertThat(fatShard.getSplitType()).isEqualTo(SplitType.SYSTEM);
    // The MDPI shard would match both hdpi and ldpi variant of the resource.
    assertThat(fatShard.getResourceTable().get()).containsResource("com.test.app:drawable/image").withConfigSize(2);
    assertThat(extractPaths(fatShard.getEntries())).containsExactly("assets/file.txt", "dex/classes.dex", "lib/x86/libtest.so", "res/drawable-hdpi/image.jpg", "res/drawable-ldpi/image.jpg", "root/license.dat");
}
Also used : BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 17 with ResourceTableBuilder

use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.

the class SystemApksGeneratorTest method shardByAbi_havingNoNativeTargeting_producesOneApk.

@Test
public void shardByAbi_havingNoNativeTargeting_producesOneApk() throws Exception {
    BundleModule bundleModule = new BundleModuleBuilder("base").addFile("assets/file.txt").addFile("dex/classes.dex").addFile("lib/x86/libtest.so").addFile("lib/x86_64/libtest.so").addFile("res/drawable/image.jpg").addFile("res/drawable-mdpi/image.jpg").addFile("root/license.dat").setManifest(androidManifest("com.test.app")).setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addDrawableResourceForMultipleDensities("image", ImmutableMap.of(DEFAULT_DENSITY_VALUE, "res/drawable/image.jpg", MDPI_VALUE, "res/drawable-mdpi/image.jpg")).build()).build();
    ImmutableList<ModuleSplit> shards = systemApksGenerator.generateSystemApks(/* modules= */
    ImmutableList.of(bundleModule), /* modulesToFuse= */
    ImmutableSet.of(BASE_MODULE_NAME), splitOptimizations(OptimizationDimension.ABI));
    assertThat(shards).hasSize(1);
    ModuleSplit fatShard = shards.get(0);
    assertThat(fatShard.getApkTargeting()).isEqualToDefaultInstance();
    assertThat(fatShard.getVariantTargeting()).isEqualTo(variantMinSdkTargeting(1));
    assertThat(fatShard.getSplitType()).isEqualTo(SplitType.SYSTEM);
    assertThat(extractPaths(fatShard.getEntries())).containsExactly("assets/file.txt", "dex/classes.dex", "lib/x86/libtest.so", "lib/x86_64/libtest.so", "res/drawable/image.jpg", "res/drawable-mdpi/image.jpg", "root/license.dat");
}
Also used : BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 18 with ResourceTableBuilder

use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.

the class SystemApksGeneratorTest method shardByAbiAndDensity_havingOneAbiAndOneDensityMultipleLanguageResourceWithDeviceSpec.

@Test
public void shardByAbiAndDensity_havingOneAbiAndOneDensityMultipleLanguageResourceWithDeviceSpec() throws Exception {
    TestComponent.useTestModule(this, TestModule.builder().withDeviceSpec(mergeSpecs(DEVICE_SPEC, locales("fr"))).build());
    BundleModule bundleModule = new BundleModuleBuilder("base").addFile("dex/classes.dex").addFile("lib/x86/libtest.so").addFile("root/license.dat").addFile("assets/languages#lang_es/image.jpg").addFile("assets/languages#lang_fr/image.jpg").setManifest(androidManifest("com.test.app")).setNativeConfig(nativeLibraries(targetedNativeDirectory("lib/x86", nativeDirectoryTargeting(X86)))).setAssetsConfig(assets(targetedAssetsDirectory("assets/languages#lang_es", assetsDirectoryTargeting(languageTargeting("es"))), targetedAssetsDirectory("assets/languages#lang_fr", assetsDirectoryTargeting(languageTargeting("fr"))))).setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addStringResourceForMultipleLocales("text", ImmutableMap.of(/* default locale */
    "", "hello", "es", "hola", "fr", "bonjour")).build()).build();
    ImmutableList<ModuleSplit> shards = systemApksGenerator.generateSystemApks(/* modules= */
    ImmutableList.of(bundleModule), /* modulesToFuse= */
    ImmutableSet.of(BASE_MODULE_NAME), splitOptimizations(OptimizationDimension.ABI, OptimizationDimension.SCREEN_DENSITY, OptimizationDimension.LANGUAGE));
    // shard {x86} x {MDPI} x {fr}
    ModuleSplit fatShard = getSystemImageSplit(shards);
    assertThat(fatShard.getApkTargeting()).isEqualTo(mergeApkTargeting(apkAbiTargeting(X86), apkLanguageTargeting("fr")));
    assertThat(fatShard.getVariantTargeting()).isEqualTo(mergeVariantTargeting(variantAbiTargeting(X86), variantMinSdkTargeting(1)));
    assertThat(fatShard.getSplitType()).isEqualTo(SplitType.SYSTEM);
    // es strings missing from resource table.
    assertThat(fatShard.getResourceTable().get()).isEqualTo(new ResourceTableBuilder().addPackage("com.test.app").addStringResourceForMultipleLocales("text", ImmutableMap.of(/* default locale */
    "", "hello", "fr", "bonjour")).build());
    assertThat(extractPaths(fatShard.getEntries())).containsExactly("dex/classes.dex", "lib/x86/libtest.so", "root/license.dat", "assets/languages#lang_fr/image.jpg");
    ModuleSplit esLangShard = Iterables.getOnlyElement(getAdditionalSplits(shards));
    assertThat(esLangShard.getApkTargeting()).isEqualTo(apkLanguageTargeting("es"));
    assertThat(esLangShard.getSplitType()).isEqualTo(SplitType.SYSTEM);
    assertThat(esLangShard.getResourceTable().get()).isEqualTo(new ResourceTableBuilder().addPackage("com.test.app").addStringResourceForMultipleLocales("text", ImmutableMap.of("es", "hola")).build());
    assertThat(extractPaths(esLangShard.getEntries())).containsExactly("assets/languages#lang_es/image.jpg");
}
Also used : BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 19 with ResourceTableBuilder

use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.

the class ModuleSplitTest method forArchive_copiesResourceTable.

@Test
public void forArchive_copiesResourceTable() throws Exception {
    BundleModule module = new BundleModuleBuilder("testModule").setManifest(androidManifest("com.test.app")).addFile("res/drawable/icon.jpg", DUMMY_CONTENT).build();
    AndroidManifest archivedManifest = AndroidManifest.create(androidManifest("com.test.app", ManifestProtoUtils.withVersionCode(123)));
    ResourceTable archivedResourceTable = new ResourceTableBuilder().addPackage("com.test.app").addDrawableResource("icon", "res/drawable/icon.jpg").build();
    Path archivedClassesDexFile = createTempClassesDexFile(DUMMY_CONTENT);
    ModuleSplit split = ModuleSplit.forArchive(module, archivedManifest, Optional.of(archivedResourceTable), archivedClassesDexFile);
    assertThat(split.getResourceTable().get()).isEqualTo(archivedResourceTable);
    assertThat(extractPaths(split.getEntries())).containsExactly("res/drawable/icon.jpg", "dex/classes.dex");
}
Also used : Path(java.nio.file.Path) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) ResourceTable(com.android.aapt.Resources.ResourceTable) Test(org.junit.Test)

Example 20 with ResourceTableBuilder

use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.

the class BuildApksManagerTest method buildApksCommand_system_generatesSingleApkWithEmptyOptimizations.

@Test
public void buildApksCommand_system_generatesSingleApkWithEmptyOptimizations() 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"))).setBundleConfig(BundleConfigBuilder.create().addSplitDimension(Value.ABI, /* negate= */
    true).addSplitDimension(Value.SCREEN_DENSITY, /* negate= */
    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"))).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)).isEmpty();
    assertThat(systemApkVariants(result)).hasSize(1);
    Variant systemVariant = systemApkVariants(result).get(0);
    assertThat(systemVariant.getTargeting()).isEqualTo(UNRESTRICTED_VARIANT_TARGETING);
    assertThat(apkDescriptions(systemVariant)).hasSize(1);
    ApkDescription systemApk = apkDescriptions(systemVariant).get(0);
    assertThat(systemApk.getTargeting()).isEqualToDefaultInstance();
    File systemApkFile = extractFromApkSetFile(apkSetFile, systemApk.getPath(), outputDir);
    try (ZipFile systemApkZipFile = new ZipFile(systemApkFile)) {
        // "res/xml/splits0.xml" is created by bundletool with list of generated splits.
        TruthZip.assertThat(systemApkZipFile).containsExactlyEntries("AndroidManifest.xml", "lib/x86/libsome.so", "lib/x86_64/libsome.so", "res/drawable-ldpi/image.jpg", "res/drawable-mdpi/image.jpg", "res/xml/splits0.xml", "resources.arsc");
    }
}
Also used : Variant(com.android.bundle.Commands.Variant) AppBundle(com.android.tools.build.bundletool.model.AppBundle) ApkDescription(com.android.bundle.Commands.ApkDescription) ZipFile(java.util.zip.ZipFile) BuildApksResult(com.android.bundle.Commands.BuildApksResult) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) ApkSetUtils.parseTocFromFile(com.android.tools.build.bundletool.testing.ApkSetUtils.parseTocFromFile) CodeRelatedFile(com.android.bundle.CodeTransparencyOuterClass.CodeRelatedFile) ZipFile(java.util.zip.ZipFile) ApkSetUtils.extractTocFromApkSetFile(com.android.tools.build.bundletool.testing.ApkSetUtils.extractTocFromApkSetFile) File(java.io.File) ApkSetUtils.extractFromApkSetFile(com.android.tools.build.bundletool.testing.ApkSetUtils.extractFromApkSetFile) Test(org.junit.Test)

Aggregations

ResourceTableBuilder (com.android.tools.build.bundletool.testing.ResourceTableBuilder)77 Test (org.junit.Test)77 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)44 BundleModuleBuilder (com.android.tools.build.bundletool.testing.BundleModuleBuilder)44 BundleModule (com.android.tools.build.bundletool.model.BundleModule)40 ResourceTable (com.android.aapt.Resources.ResourceTable)35 ApkTargeting (com.android.bundle.Targeting.ApkTargeting)25 ManifestProtoUtils.androidManifest (com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest)24 Truth.assertThat (com.google.common.truth.Truth.assertThat)24 RunWith (org.junit.runner.RunWith)24 ImmutableMap (com.google.common.collect.ImmutableMap)21 ProtoTruth.assertThat (com.google.common.truth.extensions.proto.ProtoTruth.assertThat)21 ImmutableList (com.google.common.collect.ImmutableList)20 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)20 MoreCollectors.onlyElement (com.google.common.collect.MoreCollectors.onlyElement)20 DensityAlias (com.android.bundle.Targeting.ScreenDensity.DensityAlias)18 Theories (org.junit.experimental.theories.Theories)18 Configuration (com.android.aapt.ConfigurationOuterClass.Configuration)16 Truth8.assertThat (com.google.common.truth.Truth8.assertThat)16 Collection (java.util.Collection)16