use of com.android.bundle.Targeting.AssetsDirectoryTargeting in project bundletool by google.
the class BuildApksManagerTest method splits_assetMixedTextureTargetingWithSuffixStripped_featureModule.
@Test
public void splits_assetMixedTextureTargetingWithSuffixStripped_featureModule() throws Exception {
// Create a bundle with assets containing both ETC1 textures and textures without
// targeting specified.
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.setManifest(androidManifest("com.test.app")).setResourceTable(resourceTableWithTestLabel("Test feature"))).addModule("feature_tcf_assets", builder -> builder.addFile("assets/textures/untargeted_texture.dat").addFile("assets/textures#tcf_etc1/etc1_texture.dat").setAssetsConfig(assets(targetedAssetsDirectory("assets/textures", assetsDirectoryTargeting(alternativeTextureCompressionTargeting(ETC1_RGB8))), targetedAssetsDirectory("assets/textures#tcf_etc1", assetsDirectoryTargeting(textureCompressionTargeting(ETC1_RGB8))))).setManifest(androidManifestForFeature("com.test.app", withTitle("@string/test_label", TEST_LABEL_RESOURCE_ID)))).setBundleConfig(BundleConfigBuilder.create().addSplitDimension(Value.TEXTURE_COMPRESSION_FORMAT, /* negate= */
false, /* stripSuffix= */
true, /* defaultSuffix= */
"etc1").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> splitApks = apkDescriptions(splitApkVariants);
assertThat(splitApkVariants(result)).hasSize(1);
Variant splitApkVariant = splitApkVariants(result).get(0);
assertThat(splitApkVariant.getApkSetList()).hasSize(2);
// Check that apks for ETC1 and "Other TCF" have been created
ImmutableList<ApkDescription> tcfSplits = splitApks.stream().filter(apkDesc -> apkDesc.getTargeting().hasTextureCompressionFormatTargeting()).collect(toImmutableList());
assertThat(apkNamesInApkDescriptions(tcfSplits)).containsExactly("feature_tcf_assets-other_tcf.apk", "feature_tcf_assets-etc1_rgb8.apk");
// Check the content of the apks
for (ApkDescription split : tcfSplits) {
TextureCompressionFormatTargeting textureFormatTargeting = split.getTargeting().getTextureCompressionFormatTargeting();
Set<String> files = filesInApk(split, apkSetFile);
if (textureFormatTargeting.getValueList().isEmpty()) {
// The "Other TCF" split contains the untargeted texture only.
assertThat(files).contains("assets/textures/untargeted_texture.dat");
assertThat(files).doesNotContain("assets/textures#tcf_etc1/etc1_texture.dat");
} else {
// The "ETC1" split contains the ETC1 texture only.
TextureCompressionFormat format = textureFormatTargeting.getValueList().get(0);
assertThat(format.getAlias()).isEqualTo(ETC1_RGB8);
// Suffix stripping was enabled, so "textures#tcf_etc1" folder is now renamed to "textures".
assertThat(files).contains("assets/textures/etc1_texture.dat");
assertThat(files).doesNotContain("assets/textures#tcf_etc1/etc1_texture.dat");
assertThat(files).doesNotContain("assets/textures/untargeted_texture.dat");
}
}
}
use of com.android.bundle.Targeting.AssetsDirectoryTargeting in project bundletool by google.
the class BuildApksManagerTest method buildApksCommand_standalone_deviceTierTargetingWithSuffixStripped.
@Test
public void buildApksCommand_standalone_deviceTierTargetingWithSuffixStripped() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.setManifest(androidManifest("com.test.app")).setResourceTable(resourceTableWithTestLabel("Test feature"))).addModule("device_tier_assets", builder -> builder.addFile("assets/img#tier_0/asset_low.dat").addFile("assets/img#tier_1/asset_high.dat").setAssetsConfig(assets(targetedAssetsDirectory("assets/img#tier_0", assetsDirectoryTargeting(deviceTierTargeting(0))), targetedAssetsDirectory("assets/img#tier_1", assetsDirectoryTargeting(deviceTierTargeting(1))))).setManifest(androidManifestForAssetModule("com.test.app", withInstallTimeDelivery()))).setBundleConfig(BundleConfigBuilder.create().addSplitDimension(Value.DEVICE_TIER, /* negate= */
false, /* stripSuffix= */
true, /* defaultSuffix= */
"0").build()).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
assertThat(standaloneApkVariants(result)).hasSize(1);
assertThat(apkDescriptions(standaloneApkVariants(result))).hasSize(1);
ApkDescription shard = apkDescriptions(standaloneApkVariants(result)).get(0);
// Check APK content
assertThat(apkSetFile).hasFile(shard.getPath());
try (ZipFile shardZip = new ZipFile(extractFromApkSetFile(apkSetFile, shard.getPath(), outputDir))) {
assertThat(shardZip).hasFile("assets/img/asset_low.dat");
assertThat(shardZip).doesNotHaveFile("assets/img/asset_high.dat");
assertThat(shardZip).doesNotHaveFile("assets/img#tier_0/asset_low.dat");
assertThat(shardZip).doesNotHaveFile("assets/img#tier_1/asset_high.dat");
}
// Check that default device tier targeting was applied to the APK
assertThat(shard.getTargeting().getDeviceTierTargeting().getValueList()).containsExactly(Int32Value.of(0));
}
use of com.android.bundle.Targeting.AssetsDirectoryTargeting in project bundletool by google.
the class BuildApksManagerTest method deviceTieredAssets_inAssetModule.
@Test
public void deviceTieredAssets_inAssetModule() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.setManifest(androidManifest("com.test.app"))).addModule("assetmodule", builder -> builder.addFile("assets/images#tier_0/image.jpg").addFile("assets/images#tier_1/image.jpg").setManifest(androidManifestForAssetModule("com.test.app")).setAssetsConfig(assets(targetedAssetsDirectory("assets/images#tier_0", assetsDirectoryTargeting(deviceTierTargeting(/* value= */
0, /* alternatives= */
ImmutableList.of(1)))), targetedAssetsDirectory("assets/images#tier_1", assetsDirectoryTargeting(deviceTierTargeting(/* value= */
1, /* alternatives= */
ImmutableList.of(0))))))).setBundleConfig(BundleConfigBuilder.create().addSplitDimension(Value.DEVICE_TIER, /* negate= */
false, /* stripSuffix= */
false, /* defaultSuffix= */
"0").build()).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
assertThat(result.getAssetSliceSetList()).hasSize(1);
List<ApkDescription> assetSlices = result.getAssetSliceSet(0).getApkDescriptionList();
// Check that apks for tier 0 and 1 have been created
ImmutableList<ApkDescription> deviceTierSplits = assetSlices.stream().filter(apkDesc -> apkDesc.getTargeting().hasDeviceTierTargeting()).collect(toImmutableList());
assertThat(apkNamesInApkDescriptions(deviceTierSplits)).containsExactly("assetmodule-tier_0.apk", "assetmodule-tier_1.apk");
// Check the content of the APKs
ImmutableMap<DeviceTierTargeting, ApkDescription> deviceTierSplitsByTargeting = Maps.uniqueIndex(deviceTierSplits, apk -> apk.getTargeting().getDeviceTierTargeting());
ApkDescription lowTierSplit = deviceTierSplitsByTargeting.get(deviceTierTargeting(/* value= */
0, /* alternatives= */
ImmutableList.of(1)));
assertThat(ZipPath.create(lowTierSplit.getPath()).getFileName().toString()).isEqualTo("assetmodule-tier_0.apk");
assertThat(filesInApk(lowTierSplit, apkSetFile)).contains("assets/images#tier_0/image.jpg");
ApkDescription mediumTierSplit = deviceTierSplitsByTargeting.get(deviceTierTargeting(/* value= */
1, /* alternatives= */
ImmutableList.of(0)));
assertThat(ZipPath.create(mediumTierSplit.getPath()).getFileName().toString()).isEqualTo("assetmodule-tier_1.apk");
assertThat(filesInApk(mediumTierSplit, apkSetFile)).contains("assets/images#tier_1/image.jpg");
}
use of com.android.bundle.Targeting.AssetsDirectoryTargeting in project bundletool by google.
the class TargetingGenerator method generateTargetingForAssets.
/**
* Processes given asset directories, generating targeting based on their names.
*
* @param assetDirectories Names of directories under assets/, including the "assets/" prefix.
* @return Targeting for the given asset directories.
*/
public Assets generateTargetingForAssets(Collection<ZipPath> assetDirectories) {
for (ZipPath directory : assetDirectories) {
// Extra '/' to handle special case when the directory is just "assets".
checkRootDirectoryName(ASSETS_DIR, directory + "/");
}
// Stores all different targeting values for a given set of sibling targeted directories.
// Key: targeted directory base name {@link TargetedDirectory#getPathBaseName}
// Values: {@link AssetsDirectoryTargeting} targeting expressed as value for each sibling.
HashMultimap<String, AssetsDirectoryTargeting> targetingByBaseName = HashMultimap.create();
for (ZipPath assetDirectory : FileUtils.toPathWalkingOrder(assetDirectories)) {
TargetedDirectory targetedDirectory = TargetedDirectory.parse(assetDirectory);
targetingByBaseName.put(targetedDirectory.getPathBaseName(), targetedDirectory.getLastSegment().getTargeting());
}
validateDimensions(targetingByBaseName);
// Pass 2: Building the directory targeting proto using the targetingByBaseName map.
Assets.Builder assetsBuilder = Assets.newBuilder();
for (ZipPath assetDirectory : assetDirectories) {
AssetsDirectoryTargeting.Builder targeting = AssetsDirectoryTargeting.newBuilder();
TargetedDirectory targetedDirectory = TargetedDirectory.parse(assetDirectory);
// We will calculate targeting of each path segment and merge them together.
for (int i = 0; i < targetedDirectory.getPathSegments().size(); i++) {
TargetedDirectorySegment segment = targetedDirectory.getPathSegments().get(i);
// Set targeting values.
targeting.mergeFrom(segment.getTargeting());
// Set targeting alternatives.
if (segment.getTargeting().hasLanguage()) {
// identical language across resources and assets.
continue;
}
targeting.mergeFrom(// Remove oneself from the alternatives and merge them together.
Sets.difference(targetingByBaseName.get(targetedDirectory.getSubPathBaseName(i)), ImmutableSet.of(segment.getTargeting())).stream().map(TargetingProtoUtils::toAlternativeTargeting).reduce(AssetsDirectoryTargeting.newBuilder(), (builder, targetingValue) -> builder.mergeFrom(targetingValue), (builderA, builderB) -> builderA.mergeFrom(builderB.build())).build());
}
assetsBuilder.addDirectory(TargetedAssetsDirectory.newBuilder().setPath(assetDirectory.toString()).setTargeting(targeting));
}
return assetsBuilder.build();
}
use of com.android.bundle.Targeting.AssetsDirectoryTargeting in project bundletool by google.
the class TextureCompressionFormatParityValidator method getSupportedTextureCompressionFormats.
private static SupportedTextureCompressionFormats getSupportedTextureCompressionFormats(BundleModule module) {
// Extract targeted directories from entries (like done when generating assets targeting)
ImmutableSet<TargetedDirectory> targetedDirectories = extractAssetsTargetedDirectories(module);
// Inspect the targetings to extract texture compression formats.
ImmutableSet<TextureCompressionFormatAlias> formats = extractTextureCompressionFormats(targetedDirectories);
// Check if one or more targeted directories have "fallback" sibling directories.
boolean hasFallback = targetedDirectories.stream().anyMatch(directory -> {
Optional<AssetsDirectoryTargeting> targeting = directory.getTargeting(TargetingDimension.TEXTURE_COMPRESSION_FORMAT);
if (targeting.isPresent()) {
// Check if a sibling folder without texture targeting exists. If yes, this is
// called a "fallback".
TargetedDirectory siblingFallbackDirectory = directory.removeTargeting(TargetingDimension.TEXTURE_COMPRESSION_FORMAT);
return module.findEntriesUnderPath(siblingFallbackDirectory.toZipPath()).findAny().isPresent();
}
return false;
});
return SupportedTextureCompressionFormats.create(formats, hasFallback);
}
Aggregations