Search in sources :

Example 6 with XmlProtoElementBuilder

use of com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder 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)

Example 7 with XmlProtoElementBuilder

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

the class SplitsProtoXmlBuilder method build.

public XmlNode build() {
    XmlProtoElementBuilder splitsXml = XmlProtoElementBuilder.create("splits");
    for (String moduleName : splitsByModuleAndLanguage.rowKeySet()) {
        XmlProtoElementBuilder languageXml = XmlProtoElementBuilder.create("language");
        splitsByModuleAndLanguage.row(moduleName).forEach((language, splitId) -> languageXml.addChildElement(createEntry(language, splitId)));
        splitsXml.addChildElement(XmlProtoElementBuilder.create("module").addAttribute(XmlProtoAttributeBuilder.create("name").setValueAsString(moduleName)).addChildElement(languageXml));
    }
    return XmlProtoNode.createElementNode(splitsXml.build()).getProto();
}
Also used : XmlProtoElementBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder)

Example 8 with XmlProtoElementBuilder

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

the class LocaleConfigXmlInjector method createLocalesXmlNode.

public static XmlNode createLocalesXmlNode(ImmutableList<ModuleSplit> splits) {
    // Get all locales from the base module splits
    ImmutableSet<String> allLocales = getLocalesFromBaseModuleSplits(splits);
    XmlProtoElementBuilder localesConfigXml = XmlProtoElementBuilder.create(LOCALE_CONFIG_ELEMENT);
    allLocales.stream().filter(locale -> !locale.isEmpty()).forEach(locale -> localesConfigXml.addChildElement(createAttributes(locale)));
    return XmlProtoNode.createElementNode(localesConfigXml.build()).getProto();
}
Also used : XmlProtoElementBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder) ImmutableSet(com.google.common.collect.ImmutableSet) ConfigValue(com.android.aapt.Resources.ConfigValue) Value(com.android.aapt.Resources.Value) ZipPath(com.android.tools.build.bundletool.model.ZipPath) VariantKey(com.android.tools.build.bundletool.model.VariantKey) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode) XmlProtoAttributeBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder) Entry(com.android.aapt.Resources.Entry) Item(com.android.aapt.Resources.Item) FileReference(com.android.aapt.Resources.FileReference) XmlNode(com.android.aapt.Resources.XmlNode) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ResourceId(com.android.tools.build.bundletool.model.ResourceId) ImmutableList(com.google.common.collect.ImmutableList) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ResourceInjector(com.android.tools.build.bundletool.model.ResourceInjector) ByteSource(com.google.common.io.ByteSource) XmlProtoElementBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder)

Example 9 with XmlProtoElementBuilder

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

the class Activity method asXmlProtoElement.

@Memoized
public XmlProtoElement asXmlProtoElement() {
    XmlProtoElementBuilder elementBuilder = XmlProtoElementBuilder.create(ACTIVITY_ELEMENT_NAME);
    setNameAttribute(elementBuilder);
    setThemeAttribute(elementBuilder);
    setExportedAttribute(elementBuilder);
    setExcludeFromRecentsAttribute(elementBuilder);
    setStateNotNeeded(elementBuilder);
    setIntentFilterElement(elementBuilder);
    return elementBuilder.build();
}
Also used : XmlProtoElementBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder) Memoized(com.google.auto.value.extension.memoized.Memoized)

Example 10 with XmlProtoElementBuilder

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

the class SplitsProtoXmlBuilderTest method buildOneModule.

@Test
public void buildOneModule() {
    SplitsProtoXmlBuilder splitsProtoXmlBuilder = new SplitsProtoXmlBuilder();
    splitsProtoXmlBuilder.addLanguageMapping(BundleModuleName.create("module"), /* language = */
    "en", /* splitId = */
    "module.config.en");
    XmlNode rootNode = splitsProtoXmlBuilder.build();
    // Answer:
    // <splits>
    // <module name="module">
    // <language>
    // <entry key="en" split="module.config.en">
    // </language>
    // </module>
    // </splits>
    XmlProtoElementBuilder actual = XmlProtoElementBuilder.create("splits").addChildElement(XmlProtoElementBuilder.create("module").addAttribute(createAttribute("name", "module")).addChildElement(XmlProtoElementBuilder.create("language").addChildElement(XmlProtoElementBuilder.create("entry").addAttribute(createAttribute("key", "en")).addAttribute(createAttribute("split", "module.config.en")))));
    assertThat(rootNode).isEqualTo(getProto(actual));
}
Also used : XmlProtoElementBuilder(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder) XmlNode(com.android.aapt.Resources.XmlNode) Test(org.junit.Test)

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