Search in sources :

Example 1 with DensityAlias

use of com.android.bundle.Targeting.ScreenDensity.DensityAlias in project bundletool by google.

the class StandaloneApksGeneratorTest method manyModulesShardByAbiAndDensity_havingManyAbisAndSomeResource_producesManyApks.

@Test
public void manyModulesShardByAbiAndDensity_havingManyAbisAndSomeResource_producesManyApks() throws Exception {
    BundleModule baseModule = new BundleModuleBuilder("base").addFile("assets/file.txt").addFile("dex/classes.dex").addFile("res/drawable/image.jpg").addFile("res/drawable-hdpi/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", HDPI_VALUE, "res/drawable-hdpi/image.jpg")).build()).build();
    BundleModule featureModule = new BundleModuleBuilder("feature").addFile("lib/armeabi/libtest.so").addFile("lib/x86/libtest.so").setManifest(androidManifest("com.test.app")).setNativeConfig(nativeLibraries(targetedNativeDirectory("lib/armeabi", nativeDirectoryTargeting(ARMEABI)), targetedNativeDirectory("lib/x86", nativeDirectoryTargeting(X86)))).build();
    ImmutableList<ModuleSplit> shards = standaloneApksGenerator.generateStandaloneApks(ImmutableList.of(baseModule, featureModule), standaloneApkOptimizations(OptimizationDimension.ABI, OptimizationDimension.SCREEN_DENSITY));
    assertThat(shards).hasSize(2 * 7);
    assertThat(shards.stream().map(ModuleSplit::getSplitType).distinct().collect(toImmutableSet())).containsExactly(SplitType.STANDALONE);
    ApkTargeting armTargeting = apkAbiTargeting(ARMEABI, ImmutableSet.of(X86));
    ApkTargeting x86Targeting = apkAbiTargeting(X86, ImmutableSet.of(ARMEABI));
    assertThat(shards.stream().map(ModuleSplit::getApkTargeting).collect(toImmutableList())).ignoringRepeatedFieldOrder().containsExactly(mergeApkTargeting(armTargeting, LDPI_TARGETING), mergeApkTargeting(armTargeting, MDPI_TARGETING), mergeApkTargeting(armTargeting, HDPI_TARGETING), mergeApkTargeting(armTargeting, XHDPI_TARGETING), mergeApkTargeting(armTargeting, XXHDPI_TARGETING), mergeApkTargeting(armTargeting, XXXHDPI_TARGETING), mergeApkTargeting(armTargeting, TVDPI_TARGETING), mergeApkTargeting(x86Targeting, LDPI_TARGETING), mergeApkTargeting(x86Targeting, MDPI_TARGETING), mergeApkTargeting(x86Targeting, HDPI_TARGETING), mergeApkTargeting(x86Targeting, XHDPI_TARGETING), mergeApkTargeting(x86Targeting, XXHDPI_TARGETING), mergeApkTargeting(x86Targeting, XXXHDPI_TARGETING), mergeApkTargeting(x86Targeting, TVDPI_TARGETING));
    // Check files not specific to ABI nor screen density.
    for (ModuleSplit shard : shards) {
        assertThat(extractPaths(shard.getEntries())).containsAtLeast("assets/file.txt", "dex/classes.dex", "root/license.dat");
    }
    // Check resources.
    for (ModuleSplit shard : shards) {
        DensityAlias density = shard.getApkTargeting().getScreenDensityTargeting().getValueList().get(0).getDensityAlias();
        switch(density) {
            case LDPI:
            case MDPI:
                assertThat(extractPaths(shard.findEntriesUnderPath("res/"))).containsExactly("res/drawable/image.jpg");
                assertThat(shard.getResourceTable().get()).containsResource("com.test.app:drawable/image").onlyWithConfigs(Configuration.getDefaultInstance());
                break;
            case TVDPI:
                // TVDPI is a special case because the bucket encompasses devices that could serve either
                // the MDPI or the HDPI resource, so both resources are present.
                assertThat(extractPaths(shard.findEntriesUnderPath("res/"))).containsExactly("res/drawable/image.jpg", "res/drawable-hdpi/image.jpg");
                assertThat(shard.getResourceTable().get()).containsResource("com.test.app:drawable/image").onlyWithConfigs(Configuration.getDefaultInstance(), HDPI);
                break;
            case HDPI:
            case XHDPI:
            case XXHDPI:
            case XXXHDPI:
                assertThat(extractPaths(shard.findEntriesUnderPath("res/"))).containsExactly("res/drawable-hdpi/image.jpg");
                assertThat(shard.getResourceTable().get()).containsResource("com.test.app:drawable/image").onlyWithConfigs(HDPI);
                break;
            default:
                fail("Unexpected screen density targeting: " + density);
        }
    }
    // Check native libraries.
    for (ModuleSplit shard : shards) {
        switch(shard.getApkTargeting().getAbiTargeting().getValue(0).getAlias()) {
            case ARMEABI:
                assertThat(extractPaths(shard.getEntries())).contains("lib/armeabi/libtest.so");
                assertThat(extractPaths(shard.getEntries())).containsNoneOf("lib/x86/libtest.so", "lib/x86_64/libtest.so");
                break;
            case X86:
                assertThat(extractPaths(shard.getEntries())).contains("lib/x86/libtest.so");
                assertThat(extractPaths(shard.getEntries())).containsNoneOf("lib/armeabi/libtest.so", "lib/x86_64/libtest.so");
                break;
            default:
                fail("Unexpected ABI targeting in: " + shard.getApkTargeting());
        }
    }
}
Also used : ApkTargeting(com.android.bundle.Targeting.ApkTargeting) TargetingUtils.mergeApkTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.mergeApkTargeting) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 2 with DensityAlias

