Support setting android:usesNonSdkApi in manifest_fixer.py

Add new command line flag to manifest_fixer.py which will add
'android:usesNonSdkApi="true"' attribute to the <application> tag.

Bug: 113315999
Test: build/soong/scripts/manifest_fixer.py
Change-Id: If030c90a4ced3f5c5176727c579a87d0ecab6cf8
This commit is contained in:
David Brazdil 2018-08-28 12:41:01 +01:00
parent 4bd15d36e7
commit d5b74996be
2 changed files with 66 additions and 0 deletions

View File

@ -53,6 +53,8 @@ def parse_args():
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')
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('input', help='input AndroidManifest.xml file')
parser.add_argument('output', help='output AndroidManifest.xml file')
return parser.parse_args()
@ -226,6 +228,33 @@ def add_uses_libraries(doc, new_uses_libraries):
indent = get_indent(application.previousSibling, 1)
application.appendChild(doc.createTextNode(indent))
def add_uses_non_sdk_api(doc):
"""Add android:usesNonSdkApi=true attribute to <application>.
Args:
doc: The XML document. May be modified by this function.
Raises:
RuntimeError: Invalid manifest
"""
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)
def write_xml(f, doc):
f.write('<?xml version="1.0" encoding="utf-8"?>\n')
@ -248,6 +277,9 @@ def main():
if args.uses_libraries:
add_uses_libraries(doc, args.uses_libraries)
if args.uses_non_sdk_api:
add_uses_non_sdk_api(doc)
with open(args.output, 'wb') as f:
write_xml(f, doc)

View File

@ -310,5 +310,39 @@ class AddUsesLibrariesTest(unittest.TestCase):
self.assertEqual(output, expected)
class AddUsesNonSdkApiTest(unittest.TestCase):
"""Unit tests for add_uses_libraries function."""
def run_test(self, input_manifest):
doc = minidom.parseString(input_manifest)
manifest_fixer.add_uses_non_sdk_api(doc)
output = StringIO.StringIO()
manifest_fixer.write_xml(output, doc)
return output.getvalue()
manifest_tmpl = (
'<?xml version="1.0" encoding="utf-8"?>\n'
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
' <application%s/>\n'
'</manifest>\n')
def uses_non_sdk_api(self, value):
return ' android:usesNonSdkApi="true"' if value else ''
def test_set_true(self):
"""Empty new_uses_libraries must not touch the manifest."""
manifest_input = self.manifest_tmpl % self.uses_non_sdk_api(False)
expected = self.manifest_tmpl % self.uses_non_sdk_api(True)
output = self.run_test(manifest_input)
self.assertEqual(output, expected)
def test_already_set(self):
"""new_uses_libraries must not overwrite existing tags."""
manifest_input = self.manifest_tmpl % self.uses_non_sdk_api(True)
expected = manifest_input
output = self.run_test(manifest_input)
self.assertEqual(output, expected)
if __name__ == '__main__':
unittest.main()