Exemplo n.º 1
0
def raise_min_sdk_version(doc, min_sdk_version, target_sdk_version, library):
    """Ensure the manifest contains a <uses-sdk> tag with a minSdkVersion.

  Args:
    doc: The XML document.  May be modified by this function.
    min_sdk_version: The requested minSdkVersion attribute.
    target_sdk_version: The requested targetSdkVersion attribute.
    library: True if the manifest is for a library.
  Raises:
    RuntimeError: invalid manifest
  """

    manifest = parse_manifest(doc)

    # Get or insert the uses-sdk element
    uses_sdk = get_children_with_tag(manifest, 'uses-sdk')
    if len(uses_sdk) > 1:
        raise RuntimeError('found multiple uses-sdk elements')
    elif len(uses_sdk) == 1:
        element = uses_sdk[0]
    else:
        element = doc.createElement('uses-sdk')
        indent = get_indent(manifest.firstChild, 1)
        manifest.insertBefore(element, manifest.firstChild)

        # Insert an indent before uses-sdk to line it up with the indentation of the
        # other children of the <manifest> tag.
        manifest.insertBefore(doc.createTextNode(indent), manifest.firstChild)

    # Get or insert the minSdkVersion attribute.  If it is already present, make
    # sure it as least the requested value.
    min_attr = element.getAttributeNodeNS(android_ns, 'minSdkVersion')
    if min_attr is None:
        min_attr = doc.createAttributeNS(android_ns, 'android:minSdkVersion')
        min_attr.value = min_sdk_version
        element.setAttributeNode(min_attr)
    else:
        if compare_version_gt(min_sdk_version, min_attr.value):
            min_attr.value = min_sdk_version

    # Insert the targetSdkVersion attribute if it is missing.  If it is already
    # present leave it as is.
    target_attr = element.getAttributeNodeNS(android_ns, 'targetSdkVersion')
    if target_attr is None:
        target_attr = doc.createAttributeNS(android_ns,
                                            'android:targetSdkVersion')
        if library:
            # TODO(b/117122200): libraries shouldn't set targetSdkVersion at all, but
            # ManifestMerger treats minSdkVersion="Q" as targetSdkVersion="Q" if it
            # is empty.  Set it to something low so that it will be overriden by the
            # main manifest, but high enough that it doesn't cause implicit
            # permissions grants.
            target_attr.value = '15'
        else:
            target_attr.value = target_sdk_version
        element.setAttributeNode(target_attr)
Exemplo n.º 2
0
def construct_context(versioned_contexts, target_sdk):
    context = []
    for [sdk, ctx] in versioned_contexts:
        if sdk == any_sdk or compare_version_gt(sdk, target_sdk):
            context.append(ctx)
    return context