use of com.android.bundle.Targeting.ScreenDensity.DensityAlias in project bundletool by google.

the class ModuleSplitterTest method testSplittingOnDensityAndLanguage_inSeparateDirectories.

@Test
public void testSplittingOnDensityAndLanguage_inSeparateDirectories() throws Exception {
    BundleModule bundleModule = new BundleModuleBuilder("testModule").addFile("res/drawable-xhdpi/image.jpg").addFile("res/drawable-hdpi/image.jpg").addFile("res/drawable/image.jpg").setResourceTable(resourceTable(pkg(USER_PACKAGE_OFFSET, "com.test.app", type(0x01, "drawable", entry(0x01, "image", fileReference("res/drawable-xhdpi/image.jpg", XHDPI), fileReference("res/drawable-hdpi/image.jpg", HDPI), fileReference("res/drawable/image.jpg", Configuration.getDefaultInstance()))), type(0x02, "string", entry(0x01, "welcome_label", value("Welcome", Configuration.getDefaultInstance()), value("Willkommen", locale("de"))))))).setManifest(androidManifest("com.test.app")).build();
    List<ModuleSplit> splits = createAbiDensityAndLanguageSplitter(bundleModule).splitModule();
    assertThat(splits.stream().map(ModuleSplit::getSplitType).distinct().collect(toImmutableSet())).containsExactly(SplitType.SPLIT);
    assertThat(splits.stream().map(ModuleSplit::getVariantTargeting).distinct().collect(toImmutableSet())).containsExactly(lPlusVariantTargeting());
    Map<String, ModuleSplit> splitsBySuffix = Maps.uniqueIndex(splits, ModuleSplit::getSuffix);
    // 7 density splits (ldpi, mdpi, tvdpi, hdpi, xhdpi, xxhdpi, xxxhdpi) + 1 language split
    // (german)
    // + 1 master split = 9
    assertThat(splitsBySuffix.keySet()).containsExactly("ldpi", "mdpi", "tvdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi", "de", "");
    ImmutableSet<DensityAlias> densities = ImmutableSet.of(DensityAlias.LDPI, DensityAlias.MDPI, DensityAlias.TVDPI, DensityAlias.HDPI, DensityAlias.XHDPI, DensityAlias.XXHDPI, DensityAlias.XXXHDPI);
    assertThat(splitsBySuffix.get("ldpi").getApkTargeting()).ignoringRepeatedFieldOrder().isEqualTo(mergeApkTargeting(DEFAULT_MASTER_SPLIT_SDK_TARGETING, apkDensityTargeting(DensityAlias.LDPI, Sets.difference(densities, ImmutableSet.of(DensityAlias.LDPI)))));
    assertThat(splitsBySuffix.get("mdpi").getApkTargeting()).ignoringRepeatedFieldOrder().isEqualTo(mergeApkTargeting(DEFAULT_MASTER_SPLIT_SDK_TARGETING, apkDensityTargeting(DensityAlias.MDPI, Sets.difference(densities, ImmutableSet.of(DensityAlias.MDPI)))));
    assertThat(splitsBySuffix.get("tvdpi").getApkTargeting()).ignoringRepeatedFieldOrder().isEqualTo(mergeApkTargeting(DEFAULT_MASTER_SPLIT_SDK_TARGETING, apkDensityTargeting(DensityAlias.TVDPI, Sets.difference(densities, ImmutableSet.of(DensityAlias.TVDPI)))));
    assertThat(splitsBySuffix.get("hdpi").getApkTargeting()).ignoringRepeatedFieldOrder().isEqualTo(mergeApkTargeting(DEFAULT_MASTER_SPLIT_SDK_TARGETING, apkDensityTargeting(DensityAlias.HDPI, Sets.difference(densities, ImmutableSet.of(DensityAlias.HDPI)))));
    assertThat(splitsBySuffix.get("xhdpi").getApkTargeting()).ignoringRepeatedFieldOrder().isEqualTo(mergeApkTargeting(DEFAULT_MASTER_SPLIT_SDK_TARGETING, apkDensityTargeting(DensityAlias.XHDPI, Sets.difference(densities, ImmutableSet.of(DensityAlias.XHDPI)))));
    assertThat(splitsBySuffix.get("xxhdpi").getApkTargeting()).ignoringRepeatedFieldOrder().isEqualTo(mergeApkTargeting(DEFAULT_MASTER_SPLIT_SDK_TARGETING, apkDensityTargeting(DensityAlias.XXHDPI, Sets.difference(densities, ImmutableSet.of(DensityAlias.XXHDPI)))));
    assertThat(splitsBySuffix.get("xxxhdpi").getApkTargeting()).ignoringRepeatedFieldOrder().isEqualTo(mergeApkTargeting(DEFAULT_MASTER_SPLIT_SDK_TARGETING, apkDensityTargeting(DensityAlias.XXXHDPI, Sets.difference(densities, ImmutableSet.of(DensityAlias.XXXHDPI)))));
    assertThat(splitsBySuffix.get("de").getApkTargeting()).ignoringRepeatedFieldOrder().isEqualTo(mergeApkTargeting(DEFAULT_MASTER_SPLIT_SDK_TARGETING, apkLanguageTargeting("de")));
    assertThat(splitsBySuffix.get("").getApkTargeting()).ignoringRepeatedFieldOrder().isEqualTo(DEFAULT_MASTER_SPLIT_SDK_TARGETING);
}
Also used : BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Test(org.junit.Test)

