Set targetSdkVersion in manifest_fixer
If targetSdkVersion is not set in the manifest, set it to the
value it was implicitly using before changing minSdkVersion.
Requires passing --library to manifest_fixer.py to distinguish
between apps, where the implicit value was set by aapt2 to
current, or libraries where the implicit value was 1.
Fixes cases where the manifest does not specify targetSdkVersion
and was inheriting the minSdkVersion value until manifest merger
started merging a lower targetSdkVersion value from a library.
Bug: 111347801
Test: manifest_fixer_test.py
Change-Id: I8fcf0c5f452707565ba1808f6fe552ffed055c47
diff --git a/scripts/manifest_fixer.py b/scripts/manifest_fixer.py
index 6af0ca9..25f96cd 100755
--- a/scripts/manifest_fixer.py
+++ b/scripts/manifest_fixer.py
@@ -49,6 +49,8 @@
parser = argparse.ArgumentParser()
parser.add_argument('--minSdkVersion', default='', dest='min_sdk_version',
help='specify minSdkVersion used by the build system')
+ 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',
help='specify additional <uses-library> tag to add')
parser.add_argument('input', help='input AndroidManifest.xml file')
@@ -126,7 +128,7 @@
return indent
-def raise_min_sdk_version(doc, requested):
+def raise_min_sdk_version(doc, requested, library):
"""Ensure the manifest contains a <uses-sdk> tag with a minSdkVersion.
Args:
@@ -153,16 +155,27 @@
# other children of the <manifest> tag.
manifest.insertBefore(doc.createTextNode(indent), manifest.firstChild)
- # Get or insert the minSdkVersion attribute
+ # Get or insert the minSdkVersion attribute. If it is already present, make
+ # sure it as least the requested value.
min_attr = element.getAttributeNodeNS(android_ns, 'minSdkVersion')
if min_attr is None:
min_attr = doc.createAttributeNS(android_ns, 'android:minSdkVersion')
- min_attr.value = '1'
- element.setAttributeNode(min_attr)
-
- # Update the value of the minSdkVersion attribute if necessary
- if compare_version_gt(requested, min_attr.value):
min_attr.value = requested
+ element.setAttributeNode(min_attr)
+ else:
+ if compare_version_gt(requested, min_attr.value):
+ min_attr.value = requested
+
+ # Insert the targetSdkVersion attribute if it is missing. If it is already
+ # present leave it as is.
+ target_attr = element.getAttributeNodeNS(android_ns, 'targetSdkVersion')
+ if target_attr is None:
+ target_attr = doc.createAttributeNS(android_ns, 'android:targetSdkVersion')
+ if library:
+ target_attr.value = '1'
+ else:
+ target_attr.value = requested
+ element.setAttributeNode(target_attr)
def add_uses_libraries(doc, new_uses_libraries):
@@ -230,7 +243,7 @@
ensure_manifest_android_ns(doc)
if args.min_sdk_version:
- raise_min_sdk_version(doc, args.min_sdk_version)
+ raise_min_sdk_version(doc, args.min_sdk_version, args.library)
if args.uses_libraries:
add_uses_libraries(doc, args.uses_libraries)