Search in sources :

Example 1 with ManifestEditor

use of com.android.tools.build.bundletool.model.ManifestEditor in project bundletool by google.

the class ModuleSplitter method makeInstantManifestChanges.

/**
 * Updates the split to insert instant app specific manifest changes:
 *
 * <ul>
 *   <li>Sets the targetSandboxVersion to 2.
 *   <li>Sets the minSdkVersion to 21 if it the minSdkVersion is lower than 21.
 *   <li>Removes any known splits from the base manifest, which may contain on demand split
 *       information.
 * </ul>
 */
public ModuleSplit makeInstantManifestChanges(ModuleSplit moduleSplit) {
    AndroidManifest manifest = moduleSplit.getAndroidManifest();
    ManifestEditor editor = manifest.toEditor();
    editor.setTargetSandboxVersion(2);
    if (manifest.getEffectiveMinSdkVersion() < 21) {
        editor.setMinSdkVersion(21);
    }
    editor.removeUnknownSplitComponents(allModuleNames);
    return moduleSplit.toBuilder().setAndroidManifest(editor.save()).build();
}
Also used : ManifestEditor(com.android.tools.build.bundletool.model.ManifestEditor) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest)

Example 2 with ManifestEditor

use of com.android.tools.build.bundletool.model.ManifestEditor 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 3 with ManifestEditor

use of com.android.tools.build.bundletool.model.ManifestEditor in project bundletool by google.

the class ArchivedAndroidManifestUtils method createArchivedManifest.

public static AndroidManifest createArchivedManifest(AndroidManifest manifest) {
    checkNotNull(manifest);
    ManifestEditor editor = new ManifestEditor(createMinimalManifestTag(), BundleToolVersion.getCurrentVersion()).setPackage(manifest.getPackageName()).addMetaDataBoolean(META_DATA_KEY_ARCHIVED, true);
    manifest.getVersionCode().ifPresent(editor::setVersionCode);
    manifest.getVersionName().ifPresent(editor::setVersionName);
    manifest.getSharedUserId().ifPresent(editor::setSharedUserId);
    manifest.getSharedUserLabel().ifPresent(editor::setSharedUserLabel);
    manifest.getMinSdkVersion().ifPresent(editor::setMinSdkVersion);
    manifest.getMaxSdkVersion().ifPresent(editor::setMaxSdkVersion);
    manifest.getTargetSdkVersion().ifPresent(editor::setTargetSdkVersion);
    if (manifest.hasApplicationElement()) {
        manifest.getDescription().ifPresent(editor::setDescription);
        manifest.getHasFragileUserData().ifPresent(editor::setHasFragileUserData);
        manifest.getIsGame().ifPresent(editor::setIsGame);
        manifest.getIcon().ifPresent(editor::setIcon);
        if (manifest.hasLabelString()) {
            manifest.getLabelString().ifPresent(editor::setLabelAsString);
        }
        if (manifest.hasLabelRefId()) {
            manifest.getLabelRefId().ifPresent(editor::setLabelAsRefId);
        }
        getArchivedAllowBackup(manifest).ifPresent(editor::setAllowBackup);
        manifest.getFullBackupOnly().ifPresent(editor::setFullBackupOnly);
        manifest.getFullBackupContent().ifPresent(editor::setFullBackupContent);
        manifest.getDataExtractionRules().ifPresent(editor::setDataExtractionRules);
    }
    editor.copyPermissions(manifest);
    editor.copyPermissionGroups(manifest);
    editor.addActivity(createReactivateActivity());
    editor.addReceiver(createUpdateBroadcastReceiver());
    return editor.save();
}
Also used : ManifestEditor(com.android.tools.build.bundletool.model.ManifestEditor)

Aggregations

ManifestEditor (com.android.tools.build.bundletool.model.ManifestEditor)3 AndroidManifest (com.android.tools.build.bundletool.model.AndroidManifest)2 BundleModuleName (com.android.tools.build.bundletool.model.BundleModuleName)1 BASE_MODULE_NAME (com.android.tools.build.bundletool.model.BundleModuleName.BASE_MODULE_NAME)1 CommandExecutionException (com.android.tools.build.bundletool.model.exceptions.CommandExecutionException)1 XmlProtoAttributeBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoAttributeBuilder)1 XmlProtoElement (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElement)1 XmlProtoElementBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoElementBuilder)1 XmlProtoNodeBuilder (com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNodeBuilder)1 AutoValue (com.google.auto.value.AutoValue)1 Predicates.not (com.google.common.base.Predicates.not)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 MoreCollectors.toOptional (com.google.common.collect.MoreCollectors.toOptional)1