Example 3 with DensityAlias

use of com.android.bundle.Targeting.ScreenDensity.DensityAlias in project bundletool by google.

the class ScreenDensitySelectorTest method desiredDensityBetweenBoth_higherMatches.

@Test
public void desiredDensityBetweenBoth_higherMatches() {
    // ldpi = 120, mdpi = 160, hdpi = 240
    // left side = ((2 * l) - requested-dpi) * h = ((2* 120) - 160) * 240 = 80 * 240 = 19200
    // right side = requested-dpi * requested-dpi = 160 * 160 = 25600
    // left side < right side, so higher dpi resource wins. (see ResourceTypes.cpp)
    DensityAlias desiredDensity = DensityAlias.MDPI;
    ImmutableList<ConfigValue> densityConfigs = ImmutableList.of(onlyConfig(HDPI), onlyConfig(LDPI));
    assertThat(new ScreenDensitySelector().selectBestConfigValue(densityConfigs, desiredDensity, DEFAULT_BUNDLE_VERSION)).isEqualTo(onlyConfig(HDPI));
    assertThat(new ScreenDensitySelector().selectBestDensity(toDensities(densityConfigs), toDpi(desiredDensity))).isEqualTo(HDPI_VALUE);
}
Also used : ConfigValue(com.android.aapt.Resources.ConfigValue) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Test(org.junit.Test)

