use of com.android.bundle.Files.TargetedAssetsDirectory in project bundletool by google.
the class AssetsTargetingValidator method validateTargeting.
private void validateTargeting(BundleModule module, Assets assets) {
ImmutableSet<ZipPath> assetDirsWithFiles = getDirectoriesWithFiles(module);
for (TargetedAssetsDirectory targetedDirectory : assets.getDirectoryList()) {
ZipPath path = ZipPath.create(targetedDirectory.getPath());
if (!path.startsWith(ASSETS_DIRECTORY)) {
throw InvalidBundleException.builder().withUserMessage("Path of targeted assets directory must start with 'assets/' but found '%s'.", path).build();
}
if (!assetDirsWithFiles.contains(path)) {
throw InvalidBundleException.builder().withUserMessage("Targeted directory '%s' is empty.", path).build();
}
checkNoDimensionWithoutValuesAndAlternatives(targetedDirectory);
}
if (module.getModuleType().equals(ModuleType.ASSET_MODULE) && assets.getDirectoryList().stream().anyMatch(dir -> dir.getTargeting().hasLanguage())) {
throw InvalidBundleException.builder().withUserMessage("Language targeting for asset packs is not supported, but found in module %s.", module.getName().getName()).build();
}
}
use of com.android.bundle.Files.TargetedAssetsDirectory in project bundletool by google.
the class SuffixStripper method removeAssetsTargeting.
/**
* Updates the module to remove the specified targeting from the assets - both the directories in
* assets config and the associated module entries having the specified targeting will be updated.
*/
public ModuleSplit removeAssetsTargeting(ModuleSplit moduleSplit) {
if (!moduleSplit.getAssetsConfig().isPresent()) {
return moduleSplit;
}
// Update the targeted assets directory and their associated entries.
Assets assetsConfig = moduleSplit.getAssetsConfig().get();
Assets.Builder updatedAssetsConfig = assetsConfig.toBuilder().clearDirectory();
ImmutableList<ModuleEntry> updatedEntries = moduleSplit.getEntries();
for (TargetedAssetsDirectory targetedAssetsDirectory : assetsConfig.getDirectoryList()) {
TargetedAssetsDirectory updatedTargetedAssetsDirectory = removeAssetsTargetingFromDirectory(targetedAssetsDirectory);
// Remove the targeting from the entries path.
if (!updatedTargetedAssetsDirectory.equals(targetedAssetsDirectory)) {
// Update the associated entries
ZipPath directoryPath = ZipPath.create(targetedAssetsDirectory.getPath());
updatedEntries = updatedEntries.stream().map(entry -> {
if (entry.getPath().startsWith(directoryPath)) {
return removeTargetingFromEntry(entry);
}
return entry;
}).collect(toImmutableList());
}
updatedAssetsConfig.addDirectory(updatedTargetedAssetsDirectory);
}
return moduleSplit.toBuilder().setEntries(updatedEntries).setAssetsConfig(updatedAssetsConfig.build()).build();
}
use of com.android.bundle.Files.TargetedAssetsDirectory in project bundletool by google.
the class SuffixStripper method excludeAssetsTargetingOtherValue.
/**
* Updates the module to remove the specified targeting from the assets: both the assets in assets
* config and the associated entries having the specified targeting will be updated.
*/
private ModuleSplit excludeAssetsTargetingOtherValue(ModuleSplit moduleSplit, String value) {
if (!moduleSplit.getAssetsConfig().isPresent()) {
return moduleSplit;
}
// Update the targeted assets directory and their associated entries.
Assets assetsConfig = moduleSplit.getAssetsConfig().get();
Assets.Builder updatedAssetsConfig = assetsConfig.toBuilder().clearDirectory();
ImmutableList<ModuleEntry> updatedEntries = moduleSplit.getEntries();
for (TargetedAssetsDirectory targetedAssetsDirectory : assetsConfig.getDirectoryList()) {
ZipPath directoryPath = ZipPath.create(targetedAssetsDirectory.getPath());
// Check if the directory is targeted at this dimension, but for another value.
if (dimensionHandler.isDirectoryTargetingOtherValue(targetedAssetsDirectory, value)) {
// Removed the associated entries if so.
updatedEntries = updatedEntries.stream().filter(entry -> !entry.getPath().startsWith(directoryPath)).collect(toImmutableList());
} else {
// Keep the directory otherwise.
updatedAssetsConfig.addDirectory(targetedAssetsDirectory);
}
}
return moduleSplit.toBuilder().setEntries(updatedEntries).setAssetsConfig(updatedAssetsConfig.build()).build();
}
use of com.android.bundle.Files.TargetedAssetsDirectory in project bundletool by google.
the class SameTargetingMergerTest method assetsConfigMerging_mergeDisjointAssets.
@Test
public void assetsConfigMerging_mergeDisjointAssets() throws Exception {
ModuleSplit moduleSplit = createModuleSplitBuilder().setEntries(ImmutableList.of(createModuleEntryForFile("assets/some_assets/file.txt", DUMMY_CONTENT))).setApkTargeting(ApkTargeting.getDefaultInstance()).setAssetsConfig(Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/some_assets").build()).build()).build();
ModuleSplit moduleSplit2 = createModuleSplitBuilder().setEntries(ImmutableList.of(createModuleEntryForFile("assets/some_other_assets/file.txt", DUMMY_CONTENT))).setApkTargeting(ApkTargeting.getDefaultInstance()).setAssetsConfig(Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/some_other_assets").build()).build()).build();
ImmutableCollection<ModuleSplit> splits = new SameTargetingMerger().merge(ImmutableList.of(moduleSplit, moduleSplit2));
assertThat(splits).hasSize(1);
ModuleSplit masterSplit = splits.iterator().next();
assertThat(extractPaths(masterSplit.getEntries())).containsExactly("assets/some_assets/file.txt", "assets/some_other_assets/file.txt");
assertThat(masterSplit.getAssetsConfig()).isPresent();
assertThat(masterSplit.getAssetsConfig().get().getDirectoryList().stream().map(TargetedAssetsDirectory::getPath)).containsExactly("assets/some_assets", "assets/some_other_assets");
}
use of com.android.bundle.Files.TargetedAssetsDirectory in project bundletool by google.
the class ModuleSplitsToShardMergerTest method mergeSingleShard_mergeDisjointAssets.
@Test
public void mergeSingleShard_mergeDisjointAssets() throws Exception {
ModuleSplit baseModuleSplit = createModuleSplitBuilder().setEntries(ImmutableList.of(createModuleEntryForFile("assets/some_assets/file.txt", DUMMY_CONTENT))).setAssetsConfig(Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/some_assets").build()).build()).build();
ModuleSplit featureModuleSplit = createModuleSplitBuilder().setModuleName(BundleModuleName.create(FEATURE_MODULE_NAME)).setEntries(ImmutableList.of(createModuleEntryForFile("assets/some_other_assets/file.txt", DUMMY_CONTENT))).setAssetsConfig(Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/some_other_assets").build()).build()).build();
ModuleSplit merged = splitsToShardMerger.mergeSingleShard(ImmutableList.of(baseModuleSplit, featureModuleSplit), createCache());
assertThat(extractPaths(merged.getEntries())).containsExactly("assets/some_assets/file.txt", "assets/some_other_assets/file.txt");
assertThat(merged.getAssetsConfig()).isPresent();
assertThat(merged.getAssetsConfig().get().getDirectoryList().stream().map(TargetedAssetsDirectory::getPath)).containsExactly("assets/some_assets", "assets/some_other_assets");
}
Aggregations