Search in sources :

Example 11 with DensityAlias

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

the class ScreenDensityResourcesSplitter method splitInternal.

@Override
public ImmutableCollection<ModuleSplit> splitInternal(ModuleSplit split) {
    Optional<ResourceTable> resourceTable = split.getResourceTable();
    // No resource tables means no resources, hence nothing to split here.
    if (!resourceTable.isPresent() || resourceTable.get().equals(ResourceTable.getDefaultInstance())) {
        return ImmutableList.of(split);
    }
    ImmutableList.Builder<ModuleSplit> splitsBuilder = new ImmutableList.Builder<>();
    for (DensityAlias density : densityBuckets) {
        ResourceTable optimizedTable = filterResourceTableForDensity(resourceTable.get(), density);
        // Don't generate empty splits.
        if (optimizedTable.equals(ResourceTable.getDefaultInstance())) {
            continue;
        }
        ModuleSplit.Builder moduleSplitBuilder = split.toBuilder().setApkTargeting(split.getApkTargeting().toBuilder().setScreenDensityTargeting(ScreenDensityTargeting.newBuilder().addValue(toScreenDensity(density)).addAllAlternatives(allBut(densityBuckets, density).stream().map(ScreenDensityResourcesSplitter::toScreenDensity).collect(toImmutableList()))).build()).setMasterSplit(false).addMasterManifestMutator(withSplitsRequired(true)).setEntries(ModuleSplit.filterResourceEntries(split.getEntries(), optimizedTable)).setResourceTable(optimizedTable);
        splitsBuilder.add(moduleSplitBuilder.build());
    }
    ModuleSplit defaultResourcesSplit = getDefaultResourcesSplit(split, splitsBuilder.build());
    return splitsBuilder.add(defaultResourcesSplit).build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) ResourceTable(com.android.aapt.Resources.ResourceTable)

Example 12 with DensityAlias

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

the class ScreenDensitySelectorTest method sortedOrder_deviceMatchesExistingDpi_notLessThan.

