Search in sources :

Example 1 with XmlProtoAttributeBuilder

use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder in project bundletool by google.

the class ManifestEditor method setMetadataValue.

private ManifestEditor setMetadataValue(String name, XmlProtoAttributeBuilder valueAttr) {
    XmlProtoElementBuilder applicationEl = manifestElement.getOrCreateChildElement(APPLICATION_ELEMENT_NAME);
    Optional<XmlProtoElementBuilder> existingMetadataEl = applicationEl.getChildrenElements(META_DATA_ELEMENT_NAME).filter(metadataEl -> metadataEl.getAndroidAttribute(NAME_RESOURCE_ID).map(nameAttr -> name.equals(nameAttr.getValueAsString())).orElse(false)).collect(toOptional());
    if (existingMetadataEl.isPresent()) {
        existingMetadataEl.get().removeAndroidAttribute(VALUE_RESOURCE_ID).addAttribute(valueAttr);
    } else {
        applicationEl.addChildElement(XmlProtoElementBuilder.create(META_DATA_ELEMENT_NAME).addAttribute(createAndroidAttribute("name", NAME_RESOURCE_ID).setValueAsString(name)).addAttribute(valueAttr));
    }
    return this;
}
Also used : VERSION_CODE_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.VERSION_CODE_RESOURCE_ID) DESCRIPTION_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.DESCRIPTION_RESOURCE_ID) SHARED_USER_ID_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.SHARED_USER_ID_RESOURCE_ID) TARGET_SANDBOX_VERSION_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.TARGET_SANDBOX_VERSION_RESOURCE_ID) EXTRACT_NATIVE_LIBS_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.EXTRACT_NATIVE_LIBS_ATTRIBUTE_NAME) DATA_EXTRACTION_RULES_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.DATA_EXTRACTION_RULES_RESOURCE_ID) ANDROID_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.ANDROID_NAMESPACE_URI) PROVIDER_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.PROVIDER_ELEMENT_NAME) 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) Version(com.android.tools.build.bundletool.model.version.Version) 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) ImmutableSet(com.google.common.collect.ImmutableSet) DATA_EXTRACTION_RULES_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.DATA_EXTRACTION_RULES_ATTRIBUTE_NAME) DESCRIPTION_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.DESCRIPTION_ATTRIBUTE_NAME) LABEL_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.LABEL_ATTRIBUTE_NAME) Collectors.joining(java.util.stream.Collectors.joining) VALUE_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.VALUE_RESOURCE_ID) FULL_BACKUP_CONTENT_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.FULL_BACKUP_CONTENT_ATTRIBUTE_NAME) LOCALE_CONFIG_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.LOCALE_CONFIG_RESOURCE_ID) FULL_BACKUP_ONLY_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.FULL_BACKUP_ONLY_RESOURCE_ID) VERSION_NAME_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.VERSION_NAME_ATTRIBUTE_NAME) HAS_FRAGILE_USER_DATA_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.HAS_FRAGILE_USER_DATA_ATTRIBUTE_NAME) LOCALE_CONFIG_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.LOCALE_CONFIG_ATTRIBUTE_NAME) VERSION_NAME_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.VERSION_NAME_RESOURCE_ID) IS_GAME_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.IS_GAME_RESOURCE_ID) Optional(java.util.Optional) ICON_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.ICON_RESOURCE_ID) LABEL_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.LABEL_RESOURCE_ID) ICON_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.ICON_ATTRIBUTE_NAME) RESOURCE_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.RESOURCE_RESOURCE_ID) Activity(com.android.tools.build.bundletool.model.manifestelements.Activity) CheckReturnValue(com.google.errorprone.annotations.CheckReturnValue) ALLOW_BACKUP_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.ALLOW_BACKUP_ATTRIBUTE_NAME) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode) XmlProtoAttributeBuilder.createAndroidAttribute(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder.createAndroidAttribute) XmlProtoAttributeBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder) META_DATA_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.META_DATA_ELEMENT_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) IS_GAME_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.IS_GAME_ATTRIBUTE_NAME) 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) APPLICATION_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.APPLICATION_ELEMENT_NAME) MoreCollectors.toOptional(com.google.common.collect.MoreCollectors.toOptional) 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) IS_FEATURE_SPLIT_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.IS_FEATURE_SPLIT_RESOURCE_ID) SHARED_USER_LABEL_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.SHARED_USER_LABEL_ATTRIBUTE_NAME) 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) Receiver(com.android.tools.build.bundletool.model.manifestelements.Receiver) FULL_BACKUP_CONTENT_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.FULL_BACKUP_CONTENT_RESOURCE_ID) NO_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.NO_NAMESPACE_URI) HAS_FRAGILE_USER_DATA_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.HAS_FRAGILE_USER_DATA_RESOURCE_ID) SHARED_USER_LABEL_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.SHARED_USER_LABEL_RESOURCE_ID) TARGET_SDK_VERSION_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.TARGET_SDK_VERSION_ATTRIBUTE_NAME) SHARED_USER_ID_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.SHARED_USER_ID_ATTRIBUTE_NAME) IS_SPLIT_REQUIRED_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.IS_SPLIT_REQUIRED_ATTRIBUTE_NAME) SERVICE_ELEMENT_NAME(com.android.tools.build.bundletool.model.AndroidManifest.SERVICE_ELEMENT_NAME) META_DATA_KEY_SPLITS_REQUIRED(com.android.tools.build.bundletool.model.AndroidManifest.META_DATA_KEY_SPLITS_REQUIRED) FULL_BACKUP_ONLY_ATTRIBUTE_NAME(com.android.tools.build.bundletool.model.AndroidManifest.FULL_BACKUP_ONLY_ATTRIBUTE_NAME) HAS_CODE_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.HAS_CODE_RESOURCE_ID) IS_SPLIT_REQUIRED_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.IS_SPLIT_REQUIRED_RESOURCE_ID) ALLOW_BACKUP_RESOURCE_ID(com.android.tools.build.bundletool.model.AndroidManifest.ALLOW_BACKUP_RESOURCE_ID) XmlProtoElementBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder)

