use of com.android.bundle.Files.Assets 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.Assets in project bundletool by google.
the class ModuleSplitterTest method pinSpecIsCopied.
@Test
public void pinSpecIsCopied() {
NativeLibraries nativeConfig = nativeLibraries(targetedNativeDirectory("lib/x86", nativeDirectoryTargeting("x86")));
BundleModule testModule = new BundleModuleBuilder("testModule").setManifest(androidManifest("com.test.app")).setNativeConfig(nativeConfig).addFile("lib/x86/liba.so").addFile("assets/com.android.hints.pins.txt").build();
ImmutableList<ModuleSplit> splits = createAbiAndDensitySplitter(testModule).splitModule();
assertThat(splits).hasSize(2);
assertThat(splits.stream().map((s) -> s.getEntries().stream().filter((e) -> e.getPath().endsWith(PinSpecInjector.PIN_SPEC_NAME)).count()).distinct()).containsExactly(1L);
}
use of com.android.bundle.Files.Assets in project bundletool by google.
the class BundleModuleTest method correctAssetsProtoFile_parsedAndReturned.
@Test
public void correctAssetsProtoFile_parsedAndReturned() throws Exception {
Assets assetsConfig = Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/data-armv6")).build();
BundleModule bundleModule = createMinimalModuleBuilder().addEntry(createModuleEntryForFile("assets.pb", assetsConfig.toByteArray())).build();
assertThat(bundleModule.getAssetsConfig()).hasValue(assetsConfig);
}
use of com.android.bundle.Files.Assets 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.Files.Assets in project bundletool by google.
the class TargetingGeneratorTest method generateTargetingForAssets_basicScenario.
@Test
public void generateTargetingForAssets_basicScenario() throws Exception {
Assets assetsConfig = new TargetingGenerator().generateTargetingForAssets(ImmutableList.of(ZipPath.create("assets/world/texture#tcf_etc1/i18n#lang_en"), ZipPath.create("assets/world/texture#tcf_etc1/i18n"), ZipPath.create("assets/world/texture#tcf_atc/i18n#lang_en"), ZipPath.create("assets/world/texture#tcf_atc/i18n#lang_ru")));
assertThat(assetsConfig).ignoringRepeatedFieldOrder().isEqualTo(Assets.newBuilder().addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/world/texture#tcf_etc1/i18n#lang_en").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(languageTargeting("en")), assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ETC1_RGB8, ImmutableSet.of(TextureCompressionFormatAlias.ATC)))))).addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/world/texture#tcf_etc1/i18n").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(alternativeLanguageTargeting("en")), assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ETC1_RGB8, ImmutableSet.of(TextureCompressionFormatAlias.ATC)))))).addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/world/texture#tcf_atc/i18n#lang_en").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(languageTargeting("en")), assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ATC, ImmutableSet.of(TextureCompressionFormatAlias.ETC1_RGB8)))))).addDirectory(TargetedAssetsDirectory.newBuilder().setPath("assets/world/texture#tcf_atc/i18n#lang_ru").setTargeting(mergeAssetsTargeting(assetsDirectoryTargeting(languageTargeting("ru")), assetsDirectoryTargeting(textureCompressionTargeting(TextureCompressionFormatAlias.ATC, ImmutableSet.of(TextureCompressionFormatAlias.ETC1_RGB8)))))).build());
}
Aggregations