use of com.android.bundle.Targeting.AssetsDirectoryTargeting in project bundletool by google.
the class AssetsDimensionSplitterFactory method createSplitter.
/**
* Creates a {@link ModuleSplitSplitter} capable of splitting on a given Asset targeting
* dimension, with, optionally, a dimension to be removed from asset paths.
*
* @param <T> the proto buffer message class representing the splitting targeting dimension.
* @param dimensionGetter function that extracts the sub-message representing a targeting
* dimension.
* @param targetingSetter function that creates a split targeting that will be merged with the
* targeting of the input {@link ModuleSplit}.
* @param hasTargeting predicate to test if the input {@link ModuleSplit} is already targeting on
* the dimension of this splitter.
* @param targetingDimensionToRemove If not empty, the targeting for this dimension will be
* removed from asset paths (i.e: suffixes like #tcf_xxx will be removed from paths).
* @return {@link ModuleSplitSplitter} for a given dimension functions.
*/
public static <T extends Message> ModuleSplitSplitter createSplitter(Function<AssetsDirectoryTargeting, T> dimensionGetter, Function<T, ApkTargeting> targetingSetter, Predicate<ApkTargeting> hasTargeting, Optional<TargetingDimension> targetingDimensionToRemove) {
return new ModuleSplitSplitter() {
@Override
public ImmutableCollection<ModuleSplit> split(ModuleSplit split) {
checkArgument(!hasTargeting.test(split.getApkTargeting()), "Split is already targeting the splitting dimension.");
return split.getAssetsConfig().map(assetsConfig -> splitAssetsDirectories(assetsConfig, split)).orElse(ImmutableList.of(split)).stream().map(moduleSplit -> moduleSplit.isMasterSplit() ? moduleSplit : removeAssetsTargeting(moduleSplit)).collect(toImmutableList());
}
private ModuleSplit removeAssetsTargeting(ModuleSplit split) {
return targetingDimensionToRemove.isPresent() ? SuffixStripper.createForDimension(targetingDimensionToRemove.get()).removeAssetsTargeting(split) : split;
}
private ImmutableList<ModuleSplit> splitAssetsDirectories(Assets assets, ModuleSplit split) {
Multimap<T, TargetedAssetsDirectory> directoriesMap = Multimaps.filterKeys(Multimaps.index(assets.getDirectoryList(), targetedDirectory -> dimensionGetter.apply(targetedDirectory.getTargeting())), not(this::isDefaultTargeting));
ImmutableList.Builder<ModuleSplit> splitsBuilder = new ImmutableList.Builder<>();
// Generate config splits.
directoriesMap.asMap().entrySet().forEach(entry -> {
ImmutableList<ModuleEntry> entries = listEntriesFromDirectories(entry.getValue(), split);
if (entries.isEmpty()) {
return;
}
ModuleSplit.Builder modifiedSplit = split.toBuilder();
modifiedSplit.setEntries(entries).setApkTargeting(generateTargeting(split.getApkTargeting(), entry.getKey())).setMasterSplit(false).addMasterManifestMutator(withSplitsRequired(true));
splitsBuilder.add(modifiedSplit.build());
});
// Ensure that master split (even an empty one) always exists.
ModuleSplit defaultSplit = getDefaultAssetsSplit(split, splitsBuilder.build());
if (defaultSplit.isMasterSplit() || !defaultSplit.getEntries().isEmpty()) {
splitsBuilder.add(defaultSplit);
}
return splitsBuilder.build();
}
private ModuleSplit getDefaultAssetsSplit(ModuleSplit inputSplit, ImmutableList<ModuleSplit> configSplits) {
ImmutableSet<ModuleEntry> claimedEntries = configSplits.stream().map(ModuleSplit::getEntries).flatMap(Collection::stream).collect(toImmutableSet());
return inputSplit.toBuilder().setEntries(inputSplit.getEntries().stream().filter(not(claimedEntries::contains)).collect(toImmutableList())).build();
}
private boolean isDefaultTargeting(T splittingDimensionTargeting) {
return splittingDimensionTargeting.equals(splittingDimensionTargeting.getDefaultInstanceForType());
}
private ApkTargeting generateTargeting(ApkTargeting splitTargeting, T extraTargeting) {
if (isDefaultTargeting(extraTargeting)) {
return splitTargeting;
}
return splitTargeting.toBuilder().mergeFrom(targetingSetter.apply(extraTargeting)).build();
}
private ImmutableList<ModuleEntry> listEntriesFromDirectories(Collection<TargetedAssetsDirectory> directories, ModuleSplit moduleSplit) {
return directories.stream().map(targetedAssetsDirectory -> ZipPath.create(targetedAssetsDirectory.getPath())).flatMap(moduleSplit::getEntriesInDirectory).collect(toImmutableList());
}
};
}
use of com.android.bundle.Targeting.AssetsDirectoryTargeting in project bundletool by google.
the class BuildApksManagerTest method buildApksCommand_standalone_noTextureTargeting.
@Test
public void buildApksCommand_standalone_noTextureTargeting() throws Exception {
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#tcf_atc/texture.dat").addFile("assets/textures#tcf_etc1/texture.dat").setAssetsConfig(assets(targetedAssetsDirectory("assets/textures#tcf_atc", assetsDirectoryTargeting(textureCompressionTargeting(ATC))), targetedAssetsDirectory("assets/textures#tcf_etc1", assetsDirectoryTargeting(textureCompressionTargeting(ETC1_RGB8))))).setManifest(androidManifestForFeature("com.test.app", withTitle("@string/test_label", TEST_LABEL_RESOURCE_ID)))).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).withOptimizationDimensions(TEXTURE_COMPRESSION_FORMAT).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);
assertThat(apkSetFile).hasFile(shard.getPath());
try (ZipFile shardZip = new ZipFile(extractFromApkSetFile(apkSetFile, shard.getPath(), outputDir))) {
// Even if we used targeted folders, they are all included because we did not activate
// texture targeting optimization.
assertThat(shardZip).hasFile("assets/textures#tcf_atc/texture.dat");
assertThat(shardZip).hasFile("assets/textures#tcf_etc1/texture.dat");
// Suffix stripping was NOT applied (because again we did not even activate texture targeting
// optimization).
assertThat(shardZip).doesNotHaveFile("assets/textures/texture.dat");
}
}
use of com.android.bundle.Targeting.AssetsDirectoryTargeting in project bundletool by google.
the class BuildApksManagerTest method splitNames_assetLanguages.
@Test
public void splitNames_assetLanguages() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.addFile("dex/classes.dex").addFile("assets/paks#lang_es/es.pak").addFile("assets/paks/en.pak").setManifest(androidManifest("com.test.app")).setAssetsConfig(assets(targetedAssetsDirectory("assets/paks#lang_es", assetsDirectoryTargeting(languageTargeting("es"))), targetedAssetsDirectory("assets/paks", assetsDirectoryTargeting(alternativeLanguageTargeting("es")))))).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).build());
buildApksManager.execute();
ZipFile apkSetFile = openZipFile(outputFilePath.toFile());
BuildApksResult result = extractTocFromApkSetFile(apkSetFile, outputDir);
assertThat(splitApkVariants(result)).hasSize(1);
Variant splitApkVariant = splitApkVariants(result).get(0);
assertThat(splitApkVariant.getApkSetList()).hasSize(1);
assertThat(apkNamesInVariant(splitApkVariant)).containsExactly("base-master.apk", "base-es.apk", "base-other_lang.apk");
}
use of com.android.bundle.Targeting.AssetsDirectoryTargeting in project bundletool by google.
the class BuildApksManagerTest method buildApksCommand_standalone_mixedTextureTargetingInDifferentPacksWithSameFolderNameAndNonFallbackDefault.
@Test
public void buildApksCommand_standalone_mixedTextureTargetingInDifferentPacksWithSameFolderNameAndNonFallbackDefault() throws Exception {
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.setManifest(androidManifest("com.test.app", withFusingAttribute(true)))).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(androidManifest("com.test.app", withDelivery(DeliveryType.INSTALL_TIME), withFusingAttribute(true)))).addModule("feature_assets_without_tcf", builder -> builder.addFile("assets/textures/another_untargeted_texture.dat").setAssetsConfig(assets(targetedAssetsDirectory("assets/textures", AssetsDirectoryTargeting.getDefaultInstance()))).setManifest(androidManifest("com.test.app", withDelivery(DeliveryType.INSTALL_TIME), withFusingAttribute(true)))).setBundleConfig(BundleConfigBuilder.create().addSplitDimension(Value.TEXTURE_COMPRESSION_FORMAT, /* negate= */
false, /* stripSuffix= */
true, /* defaultSuffix= */
"etc1").build()).build();
TestComponent.useTestModule(this, createTestModuleBuilder().withAppBundle(appBundle).withOutputPath(outputFilePath).withOptimizationDimensions(TEXTURE_COMPRESSION_FORMAT).build());
InvalidBundleException e = assertThrows(InvalidBundleException.class, () -> buildApksManager.execute());
assertThat(e).hasMessageThat().contains("Encountered conflicting targeting values while merging assets config.");
}
use of com.android.bundle.Targeting.AssetsDirectoryTargeting in project bundletool by google.
the class BuildApksManagerTest method buildApksCommand_standalone_mixedTextureTargetingWithSuffixStripped.
@Test
public void buildApksCommand_standalone_mixedTextureTargetingWithSuffixStripped() throws Exception {
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).withOptimizationDimensions(TEXTURE_COMPRESSION_FORMAT).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/textures/etc1_texture.dat");
assertThat(shardZip).doesNotHaveFile("assets/textures#tcf_etc1/etc1_texture.dat");
assertThat(shardZip).doesNotHaveFile("assets/textures/untargeted_texture.dat");
}
// Check that targeting was applied to both the APK and the variant
assertThat(standaloneApkVariants(result).get(0).getTargeting().getTextureCompressionFormatTargeting().getValueList()).containsExactly(textureCompressionFormat(ETC1_RGB8));
assertThat(shard.getTargeting().getTextureCompressionFormatTargeting().getValueList()).containsExactly(textureCompressionFormat(ETC1_RGB8));
}
Aggregations