Example 2 with XmlProtoAttributeBuilder

use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder in project bundletool by google.

the class FusingAndroidManifestMerger method mergeElements.

/**
 * Merges element with the same name and type from different manifests. {@code elements} list in
 * this method contains data from feature modules first and element from the base module is the
 * last one in the list.
 *
 * <p>If element is presented in more than one feature module elements are sorted by name of
 * feature module. Example: if service with name 'myService' is defined in base module and
 * features 'a' and 'b', {@code elements} list will contain its declaration in the following
 * order: 'a' feature, 'b' feature, base.
 */
private XmlProtoElement mergeElements(ApplicationElementId elementId, List<XmlProtoElement> elements) {
    // we just take the first element from the list.
    if (mode.equals(Mode.REPLACE) || elements.size() == 1) {
        return elements.get(0);
    }
    // Remove source data from nested elements as this data is meaningless for functionality and
    // just contains information about line/column where element appeared in the original xml.
    List<XmlProtoElement> elementsNoSource = elements.stream().map(element -> element.toBuilder().removeSourceDataRecursive().build()).collect(toImmutableList());
    // For intent filters we gather all distinct filters defined in all modules.
    Set<XmlProtoElement> intentFilters = elementsNoSource.stream().flatMap(element -> element.getChildrenElements(AndroidManifest.INTENT_FILTER_ELEMENT_NAME)).collect(toImmutableSet());
    // For meta-data we group them by name and take one per each name.
    ImmutableMap<String, XmlProtoElement> metadataByName = elementsNoSource.stream().flatMap(element -> element.getChildrenElements(AndroidManifest.META_DATA_ELEMENT_NAME)).filter(meta -> meta.getAndroidAttribute(AndroidManifest.NAME_RESOURCE_ID).isPresent()).collect(toImmutableMap(meta -> meta.getAndroidAttribute(AndroidManifest.NAME_RESOURCE_ID).get().getValueAsString(), Function.identity(), (a, b) -> {
        // different modules throw conflict exception.
        if (!a.equals(b)) {
            throw CommandExecutionException.builder().withInternalMessage("Multiple meta-data entries with the same name are found inside" + " %s:%s: %s, %s", elementId.getType(), elementId.getName(), a, b).build();
        }
        return a;
    }));
    // Take element declaration from feature module and add all intent filters and meta data to it.
    XmlProtoElementBuilder builder = elementsNoSource.get(0).toBuilder();
    builder.removeChildrenElementsIf(child -> {
        if (!child.isElement()) {
            return false;
        }
        XmlProtoElementBuilder childElement = child.getElement();
        String tag = childElement.getName();
        Optional<String> name = childElement.getAndroidAttribute(AndroidManifest.NAME_RESOURCE_ID).map(XmlProtoAttributeBuilder::getValueAsString);
        return tag.equals(AndroidManifest.INTENT_FILTER_ELEMENT_NAME) || (tag.equals(AndroidManifest.META_DATA_ELEMENT_NAME) && name.map(metadataByName::containsKey).orElse(false));
    });
    intentFilters.forEach(e -> builder.addChildElement(e.toBuilder()));
    metadataByName.values().forEach(e -> builder.addChildElement(e.toBuilder()));
    return builder.build();
}
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) XmlProtoElement(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement) XmlProtoAttributeBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder)