Example 4 with DensityAlias

use of com.android.bundle.Targeting.ScreenDensity.DensityAlias in project bundletool by google.

the class ScreenDensitySelectorTest method desiredDensityBetweenBoth_lowerMatches.

@Test
public void desiredDensityBetweenBoth_lowerMatches() {
    // tvdpi = 213, (requested-dpi) hdpi = 240, xhdpi = 320
    // left side = ((2 * l) - requested-dpi) * h = ((2* 213) - 240) * 320 = 426 * 320
    // right side = requested-dpi * requested-dpi = 240 * 240
    // left side > right side, so lower dpi resource wins. (see ResourceTypes.cpp)
    DensityAlias desiredDensity = DensityAlias.HDPI;
    ImmutableList<ConfigValue> densityConfigs = ImmutableList.of(onlyConfig(TVDPI), onlyConfig(XHDPI));
    assertThat(new ScreenDensitySelector().selectBestConfigValue(densityConfigs, desiredDensity, DEFAULT_BUNDLE_VERSION)).isEqualTo(onlyConfig(TVDPI));
    assertThat(new ScreenDensitySelector().selectBestDensity(toDensities(densityConfigs), toDpi(desiredDensity))).isEqualTo(TVDPI_VALUE);
}
Also used : ConfigValue(com.android.aapt.Resources.ConfigValue) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Test(org.junit.Test)

Example 5 with DensityAlias

use of com.android.bundle.Targeting.ScreenDensity.DensityAlias in project bundletool by google.

the class ScreenDensitySelectorTest method theHighestResourceSplit_matchesAllConfigsDownTo.

@Test
public void theHighestResourceSplit_matchesAllConfigsDownTo() {
    // The cut-off for XXXHDPI(640 dpi) when XXHDPI (480 dpi) is present is 527 dpi device.
    // 527dpi device still prefers 481dpi (1 dpi more than XXHDPI) resource over 640dpi.
    DensityAlias desiredDensity = DensityAlias.XXXHDPI;
    ImmutableSet<DensityAlias> alternatives = ImmutableSet.of(DensityAlias.XXHDPI);
    ImmutableList<ConfigValue> densityConfigs = ImmutableList.of(onlyConfig(XXHDPI), onlyConfig(forDpi(481)), onlyConfig(XXXHDPI));
    assertThat(new ScreenDensitySelector().selectAllMatchingConfigValues(densityConfigs, desiredDensity, alternatives, DEFAULT_BUNDLE_VERSION)).containsExactlyElementsIn(ImmutableList.of(onlyConfig(forDpi(481)), onlyConfig(XXXHDPI)));
}
Also used : ConfigValue(com.android.aapt.Resources.ConfigValue) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Test(org.junit.Test)

Aggregations

DensityAlias (com.android.bundle.Targeting.ScreenDensity.DensityAlias)19 Test (org.junit.Test)17 ConfigValue (com.android.aapt.Resources.ConfigValue)12 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)10 BundleModule (com.android.tools.build.bundletool.model.BundleModule)8 BundleModuleBuilder (com.android.tools.build.bundletool.testing.BundleModuleBuilder)8 ResourceTable (com.android.aapt.Resources.ResourceTable)5 ResourceTableBuilder (com.android.tools.build.bundletool.testing.ResourceTableBuilder)5 ApkTargeting (com.android.bundle.Targeting.ApkTargeting)4 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 Configuration (com.android.aapt.ConfigurationOuterClass.Configuration)3 ScreenDensity (com.android.bundle.Targeting.ScreenDensity)3 ManifestMutator.withSplitsRequired (com.android.tools.build.bundletool.model.ManifestMutator.withSplitsRequired)3 ResourceId (com.android.tools.build.bundletool.model.ResourceId)3 DEFAULT_DENSITY_VALUE (com.android.tools.build.bundletool.model.utils.ResourcesUtils.DEFAULT_DENSITY_VALUE)3 Version (com.android.tools.build.bundletool.model.version.Version)3 ImmutableCollection (com.google.common.collect.ImmutableCollection)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)3