Support multiple <application> or <uses-sdk> elements in manifest_*.py
Manifests may now have multiple copies of elements if they are disambiguated with android:featureFlag attributes. Remove the restrictions on duplicate elements from manifest_check.py and manifest_fixer.py, and instead iterate over all matching elements. Test: manifest_check_test.py, manifest_fixer_test.py Bug: 365170653 Flag: EXEMPT bugfix Change-Id: Ib577439d03a808a20a5fcc3e15a3117e0970d729
This commit is contained in:
@@ -23,6 +23,37 @@ from xml.dom import minidom
|
|||||||
android_ns = 'http://schemas.android.com/apk/res/android'
|
android_ns = 'http://schemas.android.com/apk/res/android'
|
||||||
|
|
||||||
|
|
||||||
|
def get_or_create_applications(doc, manifest):
|
||||||
|
"""Get all <application> tags from the manifest, or create one if none exist.
|
||||||
|
Multiple <application> tags may exist when manifest feature flagging is used.
|
||||||
|
"""
|
||||||
|
applications = get_children_with_tag(manifest, 'application')
|
||||||
|
if len(applications) == 0:
|
||||||
|
application = doc.createElement('application')
|
||||||
|
indent = get_indent(manifest.firstChild, 1)
|
||||||
|
first = manifest.firstChild
|
||||||
|
manifest.insertBefore(doc.createTextNode(indent), first)
|
||||||
|
manifest.insertBefore(application, first)
|
||||||
|
applications.append(application)
|
||||||
|
return applications
|
||||||
|
|
||||||
|
|
||||||
|
def get_or_create_uses_sdks(doc, manifest):
|
||||||
|
"""Get all <uses-sdk> tags from the manifest, or create one if none exist.
|
||||||
|
Multiple <uses-sdk> tags may exist when manifest feature flagging is used.
|
||||||
|
"""
|
||||||
|
uses_sdks = get_children_with_tag(manifest, 'uses-sdk')
|
||||||
|
if len(uses_sdks) == 0:
|
||||||
|
uses_sdk = doc.createElement('uses-sdk')
|
||||||
|
indent = get_indent(manifest.firstChild, 1)
|
||||||
|
manifest.insertBefore(uses_sdk, 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)
|
||||||
|
uses_sdks.append(uses_sdk)
|
||||||
|
return uses_sdks
|
||||||
|
|
||||||
def get_children_with_tag(parent, tag_name):
|
def get_children_with_tag(parent, tag_name):
|
||||||
children = []
|
children = []
|
||||||
for child in parent.childNodes:
|
for child in parent.childNodes:
|
||||||
|
@@ -25,10 +25,7 @@ import subprocess
|
|||||||
import sys
|
import sys
|
||||||
from xml.dom import minidom
|
from xml.dom import minidom
|
||||||
|
|
||||||
from manifest import android_ns
|
from manifest import *
|
||||||
from manifest import get_children_with_tag
|
|
||||||
from manifest import parse_manifest
|
|
||||||
from manifest import write_xml
|
|
||||||
|
|
||||||
|
|
||||||
class ManifestMismatchError(Exception):
|
class ManifestMismatchError(Exception):
|
||||||
@@ -205,15 +202,9 @@ def extract_uses_libs_xml(xml):
|
|||||||
"""Extract <uses-library> tags from the manifest."""
|
"""Extract <uses-library> tags from the manifest."""
|
||||||
|
|
||||||
manifest = parse_manifest(xml)
|
manifest = parse_manifest(xml)
|
||||||
elems = get_children_with_tag(manifest, 'application')
|
libs = [child
|
||||||
if len(elems) > 1:
|
for application in get_or_create_applications(xml, manifest)
|
||||||
raise RuntimeError('found multiple <application> tags')
|
for child in get_children_with_tag(application, 'uses-library')]
|
||||||
if not elems:
|
|
||||||
return [], [], []
|
|
||||||
|
|
||||||
application = elems[0]
|
|
||||||
|
|
||||||
libs = get_children_with_tag(application, 'uses-library')
|
|
||||||
|
|
||||||
required = [uses_library_name(x) for x in libs if uses_library_required(x)]
|
required = [uses_library_name(x) for x in libs if uses_library_required(x)]
|
||||||
optional = [
|
optional = [
|
||||||
|
@@ -234,6 +234,32 @@ class EnforceUsesLibrariesTest(unittest.TestCase):
|
|||||||
optional_uses_libraries=['//x/y/z:bar'])
|
optional_uses_libraries=['//x/y/z:bar'])
|
||||||
self.assertTrue(matches)
|
self.assertTrue(matches)
|
||||||
|
|
||||||
|
def test_multiple_applications(self):
|
||||||
|
xml = """<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<application android:featureFlag="foo">
|
||||||
|
<uses-library android:name="foo" />
|
||||||
|
<uses-library android:name="bar" android:required="false" />
|
||||||
|
</application>
|
||||||
|
<application android:featureFlag="!foo">
|
||||||
|
<uses-library android:name="foo" />
|
||||||
|
<uses-library android:name="qux" android:required="false" />
|
||||||
|
</application>
|
||||||
|
</manifest>
|
||||||
|
"""
|
||||||
|
apk = self.apk_tmpl % ('\n'.join([
|
||||||
|
uses_library_apk('foo'),
|
||||||
|
uses_library_apk('bar', required_apk(False)),
|
||||||
|
uses_library_apk('foo'),
|
||||||
|
uses_library_apk('qux', required_apk(False))
|
||||||
|
]))
|
||||||
|
matches = self.run_test(
|
||||||
|
xml,
|
||||||
|
apk,
|
||||||
|
uses_libraries=['//x/y/z:foo'],
|
||||||
|
optional_uses_libraries=['//x/y/z:bar', '//x/y/z:qux'])
|
||||||
|
self.assertTrue(matches)
|
||||||
|
|
||||||
|
|
||||||
class ExtractTargetSdkVersionTest(unittest.TestCase):
|
class ExtractTargetSdkVersionTest(unittest.TestCase):
|
||||||
|
|
||||||
|
@@ -23,15 +23,7 @@ import sys
|
|||||||
from xml.dom import minidom
|
from xml.dom import minidom
|
||||||
|
|
||||||
|
|
||||||
from manifest import android_ns
|
from manifest import *
|
||||||
from manifest import compare_version_gt
|
|
||||||
from manifest import ensure_manifest_android_ns
|
|
||||||
from manifest import find_child_with_attribute
|
|
||||||
from manifest import get_children_with_tag
|
|
||||||
from manifest import get_indent
|
|
||||||
from manifest import parse_manifest
|
|
||||||
from manifest import write_xml
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
"""Parse commandline arguments."""
|
"""Parse commandline arguments."""
|
||||||
@@ -91,47 +83,33 @@ def raise_min_sdk_version(doc, min_sdk_version, target_sdk_version, library):
|
|||||||
|
|
||||||
manifest = parse_manifest(doc)
|
manifest = parse_manifest(doc)
|
||||||
|
|
||||||
# Get or insert the uses-sdk element
|
for uses_sdk in get_or_create_uses_sdks(doc, manifest):
|
||||||
uses_sdk = get_children_with_tag(manifest, 'uses-sdk')
|
# Get or insert the minSdkVersion attribute. If it is already present, make
|
||||||
if len(uses_sdk) > 1:
|
# sure it as least the requested value.
|
||||||
raise RuntimeError('found multiple uses-sdk elements')
|
min_attr = uses_sdk.getAttributeNodeNS(android_ns, 'minSdkVersion')
|
||||||
elif len(uses_sdk) == 1:
|
if min_attr is None:
|
||||||
element = uses_sdk[0]
|
min_attr = doc.createAttributeNS(android_ns, 'android:minSdkVersion')
|
||||||
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
|
min_attr.value = min_sdk_version
|
||||||
|
uses_sdk.setAttributeNode(min_attr)
|
||||||
# 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 overridden by the
|
|
||||||
# main manifest, but high enough that it doesn't cause implicit
|
|
||||||
# permissions grants.
|
|
||||||
target_attr.value = '16'
|
|
||||||
else:
|
else:
|
||||||
target_attr.value = target_sdk_version
|
if compare_version_gt(min_sdk_version, min_attr.value):
|
||||||
element.setAttributeNode(target_attr)
|
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 = uses_sdk.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 overridden by the
|
||||||
|
# main manifest, but high enough that it doesn't cause implicit
|
||||||
|
# permissions grants.
|
||||||
|
target_attr.value = '16'
|
||||||
|
else:
|
||||||
|
target_attr.value = target_sdk_version
|
||||||
|
uses_sdk.setAttributeNode(target_attr)
|
||||||
|
|
||||||
|
|
||||||
def add_logging_parent(doc, logging_parent_value):
|
def add_logging_parent(doc, logging_parent_value):
|
||||||
@@ -147,37 +125,27 @@ def add_logging_parent(doc, logging_parent_value):
|
|||||||
manifest = parse_manifest(doc)
|
manifest = parse_manifest(doc)
|
||||||
|
|
||||||
logging_parent_key = 'android.content.pm.LOGGING_PARENT'
|
logging_parent_key = 'android.content.pm.LOGGING_PARENT'
|
||||||
elems = get_children_with_tag(manifest, 'application')
|
for application in get_or_create_applications(doc, manifest):
|
||||||
application = elems[0] if len(elems) == 1 else None
|
indent = get_indent(application.firstChild, 2)
|
||||||
if len(elems) > 1:
|
|
||||||
raise RuntimeError('found multiple <application> tags')
|
|
||||||
elif not elems:
|
|
||||||
application = doc.createElement('application')
|
|
||||||
indent = get_indent(manifest.firstChild, 1)
|
|
||||||
first = manifest.firstChild
|
|
||||||
manifest.insertBefore(doc.createTextNode(indent), first)
|
|
||||||
manifest.insertBefore(application, first)
|
|
||||||
|
|
||||||
indent = get_indent(application.firstChild, 2)
|
|
||||||
|
|
||||||
last = application.lastChild
|
|
||||||
if last is not None and last.nodeType != minidom.Node.TEXT_NODE:
|
|
||||||
last = None
|
|
||||||
|
|
||||||
if not find_child_with_attribute(application, 'meta-data', android_ns,
|
|
||||||
'name', logging_parent_key):
|
|
||||||
ul = doc.createElement('meta-data')
|
|
||||||
ul.setAttributeNS(android_ns, 'android:name', logging_parent_key)
|
|
||||||
ul.setAttributeNS(android_ns, 'android:value', logging_parent_value)
|
|
||||||
application.insertBefore(doc.createTextNode(indent), last)
|
|
||||||
application.insertBefore(ul, last)
|
|
||||||
last = application.lastChild
|
last = application.lastChild
|
||||||
|
if last is not None and last.nodeType != minidom.Node.TEXT_NODE:
|
||||||
|
last = None
|
||||||
|
|
||||||
# align the closing tag with the opening tag if it's not
|
if not find_child_with_attribute(application, 'meta-data', android_ns,
|
||||||
# indented
|
'name', logging_parent_key):
|
||||||
if last and last.nodeType != minidom.Node.TEXT_NODE:
|
ul = doc.createElement('meta-data')
|
||||||
indent = get_indent(application.previousSibling, 1)
|
ul.setAttributeNS(android_ns, 'android:name', logging_parent_key)
|
||||||
application.appendChild(doc.createTextNode(indent))
|
ul.setAttributeNS(android_ns, 'android:value', logging_parent_value)
|
||||||
|
application.insertBefore(doc.createTextNode(indent), last)
|
||||||
|
application.insertBefore(ul, last)
|
||||||
|
last = application.lastChild
|
||||||
|
|
||||||
|
# align the closing tag with the opening tag if it's not
|
||||||
|
# indented
|
||||||
|
if last and last.nodeType != minidom.Node.TEXT_NODE:
|
||||||
|
indent = get_indent(application.previousSibling, 1)
|
||||||
|
application.appendChild(doc.createTextNode(indent))
|
||||||
|
|
||||||
|
|
||||||
def add_uses_libraries(doc, new_uses_libraries, required):
|
def add_uses_libraries(doc, new_uses_libraries, required):
|
||||||
@@ -192,42 +160,32 @@ def add_uses_libraries(doc, new_uses_libraries, required):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
manifest = parse_manifest(doc)
|
manifest = parse_manifest(doc)
|
||||||
elems = get_children_with_tag(manifest, 'application')
|
for application in get_or_create_applications(doc, manifest):
|
||||||
application = elems[0] if len(elems) == 1 else None
|
indent = get_indent(application.firstChild, 2)
|
||||||
if len(elems) > 1:
|
|
||||||
raise RuntimeError('found multiple <application> tags')
|
|
||||||
elif not elems:
|
|
||||||
application = doc.createElement('application')
|
|
||||||
indent = get_indent(manifest.firstChild, 1)
|
|
||||||
first = manifest.firstChild
|
|
||||||
manifest.insertBefore(doc.createTextNode(indent), first)
|
|
||||||
manifest.insertBefore(application, first)
|
|
||||||
|
|
||||||
indent = get_indent(application.firstChild, 2)
|
last = application.lastChild
|
||||||
|
if last is not None and last.nodeType != minidom.Node.TEXT_NODE:
|
||||||
|
last = None
|
||||||
|
|
||||||
last = application.lastChild
|
for name in new_uses_libraries:
|
||||||
if last is not None and last.nodeType != minidom.Node.TEXT_NODE:
|
if find_child_with_attribute(application, 'uses-library', android_ns,
|
||||||
last = None
|
'name', name) is not None:
|
||||||
|
# If the uses-library tag of the same 'name' attribute value exists,
|
||||||
|
# respect it.
|
||||||
|
continue
|
||||||
|
|
||||||
for name in new_uses_libraries:
|
ul = doc.createElement('uses-library')
|
||||||
if find_child_with_attribute(application, 'uses-library', android_ns,
|
ul.setAttributeNS(android_ns, 'android:name', name)
|
||||||
'name', name) is not None:
|
ul.setAttributeNS(android_ns, 'android:required', str(required).lower())
|
||||||
# If the uses-library tag of the same 'name' attribute value exists,
|
|
||||||
# respect it.
|
|
||||||
continue
|
|
||||||
|
|
||||||
ul = doc.createElement('uses-library')
|
application.insertBefore(doc.createTextNode(indent), last)
|
||||||
ul.setAttributeNS(android_ns, 'android:name', name)
|
application.insertBefore(ul, last)
|
||||||
ul.setAttributeNS(android_ns, 'android:required', str(required).lower())
|
|
||||||
|
|
||||||
application.insertBefore(doc.createTextNode(indent), last)
|
# align the closing tag with the opening tag if it's not
|
||||||
application.insertBefore(ul, last)
|
# indented
|
||||||
|
if application.lastChild.nodeType != minidom.Node.TEXT_NODE:
|
||||||
# align the closing tag with the opening tag if it's not
|
indent = get_indent(application.previousSibling, 1)
|
||||||
# indented
|
application.appendChild(doc.createTextNode(indent))
|
||||||
if application.lastChild.nodeType != minidom.Node.TEXT_NODE:
|
|
||||||
indent = get_indent(application.previousSibling, 1)
|
|
||||||
application.appendChild(doc.createTextNode(indent))
|
|
||||||
|
|
||||||
|
|
||||||
def add_uses_non_sdk_api(doc):
|
def add_uses_non_sdk_api(doc):
|
||||||
@@ -240,112 +198,62 @@ def add_uses_non_sdk_api(doc):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
manifest = parse_manifest(doc)
|
manifest = parse_manifest(doc)
|
||||||
elems = get_children_with_tag(manifest, 'application')
|
for application in get_or_create_applications(doc, manifest):
|
||||||
application = elems[0] if len(elems) == 1 else None
|
attr = application.getAttributeNodeNS(android_ns, 'usesNonSdkApi')
|
||||||
if len(elems) > 1:
|
if attr is None:
|
||||||
raise RuntimeError('found multiple <application> tags')
|
attr = doc.createAttributeNS(android_ns, 'android:usesNonSdkApi')
|
||||||
elif not elems:
|
attr.value = 'true'
|
||||||
application = doc.createElement('application')
|
application.setAttributeNode(attr)
|
||||||
indent = get_indent(manifest.firstChild, 1)
|
|
||||||
first = manifest.firstChild
|
|
||||||
manifest.insertBefore(doc.createTextNode(indent), first)
|
|
||||||
manifest.insertBefore(application, first)
|
|
||||||
|
|
||||||
attr = application.getAttributeNodeNS(android_ns, 'usesNonSdkApi')
|
|
||||||
if attr is None:
|
|
||||||
attr = doc.createAttributeNS(android_ns, 'android:usesNonSdkApi')
|
|
||||||
attr.value = 'true'
|
|
||||||
application.setAttributeNode(attr)
|
|
||||||
|
|
||||||
|
|
||||||
def add_use_embedded_dex(doc):
|
def add_use_embedded_dex(doc):
|
||||||
manifest = parse_manifest(doc)
|
manifest = parse_manifest(doc)
|
||||||
elems = get_children_with_tag(manifest, 'application')
|
for application in get_or_create_applications(doc, manifest):
|
||||||
application = elems[0] if len(elems) == 1 else None
|
attr = application.getAttributeNodeNS(android_ns, 'useEmbeddedDex')
|
||||||
if len(elems) > 1:
|
if attr is None:
|
||||||
raise RuntimeError('found multiple <application> tags')
|
attr = doc.createAttributeNS(android_ns, 'android:useEmbeddedDex')
|
||||||
elif not elems:
|
attr.value = 'true'
|
||||||
application = doc.createElement('application')
|
application.setAttributeNode(attr)
|
||||||
indent = get_indent(manifest.firstChild, 1)
|
elif attr.value != 'true':
|
||||||
first = manifest.firstChild
|
raise RuntimeError('existing attribute mismatches the option of --use-embedded-dex')
|
||||||
manifest.insertBefore(doc.createTextNode(indent), first)
|
|
||||||
manifest.insertBefore(application, first)
|
|
||||||
|
|
||||||
attr = application.getAttributeNodeNS(android_ns, 'useEmbeddedDex')
|
|
||||||
if attr is None:
|
|
||||||
attr = doc.createAttributeNS(android_ns, 'android:useEmbeddedDex')
|
|
||||||
attr.value = 'true'
|
|
||||||
application.setAttributeNode(attr)
|
|
||||||
elif attr.value != 'true':
|
|
||||||
raise RuntimeError('existing attribute mismatches the option of --use-embedded-dex')
|
|
||||||
|
|
||||||
|
|
||||||
def add_extract_native_libs(doc, extract_native_libs):
|
def add_extract_native_libs(doc, extract_native_libs):
|
||||||
manifest = parse_manifest(doc)
|
manifest = parse_manifest(doc)
|
||||||
elems = get_children_with_tag(manifest, 'application')
|
for application in get_or_create_applications(doc, manifest):
|
||||||
application = elems[0] if len(elems) == 1 else None
|
value = str(extract_native_libs).lower()
|
||||||
if len(elems) > 1:
|
attr = application.getAttributeNodeNS(android_ns, 'extractNativeLibs')
|
||||||
raise RuntimeError('found multiple <application> tags')
|
if attr is None:
|
||||||
elif not elems:
|
attr = doc.createAttributeNS(android_ns, 'android:extractNativeLibs')
|
||||||
application = doc.createElement('application')
|
attr.value = value
|
||||||
indent = get_indent(manifest.firstChild, 1)
|
application.setAttributeNode(attr)
|
||||||
first = manifest.firstChild
|
elif attr.value != value:
|
||||||
manifest.insertBefore(doc.createTextNode(indent), first)
|
raise RuntimeError('existing attribute extractNativeLibs="%s" conflicts with --extract-native-libs="%s"' %
|
||||||
manifest.insertBefore(application, first)
|
(attr.value, value))
|
||||||
|
|
||||||
value = str(extract_native_libs).lower()
|
|
||||||
attr = application.getAttributeNodeNS(android_ns, 'extractNativeLibs')
|
|
||||||
if attr is None:
|
|
||||||
attr = doc.createAttributeNS(android_ns, 'android:extractNativeLibs')
|
|
||||||
attr.value = value
|
|
||||||
application.setAttributeNode(attr)
|
|
||||||
elif attr.value != value:
|
|
||||||
raise RuntimeError('existing attribute extractNativeLibs="%s" conflicts with --extract-native-libs="%s"' %
|
|
||||||
(attr.value, value))
|
|
||||||
|
|
||||||
|
|
||||||
def set_has_code_to_false(doc):
|
def set_has_code_to_false(doc):
|
||||||
manifest = parse_manifest(doc)
|
manifest = parse_manifest(doc)
|
||||||
elems = get_children_with_tag(manifest, 'application')
|
for application in get_or_create_applications(doc, manifest):
|
||||||
application = elems[0] if len(elems) == 1 else None
|
attr = application.getAttributeNodeNS(android_ns, 'hasCode')
|
||||||
if len(elems) > 1:
|
if attr is not None:
|
||||||
raise RuntimeError('found multiple <application> tags')
|
# Do nothing if the application already has a hasCode attribute.
|
||||||
elif not elems:
|
continue
|
||||||
application = doc.createElement('application')
|
attr = doc.createAttributeNS(android_ns, 'android:hasCode')
|
||||||
indent = get_indent(manifest.firstChild, 1)
|
attr.value = 'false'
|
||||||
first = manifest.firstChild
|
application.setAttributeNode(attr)
|
||||||
manifest.insertBefore(doc.createTextNode(indent), first)
|
|
||||||
manifest.insertBefore(application, first)
|
|
||||||
|
|
||||||
attr = application.getAttributeNodeNS(android_ns, 'hasCode')
|
|
||||||
if attr is not None:
|
|
||||||
# Do nothing if the application already has a hasCode attribute.
|
|
||||||
return
|
|
||||||
attr = doc.createAttributeNS(android_ns, 'android:hasCode')
|
|
||||||
attr.value = 'false'
|
|
||||||
application.setAttributeNode(attr)
|
|
||||||
|
|
||||||
|
|
||||||
def set_test_only_flag_to_true(doc):
|
def set_test_only_flag_to_true(doc):
|
||||||
manifest = parse_manifest(doc)
|
manifest = parse_manifest(doc)
|
||||||
elems = get_children_with_tag(manifest, 'application')
|
for application in get_or_create_applications(doc, manifest):
|
||||||
application = elems[0] if len(elems) == 1 else None
|
attr = application.getAttributeNodeNS(android_ns, 'testOnly')
|
||||||
if len(elems) > 1:
|
if attr is not None:
|
||||||
raise RuntimeError('found multiple <application> tags')
|
# Do nothing If the application already has a testOnly attribute.
|
||||||
elif not elems:
|
continue
|
||||||
application = doc.createElement('application')
|
attr = doc.createAttributeNS(android_ns, 'android:testOnly')
|
||||||
indent = get_indent(manifest.firstChild, 1)
|
attr.value = 'true'
|
||||||
first = manifest.firstChild
|
application.setAttributeNode(attr)
|
||||||
manifest.insertBefore(doc.createTextNode(indent), first)
|
|
||||||
manifest.insertBefore(application, first)
|
|
||||||
|
|
||||||
attr = application.getAttributeNodeNS(android_ns, 'testOnly')
|
|
||||||
if attr is not None:
|
|
||||||
# Do nothing If the application already has a testOnly attribute.
|
|
||||||
return
|
|
||||||
attr = doc.createAttributeNS(android_ns, 'android:testOnly')
|
|
||||||
attr.value = 'true'
|
|
||||||
application.setAttributeNode(attr)
|
|
||||||
|
|
||||||
|
|
||||||
def set_max_sdk_version(doc, max_sdk_version):
|
def set_max_sdk_version(doc, max_sdk_version):
|
||||||
|
@@ -228,6 +228,18 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assert_xml_equal(output, expected)
|
self.assert_xml_equal(output, expected)
|
||||||
|
|
||||||
|
def test_multiple_uses_sdks(self):
|
||||||
|
"""Tests a manifest that contains multiple uses_sdks elements."""
|
||||||
|
|
||||||
|
manifest_input = self.manifest_tmpl % (
|
||||||
|
' <uses-sdk android:featureFlag="foo" android:minSdkVersion="21" />\n'
|
||||||
|
' <uses-sdk android:featureFlag="!foo" android:minSdkVersion="22" />\n')
|
||||||
|
expected = self.manifest_tmpl % (
|
||||||
|
' <uses-sdk android:featureFlag="foo" android:minSdkVersion="28" android:targetSdkVersion="28" />\n'
|
||||||
|
' <uses-sdk android:featureFlag="!foo" android:minSdkVersion="28" android:targetSdkVersion="28" />\n')
|
||||||
|
|
||||||
|
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
|
||||||
|
self.assert_xml_equal(output, expected)
|
||||||
|
|
||||||
class AddLoggingParentTest(unittest.TestCase):
|
class AddLoggingParentTest(unittest.TestCase):
|
||||||
"""Unit tests for add_logging_parent function."""
|
"""Unit tests for add_logging_parent function."""
|
||||||
@@ -289,18 +301,16 @@ class AddUsesLibrariesTest(unittest.TestCase):
|
|||||||
manifest_tmpl = (
|
manifest_tmpl = (
|
||||||
'<?xml version="1.0" encoding="utf-8"?>\n'
|
'<?xml version="1.0" encoding="utf-8"?>\n'
|
||||||
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
|
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
|
||||||
' <application>\n'
|
|
||||||
'%s'
|
'%s'
|
||||||
' </application>\n'
|
|
||||||
'</manifest>\n')
|
'</manifest>\n')
|
||||||
|
|
||||||
def uses_libraries(self, name_required_pairs):
|
def uses_libraries(self, name_required_pairs):
|
||||||
ret = ''
|
ret = ' <application>\n'
|
||||||
for name, required in name_required_pairs:
|
for name, required in name_required_pairs:
|
||||||
ret += (
|
ret += (
|
||||||
' <uses-library android:name="%s" android:required="%s"/>\n'
|
' <uses-library android:name="%s" android:required="%s"/>\n'
|
||||||
) % (name, required)
|
) % (name, required)
|
||||||
|
ret += ' </application>\n'
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def test_empty(self):
|
def test_empty(self):
|
||||||
@@ -361,6 +371,17 @@ class AddUsesLibrariesTest(unittest.TestCase):
|
|||||||
output = self.run_test(manifest_input, ['foo', 'bar'])
|
output = self.run_test(manifest_input, ['foo', 'bar'])
|
||||||
self.assert_xml_equal(output, expected)
|
self.assert_xml_equal(output, expected)
|
||||||
|
|
||||||
|
def test_multiple_application(self):
|
||||||
|
"""When there are multiple applications, the libs are added to each."""
|
||||||
|
manifest_input = self.manifest_tmpl % (
|
||||||
|
self.uses_libraries([('foo', 'false')]) +
|
||||||
|
self.uses_libraries([('bar', 'false')]))
|
||||||
|
expected = self.manifest_tmpl % (
|
||||||
|
self.uses_libraries([('foo', 'false'), ('bar', 'true')]) +
|
||||||
|
self.uses_libraries([('bar', 'false'), ('foo', 'true')]))
|
||||||
|
output = self.run_test(manifest_input, ['foo', 'bar'])
|
||||||
|
self.assert_xml_equal(output, expected)
|
||||||
|
|
||||||
|
|
||||||
class AddUsesNonSdkApiTest(unittest.TestCase):
|
class AddUsesNonSdkApiTest(unittest.TestCase):
|
||||||
"""Unit tests for add_uses_libraries function."""
|
"""Unit tests for add_uses_libraries function."""
|
||||||
@@ -378,11 +399,11 @@ class AddUsesNonSdkApiTest(unittest.TestCase):
|
|||||||
manifest_tmpl = (
|
manifest_tmpl = (
|
||||||
'<?xml version="1.0" encoding="utf-8"?>\n'
|
'<?xml version="1.0" encoding="utf-8"?>\n'
|
||||||
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
|
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
|
||||||
' <application%s/>\n'
|
' %s\n'
|
||||||
'</manifest>\n')
|
'</manifest>\n')
|
||||||
|
|
||||||
def uses_non_sdk_api(self, value):
|
def uses_non_sdk_api(self, value):
|
||||||
return ' android:usesNonSdkApi="true"' if value else ''
|
return '<application %s/>' % ('android:usesNonSdkApi="true"' if value else '')
|
||||||
|
|
||||||
def test_set_true(self):
|
def test_set_true(self):
|
||||||
"""Empty new_uses_libraries must not touch the manifest."""
|
"""Empty new_uses_libraries must not touch the manifest."""
|
||||||
@@ -398,6 +419,13 @@ class AddUsesNonSdkApiTest(unittest.TestCase):
|
|||||||
output = self.run_test(manifest_input)
|
output = self.run_test(manifest_input)
|
||||||
self.assert_xml_equal(output, expected)
|
self.assert_xml_equal(output, expected)
|
||||||
|
|
||||||
|
def test_multiple_applications(self):
|
||||||
|
"""new_uses_libraries must be added to all applications."""
|
||||||
|
manifest_input = self.manifest_tmpl % (self.uses_non_sdk_api(True) + self.uses_non_sdk_api(False))
|
||||||
|
expected = self.manifest_tmpl % (self.uses_non_sdk_api(True) + self.uses_non_sdk_api(True))
|
||||||
|
output = self.run_test(manifest_input)
|
||||||
|
self.assert_xml_equal(output, expected)
|
||||||
|
|
||||||
|
|
||||||
class UseEmbeddedDexTest(unittest.TestCase):
|
class UseEmbeddedDexTest(unittest.TestCase):
|
||||||
"""Unit tests for add_use_embedded_dex function."""
|
"""Unit tests for add_use_embedded_dex function."""
|
||||||
@@ -415,14 +443,14 @@ class UseEmbeddedDexTest(unittest.TestCase):
|
|||||||
manifest_tmpl = (
|
manifest_tmpl = (
|
||||||
'<?xml version="1.0" encoding="utf-8"?>\n'
|
'<?xml version="1.0" encoding="utf-8"?>\n'
|
||||||
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
|
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
|
||||||
' <application%s/>\n'
|
' %s\n'
|
||||||
'</manifest>\n')
|
'</manifest>\n')
|
||||||
|
|
||||||
def use_embedded_dex(self, value):
|
def use_embedded_dex(self, value):
|
||||||
return ' android:useEmbeddedDex="%s"' % value
|
return '<application android:useEmbeddedDex="%s" />' % value
|
||||||
|
|
||||||
def test_manifest_with_undeclared_preference(self):
|
def test_manifest_with_undeclared_preference(self):
|
||||||
manifest_input = self.manifest_tmpl % ''
|
manifest_input = self.manifest_tmpl % '<application/>'
|
||||||
expected = self.manifest_tmpl % self.use_embedded_dex('true')
|
expected = self.manifest_tmpl % self.use_embedded_dex('true')
|
||||||
output = self.run_test(manifest_input)
|
output = self.run_test(manifest_input)
|
||||||
self.assert_xml_equal(output, expected)
|
self.assert_xml_equal(output, expected)
|
||||||
@@ -437,6 +465,18 @@ class UseEmbeddedDexTest(unittest.TestCase):
|
|||||||
manifest_input = self.manifest_tmpl % self.use_embedded_dex('false')
|
manifest_input = self.manifest_tmpl % self.use_embedded_dex('false')
|
||||||
self.assertRaises(RuntimeError, self.run_test, manifest_input)
|
self.assertRaises(RuntimeError, self.run_test, manifest_input)
|
||||||
|
|
||||||
|
def test_multiple_applications(self):
|
||||||
|
manifest_input = self.manifest_tmpl % (
|
||||||
|
self.use_embedded_dex('true') +
|
||||||
|
'<application/>'
|
||||||
|
)
|
||||||
|
expected = self.manifest_tmpl % (
|
||||||
|
self.use_embedded_dex('true') +
|
||||||
|
self.use_embedded_dex('true')
|
||||||
|
)
|
||||||
|
output = self.run_test(manifest_input)
|
||||||
|
self.assert_xml_equal(output, expected)
|
||||||
|
|
||||||
|
|
||||||
class AddExtractNativeLibsTest(unittest.TestCase):
|
class AddExtractNativeLibsTest(unittest.TestCase):
|
||||||
"""Unit tests for add_extract_native_libs function."""
|
"""Unit tests for add_extract_native_libs function."""
|
||||||
@@ -454,20 +494,20 @@ class AddExtractNativeLibsTest(unittest.TestCase):
|
|||||||
manifest_tmpl = (
|
manifest_tmpl = (
|
||||||
'<?xml version="1.0" encoding="utf-8"?>\n'
|
'<?xml version="1.0" encoding="utf-8"?>\n'
|
||||||
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
|
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
|
||||||
' <application%s/>\n'
|
' %s\n'
|
||||||
'</manifest>\n')
|
'</manifest>\n')
|
||||||
|
|
||||||
def extract_native_libs(self, value):
|
def extract_native_libs(self, value):
|
||||||
return ' android:extractNativeLibs="%s"' % value
|
return '<application android:extractNativeLibs="%s" />' % value
|
||||||
|
|
||||||
def test_set_true(self):
|
def test_set_true(self):
|
||||||
manifest_input = self.manifest_tmpl % ''
|
manifest_input = self.manifest_tmpl % '<application/>'
|
||||||
expected = self.manifest_tmpl % self.extract_native_libs('true')
|
expected = self.manifest_tmpl % self.extract_native_libs('true')
|
||||||
output = self.run_test(manifest_input, True)
|
output = self.run_test(manifest_input, True)
|
||||||
self.assert_xml_equal(output, expected)
|
self.assert_xml_equal(output, expected)
|
||||||
|
|
||||||
def test_set_false(self):
|
def test_set_false(self):
|
||||||
manifest_input = self.manifest_tmpl % ''
|
manifest_input = self.manifest_tmpl % '<application/>'
|
||||||
expected = self.manifest_tmpl % self.extract_native_libs('false')
|
expected = self.manifest_tmpl % self.extract_native_libs('false')
|
||||||
output = self.run_test(manifest_input, False)
|
output = self.run_test(manifest_input, False)
|
||||||
self.assert_xml_equal(output, expected)
|
self.assert_xml_equal(output, expected)
|
||||||
@@ -482,6 +522,12 @@ class AddExtractNativeLibsTest(unittest.TestCase):
|
|||||||
manifest_input = self.manifest_tmpl % self.extract_native_libs('true')
|
manifest_input = self.manifest_tmpl % self.extract_native_libs('true')
|
||||||
self.assertRaises(RuntimeError, self.run_test, manifest_input, False)
|
self.assertRaises(RuntimeError, self.run_test, manifest_input, False)
|
||||||
|
|
||||||
|
def test_multiple_applications(self):
|
||||||
|
manifest_input = self.manifest_tmpl % (self.extract_native_libs('true') + '<application/>')
|
||||||
|
expected = self.manifest_tmpl % (self.extract_native_libs('true') + self.extract_native_libs('true'))
|
||||||
|
output = self.run_test(manifest_input, True)
|
||||||
|
self.assert_xml_equal(output, expected)
|
||||||
|
|
||||||
|
|
||||||
class AddNoCodeApplicationTest(unittest.TestCase):
|
class AddNoCodeApplicationTest(unittest.TestCase):
|
||||||
"""Unit tests for set_has_code_to_false function."""
|
"""Unit tests for set_has_code_to_false function."""
|
||||||
@@ -527,6 +573,19 @@ class AddNoCodeApplicationTest(unittest.TestCase):
|
|||||||
output = self.run_test(manifest_input)
|
output = self.run_test(manifest_input)
|
||||||
self.assert_xml_equal(output, manifest_input)
|
self.assert_xml_equal(output, manifest_input)
|
||||||
|
|
||||||
|
def test_multiple_applications(self):
|
||||||
|
""" Apply to all applications """
|
||||||
|
manifest_input = self.manifest_tmpl % (
|
||||||
|
' <application android:hasCode="true" />\n' +
|
||||||
|
' <application android:hasCode="false" />\n' +
|
||||||
|
' <application/>\n')
|
||||||
|
expected = self.manifest_tmpl % (
|
||||||
|
' <application android:hasCode="true" />\n' +
|
||||||
|
' <application android:hasCode="false" />\n' +
|
||||||
|
' <application android:hasCode="false" />\n')
|
||||||
|
output = self.run_test(manifest_input)
|
||||||
|
self.assert_xml_equal(output, expected)
|
||||||
|
|
||||||
|
|
||||||
class AddTestOnlyApplicationTest(unittest.TestCase):
|
class AddTestOnlyApplicationTest(unittest.TestCase):
|
||||||
"""Unit tests for set_test_only_flag_to_true function."""
|
"""Unit tests for set_test_only_flag_to_true function."""
|
||||||
@@ -571,6 +630,20 @@ class AddTestOnlyApplicationTest(unittest.TestCase):
|
|||||||
output = self.run_test(manifest_input)
|
output = self.run_test(manifest_input)
|
||||||
self.assert_xml_equal(output, manifest_input)
|
self.assert_xml_equal(output, manifest_input)
|
||||||
|
|
||||||
|
def test_multiple_applications(self):
|
||||||
|
manifest_input = self.manifest_tmpl % (
|
||||||
|
' <application android:testOnly="true" />\n' +
|
||||||
|
' <application android:testOnly="false" />\n' +
|
||||||
|
' <application/>\n'
|
||||||
|
)
|
||||||
|
expected = self.manifest_tmpl % (
|
||||||
|
' <application android:testOnly="true" />\n' +
|
||||||
|
' <application android:testOnly="false" />\n' +
|
||||||
|
' <application android:testOnly="true" />\n'
|
||||||
|
)
|
||||||
|
output = self.run_test(manifest_input)
|
||||||
|
self.assert_xml_equal(output, expected)
|
||||||
|
|
||||||
|
|
||||||
class SetMaxSdkVersionTest(unittest.TestCase):
|
class SetMaxSdkVersionTest(unittest.TestCase):
|
||||||
"""Unit tests for set_max_sdk_version function."""
|
"""Unit tests for set_max_sdk_version function."""
|
||||||
|
Reference in New Issue
Block a user