Make manifest_fixer.py keep targetSdkVersion high with min_sdk_version

am: 7b59e7b2d0

Change-Id: I5acccdf24e02d3d06b0c76107af363076f8b57ff
This commit is contained in:
Colin Cross 2018-09-10 17:07:27 -07:00 committed by android-build-merger
commit 6d54d291c8
3 changed files with 46 additions and 35 deletions

View file

@ -25,10 +25,14 @@ import (
var manifestFixerRule = pctx.AndroidStaticRule("manifestFixer",
blueprint.RuleParams{
Command: `${config.ManifestFixerCmd} --minSdkVersion ${minSdkVersion} $args $in $out`,
Command: `${config.ManifestFixerCmd} ` +
`--minSdkVersion ${minSdkVersion} ` +
`--targetSdkVersion ${targetSdkVersion} ` +
`--raise-min-sdk-version ` +
`$args $in $out`,
CommandDeps: []string{"${config.ManifestFixerCmd}"},
},
"minSdkVersion", "args")
"minSdkVersion", "targetSdkVersion", "args")
var manifestMergerRule = pctx.AndroidStaticRule("manifestMerger",
blueprint.RuleParams{
@ -53,8 +57,9 @@ func manifestMerger(ctx android.ModuleContext, manifest android.Path, sdkContext
Input: manifest,
Output: fixedManifest,
Args: map[string]string{
"minSdkVersion": sdkVersionOrDefault(ctx, sdkContext.minSdkVersion()),
"args": strings.Join(args, " "),
"minSdkVersion": sdkVersionOrDefault(ctx, sdkContext.minSdkVersion()),
"targetSdkVersion": sdkVersionOrDefault(ctx, sdkContext.sdkVersion()),
"args": strings.Join(args, " "),
},
})
manifest = fixedManifest

View file

@ -49,6 +49,10 @@ def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--minSdkVersion', default='', dest='min_sdk_version',
help='specify minSdkVersion used by the build system')
parser.add_argument('--targetSdkVersion', default='', dest='target_sdk_version',
help='specify targetSdkVersion used by the build system')
parser.add_argument('--raise-min-sdk-version', dest='raise_min_sdk_version', action='store_true',
help='raise the minimum sdk version in the manifest if necessary')
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',
@ -130,12 +134,13 @@ def get_indent(element, default_level):
return indent
def raise_min_sdk_version(doc, requested, library):
def raise_min_sdk_version(doc, min_sdk_version, target_sdk_version, library):
"""Ensure the manifest contains a <uses-sdk> tag with a minSdkVersion.
Args:
doc: The XML document. May be modified by this function.
requested: The requested minSdkVersion attribute.
min_sdk_version: The requested minSdkVersion attribute.
target_sdk_version: The requested targetSdkVersion attribute.
Raises:
RuntimeError: invalid manifest
"""
@ -162,11 +167,11 @@ def raise_min_sdk_version(doc, requested, library):
min_attr = element.getAttributeNodeNS(android_ns, 'minSdkVersion')
if min_attr is None:
min_attr = doc.createAttributeNS(android_ns, 'android:minSdkVersion')
min_attr.value = requested
min_attr.value = min_sdk_version
element.setAttributeNode(min_attr)
else:
if compare_version_gt(requested, min_attr.value):
min_attr.value = requested
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.
@ -176,7 +181,7 @@ def raise_min_sdk_version(doc, requested, library):
if library:
target_attr.value = '1'
else:
target_attr.value = requested
target_attr.value = target_sdk_version
element.setAttributeNode(target_attr)
@ -271,8 +276,8 @@ def main():
ensure_manifest_android_ns(doc)
if args.min_sdk_version:
raise_min_sdk_version(doc, args.min_sdk_version, args.library)
if args.raise_min_sdk_version:
raise_min_sdk_version(doc, args.min_sdk_version, args.target_sdk_version, args.library)
if args.uses_libraries:
add_uses_libraries(doc, args.uses_libraries)

View file