Aggregations

XmlProtoAttributeBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder)2 XmlProtoElementBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder)2 XmlProtoNodeBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNodeBuilder)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 MoreCollectors.toOptional (com.google.common.collect.MoreCollectors.toOptional)2 Optional (java.util.Optional)2 AndroidManifest (com.android.tools.build.bundletool.model.AndroidManifest)1 ACTIVITY_ELEMENT_NAME (com.android.tools.build.bundletool.model.AndroidManifest.ACTIVITY_ELEMENT_NAME)1 ALLOW_BACKUP_ATTRIBUTE_NAME (com.android.tools.build.bundletool.model.AndroidManifest.ALLOW_BACKUP_ATTRIBUTE_NAME)1 ALLOW_BACKUP_RESOURCE_ID (com.android.tools.build.bundletool.model.AndroidManifest.ALLOW_BACKUP_RESOURCE_ID)1 ANDROID_NAMESPACE_URI (com.android.tools.build.bundletool.model.AndroidManifest.ANDROID_NAMESPACE_URI)1 APPLICATION_ELEMENT_NAME (com.android.tools.build.bundletool.model.AndroidManifest.APPLICATION_ELEMENT_NAME)1 DATA_EXTRACTION_RULES_ATTRIBUTE_NAME (com.android.tools.build.bundletool.model.AndroidManifest.DATA_EXTRACTION_RULES_ATTRIBUTE_NAME)1 DATA_EXTRACTION_RULES_RESOURCE_ID (com.android.tools.build.bundletool.model.AndroidManifest.DATA_EXTRACTION_RULES_RESOURCE_ID)1 DESCRIPTION_ATTRIBUTE_NAME (com.android.tools.build.bundletool.model.AndroidManifest.DESCRIPTION_ATTRIBUTE_NAME)1 DESCRIPTION_RESOURCE_ID (com.android.tools.build.bundletool.model.AndroidManifest.DESCRIPTION_RESOURCE_ID)1 EXTRACT_NATIVE_LIBS_ATTRIBUTE_NAME (com.android.tools.build.bundletool.model.AndroidManifest.EXTRACT_NATIVE_LIBS_ATTRIBUTE_NAME)1 EXTRACT_NATIVE_LIBS_RESOURCE_ID (com.android.tools.build.bundletool.model.AndroidManifest.EXTRACT_NATIVE_LIBS_RESOURCE_ID)1 FULL_BACKUP_CONTENT_ATTRIBUTE_NAME (com.android.tools.build.bundletool.model.AndroidManifest.FULL_BACKUP_CONTENT_ATTRIBUTE_NAME)1