Overriding placeholder version in updatable apks
Test: presubmit, checked the app version after build locally
Bug: 231691162
Change-Id: Icd242432540ea424235b226a45aac839dbc995be
diff --git a/scripts/manifest_fixer.py b/scripts/manifest_fixer.py
index 2da29ee..58079aa 100755
--- a/scripts/manifest_fixer.py
+++ b/scripts/manifest_fixer.py
@@ -70,6 +70,8 @@
parser.add_argument('--test-only', dest='test_only', action='store_true',
help=('adds testOnly="true" attribute to application. Assign true value if application elem '
'already has a testOnly attribute.'))
+ parser.add_argument('--override-placeholder-version', dest='new_version',
+ help='Overrides the versionCode if it\'s set to the placeholder value of 0')
parser.add_argument('input', help='input AndroidManifest.xml file')
parser.add_argument('output', help='output AndroidManifest.xml file')
return parser.parse_args()
@@ -362,6 +364,19 @@
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.
+
+ Args:
+ doc: The XML document. May be modified by this function.
+ new_version: The new version to set if versionCode is equal to 0.
+ """
+ manifest = parse_manifest(doc)
+ version = manifest.getAttribute("android:versionCode")
+ if (version == '0'):
+ manifest.setAttribute("android:versionCode", new_version)
+
def main():
"""Program entry point."""
try:
@@ -401,6 +416,9 @@
if args.extract_native_libs is not None:
add_extract_native_libs(doc, args.extract_native_libs)
+ if args.new_version:
+ override_placeholder_version(doc, args.new_version)
+
with open(args.output, 'w') as f:
write_xml(f, doc)
diff --git a/scripts/manifest_fixer_test.py b/scripts/manifest_fixer_test.py
index dad104a..0a62b10 100755
--- a/scripts/manifest_fixer_test.py
+++ b/scripts/manifest_fixer_test.py
@@ -643,5 +643,39 @@
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))
+
+ def run_test(self, input_manifest, version):
+ doc = minidom.parseString(input_manifest)
+ manifest_fixer.override_placeholder_version(doc, version)
+ output = io.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" '
+ 'android:versionCode="%s">\n'
+ '</manifest>\n')
+
+ def test_doesnt_override_existing_version(self):
+ """Tests that an existing version is not overridden"""
+ manifest_input = self.manifest_tmpl % '12345'
+ expected = manifest_input
+ output = self.run_test(manifest_input, '67890')
+ self.assert_xml_equal(output, expected)
+
+ def test_overrides_default_version(self):
+ """Tests that a default version is overridden"""
+ manifest_input = self.manifest_tmpl % '0'
+ expected = self.manifest_tmpl % '67890'
+ output = self.run_test(manifest_input, '67890')
+ self.assert_xml_equal(output, expected)
+
+
if __name__ == '__main__':
unittest.main(verbosity=2)