Merge "Support setting android:usesNonSdkApi in manifest_fixer.py"
diff --git a/scripts/manifest_fixer.py b/scripts/manifest_fixer.py
index 25f96cd..db35c8d 100755
--- a/scripts/manifest_fixer.py
+++ b/scripts/manifest_fixer.py
@@ -53,6 +53,8 @@
                       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 @@
     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 @@
     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)
 
diff --git a/scripts/manifest_fixer_test.py b/scripts/manifest_fixer_test.py
index 5908997..ac72e6d 100755
--- a/scripts/manifest_fixer_test.py
+++ b/scripts/manifest_fixer_test.py
@@ -310,5 +310,39 @@
     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()