use of com.android.tools.build.bundletool.model.ZipPath in project bundletool by google.
the class ApexBundleValidator method validateModule.
@Override
public void validateModule(BundleModule module) {
if (module.findEntriesUnderPath(APEX_DIRECTORY).count() == 0) {
return;
}
Optional<ModuleEntry> apexManifest = module.getEntry(APEX_MANIFEST_PATH);
if (apexManifest.isPresent()) {
validateApexManifest(apexManifest.get());
} else {
apexManifest = module.getEntry(APEX_MANIFEST_JSON_PATH);
if (!apexManifest.isPresent()) {
throw InvalidBundleException.builder().withUserMessage("Missing expected file in APEX bundle: '%s' or '%s'.", APEX_MANIFEST_PATH, APEX_MANIFEST_JSON_PATH).build();
}
validateApexManifestJson(apexManifest.get());
}
ImmutableSet.Builder<String> apexImagesBuilder = ImmutableSet.builder();
ImmutableSet.Builder<String> apexBuildInfosBuilder = ImmutableSet.builder();
ImmutableSet.Builder<String> apexFileNamesBuilder = ImmutableSet.builder();
for (ModuleEntry entry : module.getEntries()) {
ZipPath path = entry.getPath();
if (path.startsWith(APEX_DIRECTORY)) {
if (path.getFileName().toString().endsWith(BundleModule.APEX_IMAGE_SUFFIX)) {
apexImagesBuilder.add(path.toString());
apexFileNamesBuilder.add(path.getFileName().toString());
} else if (path.getFileName().toString().endsWith(BundleModule.BUILD_INFO_SUFFIX)) {
apexBuildInfosBuilder.add(path.toString());
} else {
throw InvalidBundleException.builder().withUserMessage("Unexpected file in apex directory of bundle: '%s'.", entry.getPath()).build();
}
} else if (!ALLOWED_APEX_FILES_OUTSIDE_APEX_DIRECTORY.contains(path)) {
throw InvalidBundleException.builder().withUserMessage("Unexpected file in APEX bundle: '%s'.", entry.getPath()).build();
}
}
ImmutableSet<String> apexBuildInfos = apexBuildInfosBuilder.build();
ImmutableSet<String> apexImages = apexImagesBuilder.build();
ImmutableSet<ImmutableSet<AbiName>> allAbiNameSets = apexFileNamesBuilder.build().stream().map(ApexBundleValidator::abiNamesFromFile).collect(toImmutableSet());
if (allAbiNameSets.size() != apexImages.size()) {
throw InvalidBundleException.builder().withUserMessage("Every APEX image file must target a unique set of architectures, " + "but found multiple files that target the same set of architectures.").build();
}
ImmutableSet<String> expectedImages = apexBuildInfos.stream().map(f -> f.replace(BundleModule.BUILD_INFO_SUFFIX, BundleModule.APEX_IMAGE_SUFFIX)).collect(toImmutableSet());
if (!apexBuildInfos.isEmpty() && !expectedImages.equals(apexImages)) {
throw InvalidBundleException.builder().withUserMessage("If APEX build info is provided then one must be provided for each APEX image file:\n" + " Expected %s\n" + " Found %s.", expectedImages, apexImages).build();
}
if (REQUIRED_ONE_OF_ABI_SETS.stream().anyMatch(one_of -> one_of.stream().noneMatch(allAbiNameSets::contains))) {
throw InvalidBundleException.builder().withUserMessage("APEX bundle must contain one of %s.", Joiner.on(" and one of ").join(REQUIRED_ONE_OF_ABI_SETS)).build();
}
module.getApexConfig().ifPresent(targeting -> validateTargeting(apexImages, targeting));
}
use of com.android.tools.build.bundletool.model.ZipPath 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.tools.build.bundletool.model.ZipPath 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.tools.build.bundletool.model.ZipPath 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.tools.build.bundletool.model.ZipPath in project bundletool by google.
the class ResourceAnalyzer method findAllReferencedAppResources.
private ImmutableSet<ResourceId> findAllReferencedAppResources(Item item) {
switch(item.getValueCase()) {
case REF:
if (item.getRef().getId() != 0) {
return ImmutableSet.of(ResourceId.create(item.getRef().getId()));
}
// we don't need to consider it.
break;
case FILE:
FileReference fileRef = item.getFile();
if (!fileRef.getType().equals(FileReference.Type.PROTO_XML)) {
return ImmutableSet.of();
}
ZipPath xmlResourcePath = ZipPath.create(fileRef.getPath());
try (InputStream is = appBundle.getBaseModule().getEntry(xmlResourcePath).get().getContent().openStream()) {
XmlNode xmlRoot = XmlNode.parseFrom(is);
return findAllReferencedAppResources(xmlRoot);
} catch (InvalidProtocolBufferException e) {
throw CommandExecutionException.builder().withInternalMessage("Error parsing XML file '%s'.", xmlResourcePath).withCause(e).build();
} catch (IOException e) {
throw new UncheckedIOException(String.format("Failed to parse file '%s' in base module.", xmlResourcePath), e);
}
default:
break;
}
return ImmutableSet.of();
}
Aggregations