use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder in project bundletool by google.
the class FusingAndroidManifestMerger method mergeManifests.
private AndroidManifest mergeManifests(AndroidManifest baseManifest, List<AndroidManifest> featureManifests) {
// Gather all child elements of 'application' from all manifest. If element with the same name
// and type is presented in more than one manifest we give precedency to one in feature module.
// All feature manifests are sorted by feature module name in this method.
ImmutableListMultimap<ApplicationElementId, XmlProtoElement> applicationElements = gatherApplicationElementsManifests(ImmutableList.<AndroidManifest>builder().addAll(featureManifests).add(baseManifest).build(), elementsToMerge);
// This is optimization that allows to skip merging if there is no mergeable elements in
// feature modules.
long numberOfMergeableElementsInBase = baseManifest.getManifestRoot().getElement().getChildrenElements(AndroidManifest.APPLICATION_ELEMENT_NAME).flatMap(application -> application.getChildrenElements()).filter(element -> elementsToMerge.contains(element.getName())).count();
if (numberOfMergeableElementsInBase == applicationElements.size()) {
return baseManifest;
}
// Merge manifest elements with the same name and type based on specified mode.
ImmutableMap<ApplicationElementId, XmlProtoElement> mergedElements = applicationElements.keySet().stream().collect(toImmutableMap(Function.identity(), key -> mergeElements(key, applicationElements.get(key))));
ManifestEditor manifestEditor = baseManifest.toEditor();
XmlProtoElementBuilder applicationElement = manifestEditor.getRawProto().getOrCreateChildElement(AndroidManifest.APPLICATION_ELEMENT_NAME);
// Replace original elements from the base manifest with merged ones. This is done in a way to
// preserve original elements ordering and additional elements are added to the end.
Set<XmlProtoElement> replacedElements = Sets.newIdentityHashSet();
applicationElement.modifyChildElements(child -> stream(getCorrespondingElementFromMergedElements(child, mergedElements)).peek(replacedElements::add).map(element -> XmlProtoNodeBuilder.createElementNode(element.toBuilder())).collect(toOptional()).orElse(child));
mergedElements.values().stream().filter(not(replacedElements::contains)).forEach(element -> applicationElement.addChildElement(element.toBuilder()));
return manifestEditor.save();
}
use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder in project bundletool by google.
the class ManifestProtoUtils method withUserCountriesConditionInternal.
private static ManifestMutator withUserCountriesConditionInternal(ImmutableList<String> codes, Optional<Boolean> exclude) {
XmlProtoElementBuilder userCountries = XmlProtoElementBuilder.create(DISTRIBUTION_NAMESPACE_URI, "user-countries");
exclude.ifPresent(excludeValue -> userCountries.addAttribute(XmlProtoAttributeBuilder.create(DISTRIBUTION_NAMESPACE_URI, "exclude").setValueAsBoolean(excludeValue)));
for (String countryCode : codes) {
userCountries.addChildElement(XmlProtoElementBuilder.create(DISTRIBUTION_NAMESPACE_URI, "country").addAttribute(XmlProtoAttributeBuilder.create(DISTRIBUTION_NAMESPACE_URI, "code").setValueAsString(countryCode)));
}
return manifestElement -> manifestElement.getOrCreateChildElement(DISTRIBUTION_NAMESPACE_URI, "module").getOrCreateChildElement(DISTRIBUTION_NAMESPACE_URI, "delivery").getOrCreateChildElement(DISTRIBUTION_NAMESPACE_URI, "install-time").getOrCreateChildElement(DISTRIBUTION_NAMESPACE_URI, "conditions").addChildElement(userCountries);
}
Aggregations