@Test
public void sortedOrder_deviceMatchesExistingDpi_notLessThan() {
    DensityAlias desiredDensity = DensityAlias.HDPI;
    ImmutableList<ConfigValue> densityConfigs = ImmutableList.of(onlyConfig(HDPI), onlyConfig(XHDPI), onlyConfig(XXHDPI), onlyConfig(XXXHDPI));
    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 13 with DensityAlias

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

the class ScreenDensitySelectorTest method theOnlyResourceSplit_matchesAllConfigs.

@Test
public void theOnlyResourceSplit_matchesAllConfigs() {
    DensityAlias desiredDensity = DensityAlias.MDPI;
    ImmutableList<ConfigValue> densityConfigs = ImmutableList.of(onlyConfig(LDPI), onlyConfig(HDPI), onlyConfig(XXXHDPI));
    assertThat(new ScreenDensitySelector().selectAllMatchingConfigValues(densityConfigs, desiredDensity, /* alternatives= */
    ImmutableSet.of(), DEFAULT_BUNDLE_VERSION)).containsExactlyElementsIn(densityConfigs);
}
Also used : ConfigValue(com.android.aapt.Resources.ConfigValue) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Test(org.junit.Test)

Example 14 with DensityAlias

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

the class StandaloneApksGeneratorTest method shardByAbiAndDensity_havingManyAbisAndSomeResource_producesManyApks.

@Test
public void shardByAbiAndDensity_havingManyAbisAndSomeResource_producesManyApks() throws Exception {
    BundleModule bundleModule = new BundleModuleBuilder("base").addFile("assets/file.txt").addFile("dex/classes.dex").addFile("lib/armeabi/libtest.so").addFile("lib/x86/libtest.so").addFile("lib/x86_64/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/armeabi", nativeDirectoryTargeting(ARMEABI)), targetedNativeDirectory("lib/x86", nativeDirectoryTargeting(X86)), targetedNativeDirectory("lib/x86_64", nativeDirectoryTargeting(X86_64)))).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 = standaloneApksGenerator.generateStandaloneApks(ImmutableList.of(bundleModule), standaloneApkOptimizations(OptimizationDimension.ABI, OptimizationDimension.SCREEN_DENSITY));
    assertThat(shards).hasSize(3 * 7);
    assertThat(shards.stream().map(ModuleSplit::getSplitType).distinct().collect(toImmutableSet())).containsExactly(SplitType.STANDALONE);
    ApkTargeting armTargeting = apkAbiTargeting(ARMEABI, ImmutableSet.of(X86, X86_64));
    ApkTargeting x86Targeting = apkAbiTargeting(X86, ImmutableSet.of(ARMEABI, X86_64));
    ApkTargeting x64Targeting = apkAbiTargeting(X86_64, ImmutableSet.of(ARMEABI, X86));
    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), mergeApkTargeting(x64Targeting, LDPI_TARGETING), mergeApkTargeting(x64Targeting, MDPI_TARGETING), mergeApkTargeting(x64Targeting, HDPI_TARGETING), mergeApkTargeting(x64Targeting, XHDPI_TARGETING), mergeApkTargeting(x64Targeting, XXHDPI_TARGETING), mergeApkTargeting(x64Targeting, XXXHDPI_TARGETING), mergeApkTargeting(x64Targeting, 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().getValue(0).getDensityAlias();
        switch(density) {
            case LDPI:
                assertThat(extractPaths(shard.findEntriesUnderPath("res"))).containsExactly("res/drawable-ldpi/image.jpg");
                assertThat(shard.getResourceTable().get()).containsResource("com.test.app:drawable/image").onlyWithConfigs(LDPI);
                break;
            case MDPI:
                // MDPI is a special case because the bucket encompasses devices that could serve either
                // the LDPI or the HDPI resource, so both resources are present.
                assertThat(extractPaths(shard.findEntriesUnderPath("res"))).containsExactly("res/drawable-ldpi/image.jpg", "res/drawable-hdpi/image.jpg");
                assertThat(shard.getResourceTable().get()).containsResource("com.test.app:drawable/image").onlyWithConfigs(LDPI, HDPI);
                break;
            case TVDPI:
            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 targeting density: " + 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;
            case X86_64:
                assertThat(extractPaths(shard.getEntries())).contains("lib/x86_64/libtest.so");
                assertThat(extractPaths(shard.getEntries())).containsNoneOf("lib/armeabi/libtest.so", "lib/x86/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 15 with DensityAlias

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

the class ScreenDensityResourcesSplitterTest method lowestDensityConfigsPinnedToMaster_masterCoversRangeOfDensities.

@Test
public void lowestDensityConfigsPinnedToMaster_masterCoversRangeOfDensities() throws Exception {
    BundleModule testModule = new BundleModuleBuilder("testModule").addFile("res/drawable-ldpi/image.jpg").addFile("res/drawable-xxxhdpi/image.jpg").addFile("res/drawable-ldpi/other.jpg").addFile("res/drawable-mdpi/other.jpg").addFile("res/drawable-tvdpi/other.jpg").addFile("res/drawable-xhdpi/other.jpg").addFile("res/drawable-xxhdpi/other.jpg").addFile("res/drawable-xxxhdpi/other.jpg").setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addDrawableResourceForMultipleDensities("image", ImmutableMap.of(/* ldpi */
    120, "res/drawable-ldpi/image.jpg", /* xxxhdpi */
    640, "res/drawable-xxxhdpi/image.jpg")).addDrawableResourceForMultipleDensities("other", ImmutableMap.<Integer, String>builder().put(/* ldpi */
    120, "res/drawable-ldpi/other.jpg").put(/* mdpi */
    160, "res/drawable-mdpi/other.jpg").put(/* tvdpi */
    213, "res/drawable-tvdpi/other.jpg").put(/* hdpi */
    240, "res/drawable-hdpi/other.jpg").put(/* xhdpi */
    320, "res/drawable-xhdpi/other.jpg").put(/* xxhdpi */
    480, "res/drawable-xxhdpi/other.jpg").put(/* xxxhdpi */
    640, "res/drawable-xxxhdpi/image.jpg").build()).build()).setManifest(androidManifest("com.test.app")).build();
    // 0x7f010000 is the "drawable/image" resource.
    Predicate<ResourceId> pinnedLowDensityResourcesPredicate = resourceId -> resourceId.getFullResourceId() == 0x7f010000;
    ScreenDensityResourcesSplitter splitter = new ScreenDensityResourcesSplitter(BundleToolVersion.getCurrentVersion(), NO_RESOURCES_PINNED_TO_MASTER, pinnedLowDensityResourcesPredicate, /* pinLowestBucketOfStylesToMaster= */
    false);
    ImmutableCollection<ModuleSplit> densitySplits = splitter.split(ModuleSplit.forResources(testModule));
    ImmutableList<ModuleSplit> configSplits = densitySplits.stream().filter(split -> !split.isMasterSplit()).collect(toImmutableList());
    assertThat(configSplits).isNotEmpty();
    for (ModuleSplit configSplit : configSplits) {
        DensityAlias targetDensity = configSplit.getApkTargeting().getScreenDensityTargeting().getValue(0).getDensityAlias();
        switch(targetDensity) {
            case LDPI:
            case MDPI:
                // Devices <= MDPI are covered by the LDPI config.
                assertThat(extractPaths(configSplit.getEntries())).doesNotContain("res/drawable-xxxhdpi/image.jpg");
                break;
            default:
                // Devices > MDPI are covered by the XXXHDPI config.
                assertThat(extractPaths(configSplit.getEntries())).contains("res/drawable-xxxhdpi/image.jpg");
        }
    }
}
Also used : ApkTargeting(com.android.bundle.Targeting.ApkTargeting) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) ImmutableCollection(com.google.common.collect.ImmutableCollection) MoreCollectors.onlyElement(com.google.common.collect.MoreCollectors.onlyElement) ManifestMutator.withSplitsRequired(com.android.tools.build.bundletool.model.ManifestMutator.withSplitsRequired) Item(com.android.aapt.Resources.Item) LDPI_VALUE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.LDPI_VALUE) TVDPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory.TVDPI) ResourceId(com.android.tools.build.bundletool.model.ResourceId) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) ResourcesTableFactory.entry(com.android.tools.build.bundletool.testing.ResourcesTableFactory.entry) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Version(com.android.tools.build.bundletool.model.version.Version) ResourcesTableFactory.type(com.android.tools.build.bundletool.testing.ResourcesTableFactory.type) XXHDPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory.XXHDPI) ResourceTable(com.android.aapt.Resources.ResourceTable) Theory(org.junit.experimental.theories.Theory) BundleToolVersion(com.android.tools.build.bundletool.model.version.BundleToolVersion) ImmutableSet(com.google.common.collect.ImmutableSet) ConfigValue(com.android.aapt.Resources.ConfigValue) ImmutableMap(com.google.common.collect.ImmutableMap) XXXHDPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory.XXXHDPI) Predicate(java.util.function.Predicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) DEFAULT_DENSITY_BUCKETS(com.android.tools.build.bundletool.splitters.ScreenDensityResourcesSplitter.DEFAULT_DENSITY_BUCKETS) ResourcesTableFactory.sdk(com.android.tools.build.bundletool.testing.ResourcesTableFactory.sdk) XXXHDPI_VALUE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.XXXHDPI_VALUE) Collectors(java.util.stream.Collectors) ScreenDensity(com.android.bundle.Targeting.ScreenDensity) Sets(com.google.common.collect.Sets) FromDataPoints(org.junit.experimental.theories.FromDataPoints) ByteString(com.google.protobuf.ByteString) List(java.util.List) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) Optional(java.util.Optional) DataPoints(org.junit.experimental.theories.DataPoints) DEFAULT_DENSITY_VALUE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.DEFAULT_DENSITY_VALUE) MDPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory.MDPI) Value(com.android.aapt.Resources.Value) RunWith(org.junit.runner.RunWith) MDPI_VALUE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.MDPI_VALUE) TestUtils.extractPaths(com.android.tools.build.bundletool.testing.TestUtils.extractPaths) ResourcesTableFactory.resourceTable(com.android.tools.build.bundletool.testing.ResourcesTableFactory.resourceTable) TruthResourceTable.assertThat(com.android.tools.build.bundletool.testing.truth.resources.TruthResourceTable.assertThat) BundleModuleBuilder(com.android.tools.build.bundletool.testing.BundleModuleBuilder) ProtoTruth.assertThat(com.google.common.truth.extensions.proto.ProtoTruth.assertThat) ImmutableList(com.google.common.collect.ImmutableList) Theories(org.junit.experimental.theories.Theories) TVDPI_VALUE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.TVDPI_VALUE) ResourcesTableFactory.mergeConfigs(com.android.tools.build.bundletool.testing.ResourcesTableFactory.mergeConfigs) Truth8(com.google.common.truth.Truth8) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Predicates(com.google.common.base.Predicates) ResourcesTableFactory.pkg(com.android.tools.build.bundletool.testing.ResourcesTableFactory.pkg) Truth8.assertThat(com.google.common.truth.Truth8.assertThat) ResourcesTableFactory._560DPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory._560DPI) ManifestProtoUtils.compareManifestMutators(com.android.tools.build.bundletool.testing.ManifestProtoUtils.compareManifestMutators) LDPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory.LDPI) HDPI_VALUE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.HDPI_VALUE) USER_PACKAGE_OFFSET(com.android.tools.build.bundletool.testing.ResourcesTableFactory.USER_PACKAGE_OFFSET) HDPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory.HDPI) TestCase.fail(junit.framework.TestCase.fail) XXHDPI_VALUE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.XXHDPI_VALUE) TargetingUtils.assertForNonDefaultSplits(com.android.tools.build.bundletool.testing.TargetingUtils.assertForNonDefaultSplits) Test(org.junit.Test) XHDPI(com.android.tools.build.bundletool.testing.ResourcesTableFactory.XHDPI) Truth.assertThat(com.google.common.truth.Truth.assertThat) ResourcesTableFactory.fileReference(com.android.tools.build.bundletool.testing.ResourcesTableFactory.fileReference) TargetingUtils.apkDensityTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.apkDensityTargeting) ResourcesTableFactory.forDpi(com.android.tools.build.bundletool.testing.ResourcesTableFactory.forDpi) StringPool(com.android.aapt.Resources.StringPool) XHDPI_VALUE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.XHDPI_VALUE) TargetingUtils.assertForSingleDefaultSplit(com.android.tools.build.bundletool.testing.TargetingUtils.assertForSingleDefaultSplit) BundleModule(com.android.tools.build.bundletool.model.BundleModule) Configuration(com.android.aapt.ConfigurationOuterClass.Configuration) ResourceId(com.android.tools.build.bundletool.model.ResourceId) 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)

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