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();
}
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);
}
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);
}
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());
}
}
}
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");
}
}
}
Aggregations