Search in sources :

Example 11 with XmlProtoElementBuilder

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();
}
Also used : Iterables(com.google.common.collect.Iterables) BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) XmlProtoAttributeBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder) BASE_MODULE_NAME(com.android.tools.build.bundletool.model.BundleModuleName.BASE_MODULE_NAME) Function(java.util.function.Function) Predicates.not(com.google.common.base.Predicates.not) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) XmlProtoNodeBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNodeBuilder) XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) MoreCollectors.toOptional(com.google.common.collect.MoreCollectors.toOptional) XmlProtoElementBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) CommandExecutionException(com.android.tools.build.bundletool.model.exceptions.CommandExecutionException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) SetMultimap(com.google.common.collect.SetMultimap) Sets(com.google.common.collect.Sets) Streams.stream(com.google.common.collect.Streams.stream) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) List(java.util.List) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) AutoValue(com.google.auto.value.AutoValue) Optional(java.util.Optional) ManifestEditor(com.android.tools.build.bundletool.model.ManifestEditor) Comparator(java.util.Comparator) XmlProtoElementBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder) ManifestEditor(com.android.tools.build.bundletool.model.ManifestEditor) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement)

Example 12 with XmlProtoElementBuilder

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);
}
Also used : ACTIVITY_ALIAS_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.ACTIVITY_ALIAS_ELEMENT_NAME) PERMISSION_TREE_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.PERMISSION_TREE_ELEMENT_NAME) Arrays(java.util.Arrays) ISOLATED_SPLITS_ID(com.android.tools.build.bundletool.model.AndroidManifest.ISOLATED_SPLITS_ID) VERSION_CODE_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.VERSION_CODE_RESOURCE_ID) INSTALL_LOCATION_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.INSTALL_LOCATION_ATTRIBUTE_NAME) TARGET_SANDBOX_VERSION_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.TARGET_SANDBOX_VERSION_RESOURCE_ID) Primitive(com.android.aapt.Resources.Primitive) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) EXTRACT_NATIVE_LIBS_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.EXTRACT_NATIVE_LIBS_ATTRIBUTE_NAME) XmlAttribute(com.android.aapt.Resources.XmlAttribute) ANDROID_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.ANDROID_NAMESPACE_URI) Item(com.android.aapt.Resources.Item) DeliveryType(com.android.bundle.Commands.DeliveryType) SUPPORTS_GL_TEXTURE_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.SUPPORTS_GL_TEXTURE_ELEMENT_NAME) PROVIDER_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.PROVIDER_ELEMENT_NAME) XmlNamespace(com.android.aapt.Resources.XmlNamespace) META_DATA_KEY_FUSED_MODULE_NAMES(com.android.tools.build.bundletool.model.AndroidManifest.META_DATA_KEY_FUSED_MODULE_NAMES) NAME_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.NAME_RESOURCE_ID) SDK_LIBRARY_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.SDK_LIBRARY_ELEMENT_NAME) SPLIT_NAME_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.SPLIT_NAME_ATTRIBUTE_NAME) MAX_SDK_VERSION_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.MAX_SDK_VERSION_ATTRIBUTE_NAME) SPLIT_NAME_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.SPLIT_NAME_RESOURCE_ID) XmlProtoElementBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder) Reference(com.android.aapt.Resources.Reference) VALUE_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.VALUE_RESOURCE_ID) VALUE_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.VALUE_ATTRIBUTE_NAME) VERSION_NAME_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.VERSION_NAME_ATTRIBUTE_NAME) NATIVE_ACTIVITY_LIB_NAME(com.android.tools.build.bundletool.model.AndroidManifest.NATIVE_ACTIVITY_LIB_NAME) VERSION_NAME_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.VERSION_NAME_RESOURCE_ID) Optional(java.util.Optional) PERMISSION_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.PERMISSION_ELEMENT_NAME) ICON_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.ICON_RESOURCE_ID) ICON_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.ICON_ATTRIBUTE_NAME) DEVICE_GROUP_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.DEVICE_GROUP_ELEMENT_NAME) RESOURCE_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.RESOURCE_RESOURCE_ID) RECEIVER_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.RECEIVER_ELEMENT_NAME) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode) XmlProtoAttributeBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder) PERMISSION_GROUP_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.PERMISSION_GROUP_ELEMENT_NAME) THEME_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.THEME_RESOURCE_ID) DISTRIBUTION_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.DISTRIBUTION_NAMESPACE_URI) Function(java.util.function.Function) META_DATA_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.META_DATA_ELEMENT_NAME) CONDITION_DEVICE_GROUPS_NAME(com.android.tools.build.bundletool.model.AndroidManifest.CONDITION_DEVICE_GROUPS_NAME) EXTRACT_NATIVE_LIBS_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.EXTRACT_NATIVE_LIBS_RESOURCE_ID) ImmutableList(com.google.common.collect.ImmutableList) ACTIVITY_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.ACTIVITY_ELEMENT_NAME) MODULE_TYPE_ASSET_VALUE(com.android.tools.build.bundletool.model.AndroidManifest.MODULE_TYPE_ASSET_VALUE) INSTALL_LOCATION_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.INSTALL_LOCATION_RESOURCE_ID) MAX_SDK_VERSION_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.MAX_SDK_VERSION_RESOURCE_ID) XmlProtoNodeBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNodeBuilder) Nullable(javax.annotation.Nullable) APPLICATION_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.APPLICATION_ELEMENT_NAME) USES_SDK_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.USES_SDK_ELEMENT_NAME) MIN_SDK_VERSION_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.MIN_SDK_VERSION_RESOURCE_ID) THEME_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.THEME_ATTRIBUTE_NAME) ObjectArrays(com.google.common.collect.ObjectArrays) MIN_SDK_VERSION_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.MIN_SDK_VERSION_ATTRIBUTE_NAME) TARGET_SDK_VERSION_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.TARGET_SDK_VERSION_RESOURCE_ID) SDK_MAJOR_VERSION_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.SDK_MAJOR_VERSION_ATTRIBUTE_NAME) NO_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.NO_NAMESPACE_URI) TARGET_SDK_VERSION_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.TARGET_SDK_VERSION_ATTRIBUTE_NAME) Consumer(java.util.function.Consumer) XmlNode(com.android.aapt.Resources.XmlNode) SERVICE_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.SERVICE_ELEMENT_NAME) HAS_CODE_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.HAS_CODE_RESOURCE_ID) XmlElement(com.android.aapt.Resources.XmlElement) DEBUGGABLE_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.DEBUGGABLE_ATTRIBUTE_NAME) Resources(com.android.aapt.Resources) NAME_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.NAME_ATTRIBUTE_NAME) DEBUGGABLE_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.DEBUGGABLE_RESOURCE_ID) XmlProtoElementBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder)

