Merge "Touch up manifest if there's no source code."

This commit is contained in:
Jaewoong Jung
2019-06-06 13:52:37 +00:00
committed by Gerrit Code Review
7 changed files with 156 additions and 9 deletions

View File

@@ -59,6 +59,9 @@ def parse_args():
default=None, type=lambda x: (str(x).lower() == 'true'),
help=('specify if the app wants to use embedded native libraries. Must not conflict '
'if already declared in the manifest.'))
parser.add_argument('--has-no-code', dest='has_no_code', action='store_true',
help=('adds hasCode="false" attribute to application. Ignored if application elem '
'already has a hasCode attribute.'))
parser.add_argument('input', help='input AndroidManifest.xml file')
parser.add_argument('output', help='output AndroidManifest.xml file')
return parser.parse_args()
@@ -245,6 +248,28 @@ def add_extract_native_libs(doc, extract_native_libs):
(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)
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 main():
"""Program entry point."""
try:
@@ -269,6 +294,9 @@ def main():
if args.use_embedded_dex:
add_use_embedded_dex(doc)
if args.has_no_code:
set_has_code_to_false(doc)
if args.extract_native_libs is not None:
add_extract_native_libs(doc, args.extract_native_libs)

View File

@@ -424,5 +424,47 @@ class AddExtractNativeLibsTest(unittest.TestCase):
self.assertRaises(RuntimeError, self.run_test, manifest_input, False)
class AddNoCodeApplicationTest(unittest.TestCase):
"""Unit tests for set_has_code_to_false function."""
def run_test(self, input_manifest):
doc = minidom.parseString(input_manifest)
manifest_fixer.set_has_code_to_false(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'
'%s'
'</manifest>\n')
def test_no_application(self):
manifest_input = self.manifest_tmpl % ''
expected = self.manifest_tmpl % ' <application android:hasCode="false"/>\n'
output = self.run_test(manifest_input)
self.assertEqual(output, expected)
def test_has_application_no_has_code(self):
manifest_input = self.manifest_tmpl % ' <application/>\n'
expected = self.manifest_tmpl % ' <application android:hasCode="false"/>\n'
output = self.run_test(manifest_input)
self.assertEqual(output, expected)
def test_has_application_has_code_false(self):
""" Do nothing if there's already an application elemeent. """
manifest_input = self.manifest_tmpl % ' <application android:hasCode="false"/>\n'
output = self.run_test(manifest_input)
self.assertEqual(output, manifest_input)
def test_has_application_has_code_true(self):
""" Do nothing if there's already an application elemeent even if its
hasCode attribute is true. """
manifest_input = self.manifest_tmpl % ' <application android:hasCode="true"/>\n'
output = self.run_test(manifest_input)
self.assertEqual(output, manifest_input)
if __name__ == '__main__':
unittest.main(verbosity=2)