@ -55,9 +55,10 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
"""Unit tests for raise_min_sdk_version function."""
def raise_min_sdk_version_test(self, input_manifest, min_sdk_version,
library):
target_sdk_version, library):
doc = minidom.parseString(input_manifest)
manifest_fixer.raise_min_sdk_version(doc, min_sdk_version, library)
manifest_fixer.raise_min_sdk_version(doc, min_sdk_version,
target_sdk_version, library)
output = StringIO.StringIO()
manifest_fixer.write_xml(output, doc)
return output.getvalue()
@ -84,7 +85,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
manifest_input = self.manifest_tmpl % ''
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', False)
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assertEqual(output, expected)
def test_no_min(self):
@ -93,7 +94,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
manifest_input = self.manifest_tmpl % ' <uses-sdk extra="foo"/>\n'
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28',
extra='extra="foo"')
output = self.raise_min_sdk_version_test(manifest_input, '28', False)
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assertEqual(output, expected)
def test_raise_min(self):
@ -101,7 +102,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', False)
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assertEqual(output, expected)
def test_raise(self):
@ -109,7 +110,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', False)
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assertEqual(output, expected)
def test_no_raise_min(self):
@ -117,7 +118,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
manifest_input = self.manifest_tmpl % self.uses_sdk(min='28')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
output = self.raise_min_sdk_version_test(manifest_input, '27', False)
output = self.raise_min_sdk_version_test(manifest_input, '27', '27', False)
self.assertEqual(output, expected)
def test_raise_codename(self):
@ -125,7 +126,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
manifest_input = self.manifest_tmpl % self.uses_sdk(min='28')
expected = self.manifest_tmpl % self.uses_sdk(min='P', target='P')
output = self.raise_min_sdk_version_test(manifest_input, 'P', False)
output = self.raise_min_sdk_version_test(manifest_input, 'P', 'P', False)
self.assertEqual(output, expected)
def test_no_raise_codename(self):
@ -133,7 +134,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
manifest_input = self.manifest_tmpl % self.uses_sdk(min='P')
expected = self.manifest_tmpl % self.uses_sdk(min='P', target='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', False)
output = self.raise_min_sdk_version_test(manifest_input, '28', '28', False)
self.assertEqual(output, expected)
def test_target(self):
@ -141,31 +142,31 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
manifest_input = self.manifest_tmpl % self.uses_sdk(min='26', target='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
output = self.raise_min_sdk_version_test(manifest_input, '28', False)
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assertEqual(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='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', False)
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='29')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assertEqual(output, expected)
def test_target_no_min(self):
"""Tests inserting targetSdkVersion when minSdkVersion exists."""
""""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')
output = self.raise_min_sdk_version_test(manifest_input, '28', False)
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assertEqual(output, expected)
def test_no_target_no_min(self):
"""Tests inserting targetSdkVersion when minSdkVersion does not exist."""
manifest_input = self.manifest_tmpl % ''
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='28')
output = self.raise_min_sdk_version_test(manifest_input, '28', False)
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='29')
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assertEqual(output, expected)
def test_library_no_target(self):
@ -173,7 +174,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
manifest_input = self.manifest_tmpl % self.uses_sdk(min='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='1')
output = self.raise_min_sdk_version_test(manifest_input, '28', True)
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
self.assertEqual(output, expected)
def test_library_target_no_min(self):
@ -181,7 +182,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
manifest_input = self.manifest_tmpl % self.uses_sdk(target='27')
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='27')
output = self.raise_min_sdk_version_test(manifest_input, '28', True)
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
self.assertEqual(output, expected)
def test_library_no_target_no_min(self):
@ -189,7 +190,7 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
manifest_input = self.manifest_tmpl % ''
expected = self.manifest_tmpl % self.uses_sdk(min='28', target='1')
output = self.raise_min_sdk_version_test(manifest_input, '28', True)
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', True)
self.assertEqual(output, expected)
def test_extra(self):
@ -203,10 +204,10 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
# pylint: disable=line-too-long
expected = self.manifest_tmpl % (
' <!-- comment -->\n'
' <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="28" extra="foo"/>\n'
' <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="29" extra="foo"/>\n'
' <application/>\n')
output = self.raise_min_sdk_version_test(manifest_input, '28', False)
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assertEqual(output, expected)
@ -217,10 +218,10 @@ class RaiseMinSdkVersionTest(unittest.TestCase):
# pylint: disable=line-too-long
expected = self.manifest_tmpl % (
' <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="28"/>\n'
' <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="29"/>\n'
' <!-- comment -->\n')
output = self.raise_min_sdk_version_test(manifest_input, '28', False)
output = self.raise_min_sdk_version_test(manifest_input, '28', '29', False)
self.assertEqual(output, expected)