Aggregations

XmlProtoElementBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder)12 XmlProtoAttributeBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder)5 ImmutableList (com.google.common.collect.ImmutableList)5 XmlNode (com.android.aapt.Resources.XmlNode)4 XmlProtoNodeBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNodeBuilder)4 ImmutableSet (com.google.common.collect.ImmutableSet)4 Optional (java.util.Optional)4 AndroidManifest (com.android.tools.build.bundletool.model.AndroidManifest)3 Item (com.android.aapt.Resources.Item)2 ACTIVITY_ELEMENT_NAME (com.android.tools.build.bundletool.model.AndroidManifest.ACTIVITY_ELEMENT_NAME)2 ANDROID_NAMESPACE_URI (com.android.tools.build.bundletool.model.AndroidManifest.ANDROID_NAMESPACE_URI)2 APPLICATION_ELEMENT_NAME (com.android.tools.build.bundletool.model.AndroidManifest.APPLICATION_ELEMENT_NAME)2 EXTRACT_NATIVE_LIBS_ATTRIBUTE_NAME (com.android.tools.build.bundletool.model.AndroidManifest.EXTRACT_NATIVE_LIBS_ATTRIBUTE_NAME)2 EXTRACT_NATIVE_LIBS_RESOURCE_ID (com.android.tools.build.bundletool.model.AndroidManifest.EXTRACT_NATIVE_LIBS_RESOURCE_ID)2 HAS_CODE_RESOURCE_ID (com.android.tools.build.bundletool.model.AndroidManifest.HAS_CODE_RESOURCE_ID)2 ICON_ATTRIBUTE_NAME (com.android.tools.build.bundletool.model.AndroidManifest.ICON_ATTRIBUTE_NAME)2 ICON_RESOURCE_ID (com.android.tools.build.bundletool.model.AndroidManifest.ICON_RESOURCE_ID)2 XmlProtoNode (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode)2 Memoized (com.google.auto.value.extension.memoized.Memoized)2 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)2