Merge changes from topic "flag_application_manifest" into main
* changes:
Support multiple <application> or <uses-sdk> elements in manifest_*.py
Fix manifest_fixer.py warnings
diff --git a/scripts/manifest.py b/scripts/manifest.py
index 81f9c61..32603e8 100755
--- a/scripts/manifest.py
+++ b/scripts/manifest.py
@@ -23,9 +23,40 @@
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):
children = []
- for child in parent.childNodes:
+ for child in parent.childNodes:
if child.nodeType == minidom.Node.ELEMENT_NODE and \
child.tagName == tag_name:
children.append(child)
diff --git a/scripts/manifest_check.py b/scripts/manifest_check.py
index b101259..1e32d1d 100755
--- a/scripts/manifest_check.py
+++ b/scripts/manifest_check.py
@@ -25,10 +25,7 @@
import sys
from xml.dom import minidom
-from manifest import android_ns
-from manifest import get_children_with_tag
-from manifest import parse_manifest
-from manifest import write_xml
+from manifest import *
class ManifestMismatchError(Exception):
@@ -122,7 +119,7 @@
# handles module names specified in Android.bp properties. However not all
# <uses-library> entries in the manifest correspond to real modules: some of
# the optional libraries may be missing at build time. Therefor this script
- # accepts raw module names as spelled in Android.bp/Amdroid.mk and trims the
+ # accepts raw module names as spelled in Android.bp/Android.mk and trims the
# optional namespace part manually.
required = trim_namespace_parts(required)
optional = trim_namespace_parts(optional)
@@ -205,15 +202,9 @@
"""Extract <uses-library> tags from the manifest."""
manifest = parse_manifest(xml)
- elems = get_children_with_tag(manifest, 'application')
- if len(elems) > 1:
- raise RuntimeError('found multiple <application> tags')
- if not elems:
- return [], [], []
-
- application = elems[0]
-
- libs = get_children_with_tag(application, 'uses-library')
+ libs = [child
+ for application in get_or_create_applications(xml, manifest)
+ for child in get_children_with_tag(application, 'uses-library')]
required = [uses_library_name(x) for x in libs if uses_library_required(x)]
optional = [
@@ -266,7 +257,7 @@
manifest: manifest (either parsed XML or aapt dump of APK)
is_apk: if the manifest comes from an APK or an XML file
"""
- if is_apk: #pylint: disable=no-else-return
+ if is_apk: #pylint: disable=no-else-return
return extract_target_sdk_version_apk(manifest)
else:
return extract_target_sdk_version_xml(manifest)
@@ -376,7 +367,7 @@
# Create a status file that is empty on success, or contains an
# error message on failure. When exceptions are suppressed,
- # dexpreopt command command will check file size to determine if
+ # dexpreopt command will check file size to determine if
# the check has failed.
if args.enforce_uses_libraries_status:
with open(args.enforce_uses_libraries_status, 'w') as f:
@@ -386,7 +377,7 @@
if args.extract_target_sdk_version:
try:
print(extract_target_sdk_version(manifest, is_apk))
- except: #pylint: disable=bare-except
+ except: #pylint: disable=bare-except
# Failed; don't crash, return "any" SDK version. This will
# result in dexpreopt not adding any compatibility libraries.
print(10000)
diff --git a/scripts/manifest_check_test.py b/scripts/manifest_check_test.py
index 8003b3e..abe0d8b 100755
--- a/scripts/manifest_check_test.py
+++ b/scripts/manifest_check_test.py
@@ -44,8 +44,8 @@
class EnforceUsesLibrariesTest(unittest.TestCase):
"""Unit tests for add_extract_native_libs function."""
- def run_test(self, xml, apk, uses_libraries=[], optional_uses_libraries=[],
- missing_optional_uses_libraries=[]): #pylint: disable=dangerous-default-value
+ def run_test(self, xml, apk, uses_libraries=(), optional_uses_libraries=(),
+ missing_optional_uses_libraries=()): #pylint: disable=dangerous-default-value
doc = minidom.parseString(xml)
try:
relax = False
@@ -114,14 +114,14 @@
self.assertFalse(matches)
def test_missing_uses_library(self):
- xml = self.xml_tmpl % ('')
- apk = self.apk_tmpl % ('')
+ xml = self.xml_tmpl % ''
+ apk = self.apk_tmpl % ''
matches = self.run_test(xml, apk, uses_libraries=['foo'])
self.assertFalse(matches)
def test_missing_optional_uses_library(self):
- xml = self.xml_tmpl % ('')
- apk = self.apk_tmpl % ('')
+ xml = self.xml_tmpl % ''
+ apk = self.apk_tmpl % ''
matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
self.assertFalse(matches)
@@ -234,6 +234,32 @@
optional_uses_libraries=['//x/y/z:bar'])
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):
@@ -256,12 +282,12 @@
"targetSdkVersion:'%s'\n"
"uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n")
- def test_targert_sdk_version_28(self):
+ def test_target_sdk_version_28(self):
xml = self.xml_tmpl % '28'
apk = self.apk_tmpl % '28'
self.run_test(xml, apk, '28')
- def test_targert_sdk_version_29(self):
+ def test_target_sdk_version_29(self):
xml = self.xml_tmpl % '29'
apk = self.apk_tmpl % '29'
self.run_test(xml, apk, '29')
diff --git a/scripts/manifest_fixer.py b/scripts/manifest_fixer.py
index 58079aa..9847ad5 100755
--- a/scripts/manifest_fixer.py
+++ b/scripts/manifest_fixer.py
@@ -23,15 +23,7 @@
from xml.dom import minidom
-from manifest import android_ns
-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
-
+from manifest import *
def parse_args():
"""Parse commandline arguments."""
@@ -48,9 +40,9 @@
parser.add_argument('--library', dest='library', action='store_true',
help='manifest is for a static library')
parser.add_argument('--uses-library', dest='uses_libraries', action='append',
- help='specify additional <uses-library> tag to add. android:requred is set to true')
+ help='specify additional <uses-library> tag to add. android:required is set to true')
parser.add_argument('--optional-uses-library', dest='optional_uses_libraries', action='append',
- help='specify additional <uses-library> tag to add. android:requred is set to false')
+ help='specify additional <uses-library> tag to add. android:required is set to false')
parser.add_argument('--uses-non-sdk-api', dest='uses_non_sdk_api', action='store_true',
help='manifest is for a package built against the platform')
parser.add_argument('--logging-parent', dest='logging_parent', default='',
@@ -91,47 +83,33 @@
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):
+ for uses_sdk in get_or_create_uses_sdks(doc, manifest):
+ # Get or insert the minSdkVersion attribute. If it is already present, make
+ # sure it as least the requested value.
+ min_attr = uses_sdk.getAttributeNodeNS(android_ns, 'minSdkVersion')
+ if min_attr is None:
+ min_attr = doc.createAttributeNS(android_ns, 'android:minSdkVersion')
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 = '16'
+ uses_sdk.setAttributeNode(min_attr)
else:
- target_attr.value = target_sdk_version
- element.setAttributeNode(target_attr)
+ 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 = 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):
@@ -147,37 +125,27 @@
manifest = parse_manifest(doc)
logging_parent_key = 'android.content.pm.LOGGING_PARENT'
- elems = get_children_with_tag(manifest, 'application')
- application = elems[0] if len(elems) == 1 else None
- 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)
+ for application in get_or_create_applications(doc, manifest):
+ indent = get_indent(application.firstChild, 2)
- 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
+ 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
- # indented
- if last and last.nodeType != minidom.Node.TEXT_NODE:
- indent = get_indent(application.previousSibling, 1)
- application.appendChild(doc.createTextNode(indent))
+ 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
+
+ # 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):
@@ -192,42 +160,32 @@
"""
manifest = parse_manifest(doc)
- elems = get_children_with_tag(manifest, 'application')
- application = elems[0] if len(elems) == 1 else None
- 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)
+ for application in get_or_create_applications(doc, manifest):
+ indent = get_indent(application.firstChild, 2)
- 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
- if last is not None and last.nodeType != minidom.Node.TEXT_NODE:
- last = None
+ for name in new_uses_libraries:
+ if find_child_with_attribute(application, 'uses-library', android_ns,
+ '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:
- if find_child_with_attribute(application, 'uses-library', android_ns,
- 'name', name) is not None:
- # If the uses-library tag of the same 'name' attribute value exists,
- # respect it.
- continue
+ ul = doc.createElement('uses-library')
+ ul.setAttributeNS(android_ns, 'android:name', name)
+ ul.setAttributeNS(android_ns, 'android:required', str(required).lower())
- ul = doc.createElement('uses-library')
- ul.setAttributeNS(android_ns, 'android:name', name)
- ul.setAttributeNS(android_ns, 'android:required', str(required).lower())
+ application.insertBefore(doc.createTextNode(indent), last)
+ application.insertBefore(ul, last)
- application.insertBefore(doc.createTextNode(indent), last)
- application.insertBefore(ul, last)
-
- # align the closing tag with the opening tag if it's not
- # indented
- if application.lastChild.nodeType != minidom.Node.TEXT_NODE:
- indent = get_indent(application.previousSibling, 1)
- application.appendChild(doc.createTextNode(indent))
+ # align the closing tag with the opening tag if it's not
+ # indented
+ 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):
@@ -240,111 +198,63 @@
"""
manifest = parse_manifest(doc)
- elems = get_children_with_tag(manifest, 'application')
- application = elems[0] if len(elems) == 1 else None
- 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)
-
- attr = application.getAttributeNodeNS(android_ns, 'usesNonSdkApi')
- if attr is None:
- attr = doc.createAttributeNS(android_ns, 'android:usesNonSdkApi')
- attr.value = 'true'
- application.setAttributeNode(attr)
+ for application in get_or_create_applications(doc, manifest):
+ 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):
manifest = parse_manifest(doc)
- elems = get_children_with_tag(manifest, 'application')
- application = elems[0] if len(elems) == 1 else None
- 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)
-
- 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')
+ for application in get_or_create_applications(doc, manifest):
+ 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):
manifest = parse_manifest(doc)
- elems = get_children_with_tag(manifest, 'application')
- application = elems[0] if len(elems) == 1 else None
- 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)
-
- 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))
+ for application in get_or_create_applications(doc, manifest):
+ 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):
manifest = parse_manifest(doc)
- elems = get_children_with_tag(manifest, 'application')
- application = elems[0] if len(elems) == 1 else None
- 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)
+ for application in get_or_create_applications(doc, manifest):
+ attr = application.getAttributeNodeNS(android_ns, 'hasCode')
+ if attr is not None:
+ # Do nothing if the application already has a hasCode attribute.
+ continue
+ attr = doc.createAttributeNS(android_ns, 'android:hasCode')
+ attr.value = 'false'
+ application.setAttributeNode(attr)
- 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):
manifest = parse_manifest(doc)
- elems = get_children_with_tag(manifest, 'application')
- application = elems[0] if len(elems) == 1 else None
- 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)
+ for application in get_or_create_applications(doc, manifest):
+ attr = application.getAttributeNodeNS(android_ns, 'testOnly')
+ if attr is not None:
+ # Do nothing If the application already has a testOnly attribute.
+ continue
+ attr = doc.createAttributeNS(android_ns, 'android:testOnly')
+ attr.value = 'true'
+ application.setAttributeNode(attr)
- 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):
"""Replace the maxSdkVersion attribute value for permission and
@@ -364,6 +274,7 @@
if max_attr and max_attr.value == 'current':
max_attr.value = max_sdk_version
+
def override_placeholder_version(doc, new_version):
"""Replace the versionCode attribute value if it\'s currently
set to the placeholder version of 0.
@@ -374,9 +285,10 @@
"""
manifest = parse_manifest(doc)
version = manifest.getAttribute("android:versionCode")
- if (version == '0'):
+ if version == '0':
manifest.setAttribute("android:versionCode", new_version)
+
def main():
"""Program entry point."""
try:
@@ -427,5 +339,6 @@
print('error: ' + str(err), file=sys.stderr)
sys.exit(-1)
+
if __name__ == '__main__':
main()
diff --git a/scripts/manifest_fixer_test.py b/scripts/manifest_fixer_test.py
index 0a62b10..e4d8dc3 100755
--- a/scripts/manifest_fixer_test.py
+++ b/scripts/manifest_fixer_test.py
@@ -20,12 +20,13 @@
import sys
import unittest
from xml.dom import minidom
-import xml.etree.ElementTree as ET
+import xml.etree.ElementTree as ElementTree
import manifest_fixer
sys.dont_write_bytecode = True
+
class CompareVersionGtTest(unittest.TestCase):
"""Unit tests for compare_version_gt function."""
@@ -69,25 +70,24 @@
'%s'
'</manifest>\n')
- # pylint: disable=redefined-builtin
- def uses_sdk(self, min=None, target=None, extra=''):
+ def uses_sdk(self, min_sdk=None, target_sdk=None, extra=''):
attrs = ''
- if min:
- attrs += ' android:minSdkVersion="%s"' % (min)
- if target:
- attrs += ' android:targetSdkVersion="%s"' % (target)
+ if min_sdk:
+ attrs += ' android:minSdkVersion="%s"' % min_sdk
+ if target_sdk:
+ attrs += ' android:targetSdkVersion="%s"' % target_sdk
if extra:
attrs += ' ' + extra
- return ' <uses-sdk%s/>\n' % (attrs)
+ return ' <uses-sdk%s/>\n' % attrs
def assert_xml_equal(self, output, expected):
- self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
+ self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def test_no_uses_sdk(self):
"""Tests inserting a uses-sdk element into a manifest."""
manifest_input = self.manifest_tmpl % ''
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assert_xml_equal(output, expected)
@@ -95,7 +95,7 @@
"""Tests inserting a minSdkVersion attribute into a uses-sdk element."""
manifest_input = self.manifest_tmpl % ' <uses-sdk extra="foo"/>\n'
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28',
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28',
extra='extra="foo"')
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assert_xml_equal(output, expected)
@@ -103,64 +103,64 @@
def test_raise_min(self):
"""Tests inserting a minSdkVersion attribute into a uses-sdk element."""
- manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
+ manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assert_xml_equal(output, expected)
def test_raise(self):
"""Tests raising a minSdkVersion attribute."""
- manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
+ manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assert_xml_equal(output, expected)
def test_no_raise_min(self):
"""Tests a minSdkVersion that doesn't need raising."""
- manifest_input = self.manifest_tmpl % self.uses_sdk(min='28')
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
+ manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='28')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27')
output = self.raise_min_sdk_version_test(manifest_input, '27', '27', False)
self.assert_xml_equal(output, expected)
def test_raise_codename(self):
"""Tests raising a minSdkVersion attribute to a codename."""
- manifest_input = self.manifest_tmpl % self.uses_sdk(min='28')
- expected = self.manifest_tmpl % self.uses_sdk(min='P', target='P')
+ manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='28')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='P', target_sdk='P')
output = self.raise_min_sdk_version_test(manifest_input, 'P', 'P', False)
self.assert_xml_equal(output, expected)
def test_no_raise_codename(self):
"""Tests a minSdkVersion codename that doesn't need raising."""
- manifest_input = self.manifest_tmpl % self.uses_sdk(min='P')
- expected = self.manifest_tmpl % self.uses_sdk(min='P', target='28')
+ manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='P')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='P', target_sdk='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assert_xml_equal(output, expected)
def test_target(self):
"""Tests an existing targetSdkVersion is preserved."""
- manifest_input = self.manifest_tmpl % self.uses_sdk(min='26', target='27')
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
+ manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='26', target_sdk='27')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assert_xml_equal(output, expected)
def test_no_target(self):
"""Tests inserting targetSdkVersion when minSdkVersion exists."""
- manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='29')
+ manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='29')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assert_xml_equal(output, expected)
def test_target_no_min(self):
""""Tests inserting targetSdkVersion when minSdkVersion exists."""
- manifest_input = self.manifest_tmpl % self.uses_sdk(target='27')
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
+ manifest_input = self.manifest_tmpl % self.uses_sdk(target_sdk='27')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assert_xml_equal(output, expected)
@@ -168,23 +168,23 @@
"""Tests inserting targetSdkVersion when minSdkVersion does not exist."""
manifest_input = self.manifest_tmpl % ''
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='29')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='29')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assert_xml_equal(output, expected)
def test_library_no_target(self):
"""Tests inserting targetSdkVersion when minSdkVersion exists."""
- manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='16')
+ manifest_input = self.manifest_tmpl % self.uses_sdk(min_sdk='27')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='16')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
self.assert_xml_equal(output, expected)
def test_library_target_no_min(self):
"""Tests inserting targetSdkVersion when minSdkVersion exists."""
- manifest_input = self.manifest_tmpl % self.uses_sdk(target='27')
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
+ manifest_input = self.manifest_tmpl % self.uses_sdk(target_sdk='27')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='27')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
self.assert_xml_equal(output, expected)
@@ -192,7 +192,7 @@
"""Tests inserting targetSdkVersion when minSdkVersion does not exist."""
manifest_input = self.manifest_tmpl % ''
- expected = self.manifest_tmpl % self.uses_sdk(min='28', target='16')
+ expected = self.manifest_tmpl % self.uses_sdk(min_sdk='28', target_sdk='16')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
self.assert_xml_equal(output, expected)
@@ -228,12 +228,24 @@
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):
"""Unit tests for add_logging_parent function."""
def assert_xml_equal(self, output, expected):
- self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
+ self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def add_logging_parent_test(self, input_manifest, logging_parent=None):
doc = minidom.parseString(input_manifest)
@@ -253,8 +265,8 @@
attrs = ''
if logging_parent:
meta_text = ('<meta-data android:name="android.content.pm.LOGGING_PARENT" '
- 'android:value="%s"/>\n') % (logging_parent)
- attrs += ' <application>\n %s </application>\n' % (meta_text)
+ 'android:value="%s"/>\n') % logging_parent
+ attrs += ' <application>\n %s </application>\n' % meta_text
return attrs
@@ -277,7 +289,7 @@
"""Unit tests for add_uses_libraries function."""
def assert_xml_equal(self, output, expected):
- self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
+ self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest, new_uses_libraries):
doc = minidom.parseString(input_manifest)
@@ -289,18 +301,16 @@
manifest_tmpl = (
'<?xml version="1.0" encoding="utf-8"?>\n'
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
- ' <application>\n'
'%s'
- ' </application>\n'
'</manifest>\n')
def uses_libraries(self, name_required_pairs):
- ret = ''
+ ret = ' <application>\n'
for name, required in name_required_pairs:
ret += (
' <uses-library android:name="%s" android:required="%s"/>\n'
) % (name, required)
-
+ ret += ' </application>\n'
return ret
def test_empty(self):
@@ -361,12 +371,23 @@
output = self.run_test(manifest_input, ['foo', 'bar'])
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):
"""Unit tests for add_uses_libraries function."""
def assert_xml_equal(self, output, expected):
- self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
+ self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest):
doc = minidom.parseString(input_manifest)
@@ -378,11 +399,11 @@
manifest_tmpl = (
'<?xml version="1.0" encoding="utf-8"?>\n'
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
- ' <application%s/>\n'
+ ' %s\n'
'</manifest>\n')
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):
"""Empty new_uses_libraries must not touch the manifest."""
@@ -398,12 +419,19 @@
output = self.run_test(manifest_input)
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):
"""Unit tests for add_use_embedded_dex function."""
def assert_xml_equal(self, output, expected):
- self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
+ self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest):
doc = minidom.parseString(input_manifest)
@@ -415,14 +443,14 @@
manifest_tmpl = (
'<?xml version="1.0" encoding="utf-8"?>\n'
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
- ' <application%s/>\n'
+ ' %s\n'
'</manifest>\n')
def use_embedded_dex(self, value):
- return ' android:useEmbeddedDex="%s"' % value
+ return '<application android:useEmbeddedDex="%s" />' % value
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')
output = self.run_test(manifest_input)
self.assert_xml_equal(output, expected)
@@ -437,12 +465,24 @@
manifest_input = self.manifest_tmpl % self.use_embedded_dex('false')
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):
"""Unit tests for add_extract_native_libs function."""
def assert_xml_equal(self, output, expected):
- self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
+ self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest, value):
doc = minidom.parseString(input_manifest)
@@ -454,20 +494,20 @@
manifest_tmpl = (
'<?xml version="1.0" encoding="utf-8"?>\n'
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
- ' <application%s/>\n'
+ ' %s\n'
'</manifest>\n')
def extract_native_libs(self, value):
- return ' android:extractNativeLibs="%s"' % value
+ return '<application android:extractNativeLibs="%s" />' % value
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')
output = self.run_test(manifest_input, True)
self.assert_xml_equal(output, expected)
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')
output = self.run_test(manifest_input, False)
self.assert_xml_equal(output, expected)
@@ -482,12 +522,18 @@
manifest_input = self.manifest_tmpl % self.extract_native_libs('true')
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):
"""Unit tests for set_has_code_to_false function."""
def assert_xml_equal(self, output, expected):
- self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
+ self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest):
doc = minidom.parseString(input_manifest)
@@ -515,7 +561,7 @@
self.assert_xml_equal(output, expected)
def test_has_application_has_code_false(self):
- """ Do nothing if there's already an application elemeent. """
+ """ Do nothing if there's already an application element. """
manifest_input = self.manifest_tmpl % ' <application android:hasCode="false"/>\n'
output = self.run_test(manifest_input)
self.assert_xml_equal(output, manifest_input)
@@ -527,12 +573,25 @@
output = self.run_test(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):
"""Unit tests for set_test_only_flag_to_true function."""
def assert_xml_equal(self, output, expected):
- self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
+ self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest):
doc = minidom.parseString(input_manifest)
@@ -571,12 +630,26 @@
output = self.run_test(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):
"""Unit tests for set_max_sdk_version function."""
def assert_xml_equal(self, output, expected):
- self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
+ self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest, max_sdk_version):
doc = minidom.parseString(input_manifest)
@@ -591,15 +664,15 @@
'%s'
'</manifest>\n')
- def permission(self, max=None):
- if max is None:
+ def permission(self, max_sdk=None):
+ if max_sdk is None:
return ' <permission/>'
- return ' <permission android:maxSdkVersion="%s"/>\n' % max
+ return ' <permission android:maxSdkVersion="%s"/>\n' % max_sdk
- def uses_permission(self, max=None):
- if max is None:
+ def uses_permission(self, max_sdk=None):
+ if max_sdk is None:
return ' <uses-permission/>'
- return ' <uses-permission android:maxSdkVersion="%s"/>\n' % max
+ return ' <uses-permission android:maxSdkVersion="%s"/>\n' % max_sdk
def test_permission_no_max_sdk_version(self):
"""Tests if permission has no maxSdkVersion attribute"""
@@ -643,11 +716,12 @@
output = self.run_test(manifest_input, '9000')
self.assert_xml_equal(output, expected)
+
class OverrideDefaultVersionTest(unittest.TestCase):
"""Unit tests for override_default_version function."""
def assert_xml_equal(self, output, expected):
- self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))
+ self.assertEqual(ElementTree.canonicalize(output), ElementTree.canonicalize(expected))
def run_test(self, input_manifest, version):
doc = minidom.parseString(input_manifest)