Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2018 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | """A tool for inserting values from the build system into a manifest.""" |
| 18 | |
| 19 | from __future__ import print_function |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame^] | 20 | |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 21 | import argparse |
| 22 | import sys |
| 23 | from xml.dom import minidom |
| 24 | |
| 25 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame^] | 26 | from manifest import android_ns |
| 27 | from manifest import compare_version_gt |
| 28 | from manifest import ensure_manifest_android_ns |
| 29 | from manifest import find_child_with_attribute |
| 30 | from manifest import get_children_with_tag |
| 31 | from manifest import get_indent |
| 32 | from manifest import parse_manifest |
| 33 | from manifest import write_xml |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 34 | |
| 35 | |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 36 | def parse_args(): |
| 37 | """Parse commandline arguments.""" |
| 38 | |
| 39 | parser = argparse.ArgumentParser() |
| 40 | parser.add_argument('--minSdkVersion', default='', dest='min_sdk_version', |
| 41 | help='specify minSdkVersion used by the build system') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 42 | parser.add_argument('--targetSdkVersion', default='', dest='target_sdk_version', |
| 43 | help='specify targetSdkVersion used by the build system') |
| 44 | parser.add_argument('--raise-min-sdk-version', dest='raise_min_sdk_version', action='store_true', |
| 45 | help='raise the minimum sdk version in the manifest if necessary') |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 46 | parser.add_argument('--library', dest='library', action='store_true', |
| 47 | help='manifest is for a static library') |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 48 | parser.add_argument('--uses-library', dest='uses_libraries', action='append', |
Jiyong Park | fa17afe | 2018-10-16 11:00:04 +0900 | [diff] [blame] | 49 | help='specify additional <uses-library> tag to add. android:requred is set to true') |
| 50 | parser.add_argument('--optional-uses-library', dest='optional_uses_libraries', action='append', |
| 51 | help='specify additional <uses-library> tag to add. android:requred is set to false') |
David Brazdil | d5b7499 | 2018-08-28 12:41:01 +0100 | [diff] [blame] | 52 | parser.add_argument('--uses-non-sdk-api', dest='uses_non_sdk_api', action='store_true', |
| 53 | help='manifest is for a package built against the platform') |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 54 | parser.add_argument('--use-embedded-dex', dest='use_embedded_dex', action='store_true', |
| 55 | help=('specify if the app wants to use embedded dex and avoid extracted,' |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 56 | 'locally compiled code. Must not conflict if already declared ' |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 57 | 'in the manifest.')) |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 58 | parser.add_argument('--extract-native-libs', dest='extract_native_libs', |
| 59 | default=None, type=lambda x: (str(x).lower() == 'true'), |
| 60 | help=('specify if the app wants to use embedded native libraries. Must not conflict ' |
| 61 | 'if already declared in the manifest.')) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 62 | parser.add_argument('input', help='input AndroidManifest.xml file') |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 63 | parser.add_argument('output', help='output AndroidManifest.xml file') |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 64 | return parser.parse_args() |
| 65 | |
| 66 | |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 67 | def raise_min_sdk_version(doc, min_sdk_version, target_sdk_version, library): |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 68 | """Ensure the manifest contains a <uses-sdk> tag with a minSdkVersion. |
| 69 | |
| 70 | Args: |
| 71 | doc: The XML document. May be modified by this function. |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 72 | min_sdk_version: The requested minSdkVersion attribute. |
| 73 | target_sdk_version: The requested targetSdkVersion attribute. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame^] | 74 | library: True if the manifest is for a library. |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 75 | Raises: |
| 76 | RuntimeError: invalid manifest |
| 77 | """ |
| 78 | |
| 79 | manifest = parse_manifest(doc) |
| 80 | |
| 81 | # Get or insert the uses-sdk element |
| 82 | uses_sdk = get_children_with_tag(manifest, 'uses-sdk') |
| 83 | if len(uses_sdk) > 1: |
| 84 | raise RuntimeError('found multiple uses-sdk elements') |
| 85 | elif len(uses_sdk) == 1: |
| 86 | element = uses_sdk[0] |
| 87 | else: |
| 88 | element = doc.createElement('uses-sdk') |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 89 | indent = get_indent(manifest.firstChild, 1) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 90 | manifest.insertBefore(element, manifest.firstChild) |
| 91 | |
| 92 | # Insert an indent before uses-sdk to line it up with the indentation of the |
| 93 | # other children of the <manifest> tag. |
| 94 | manifest.insertBefore(doc.createTextNode(indent), manifest.firstChild) |
| 95 | |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 96 | # Get or insert the minSdkVersion attribute. If it is already present, make |
| 97 | # sure it as least the requested value. |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 98 | min_attr = element.getAttributeNodeNS(android_ns, 'minSdkVersion') |
| 99 | if min_attr is None: |
| 100 | min_attr = doc.createAttributeNS(android_ns, 'android:minSdkVersion') |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 101 | min_attr.value = min_sdk_version |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 102 | element.setAttributeNode(min_attr) |
| 103 | else: |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 104 | if compare_version_gt(min_sdk_version, min_attr.value): |
| 105 | min_attr.value = min_sdk_version |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 106 | |
| 107 | # Insert the targetSdkVersion attribute if it is missing. If it is already |
| 108 | # present leave it as is. |
| 109 | target_attr = element.getAttributeNodeNS(android_ns, 'targetSdkVersion') |
| 110 | if target_attr is None: |
| 111 | target_attr = doc.createAttributeNS(android_ns, 'android:targetSdkVersion') |
| 112 | if library: |
Colin Cross | 4b17606 | 2018-10-01 15:15:51 -0700 | [diff] [blame] | 113 | # TODO(b/117122200): libraries shouldn't set targetSdkVersion at all, but |
| 114 | # ManifestMerger treats minSdkVersion="Q" as targetSdkVersion="Q" if it |
| 115 | # is empty. Set it to something low so that it will be overriden by the |
| 116 | # main manifest, but high enough that it doesn't cause implicit |
| 117 | # permissions grants. |
| 118 | target_attr.value = '15' |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 119 | else: |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 120 | target_attr.value = target_sdk_version |
Colin Cross | 1b6a3cf | 2018-07-24 14:51:30 -0700 | [diff] [blame] | 121 | element.setAttributeNode(target_attr) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 122 | |
| 123 | |
Jiyong Park | fa17afe | 2018-10-16 11:00:04 +0900 | [diff] [blame] | 124 | def add_uses_libraries(doc, new_uses_libraries, required): |
| 125 | """Add additional <uses-library> tags |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 126 | |
| 127 | Args: |
| 128 | doc: The XML document. May be modified by this function. |
| 129 | new_uses_libraries: The names of libraries to be added by this function. |
Jiyong Park | fa17afe | 2018-10-16 11:00:04 +0900 | [diff] [blame] | 130 | required: The value of android:required attribute. Can be true or false. |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 131 | Raises: |
| 132 | RuntimeError: Invalid manifest |
| 133 | """ |
| 134 | |
| 135 | manifest = parse_manifest(doc) |
| 136 | elems = get_children_with_tag(manifest, 'application') |
| 137 | application = elems[0] if len(elems) == 1 else None |
| 138 | if len(elems) > 1: |
| 139 | raise RuntimeError('found multiple <application> tags') |
| 140 | elif not elems: |
| 141 | application = doc.createElement('application') |
| 142 | indent = get_indent(manifest.firstChild, 1) |
| 143 | first = manifest.firstChild |
| 144 | manifest.insertBefore(doc.createTextNode(indent), first) |
| 145 | manifest.insertBefore(application, first) |
| 146 | |
| 147 | indent = get_indent(application.firstChild, 2) |
| 148 | |
| 149 | last = application.lastChild |
| 150 | if last is not None and last.nodeType != minidom.Node.TEXT_NODE: |
| 151 | last = None |
| 152 | |
| 153 | for name in new_uses_libraries: |
| 154 | if find_child_with_attribute(application, 'uses-library', android_ns, |
| 155 | 'name', name) is not None: |
| 156 | # If the uses-library tag of the same 'name' attribute value exists, |
| 157 | # respect it. |
| 158 | continue |
| 159 | |
| 160 | ul = doc.createElement('uses-library') |
| 161 | ul.setAttributeNS(android_ns, 'android:name', name) |
Jiyong Park | fa17afe | 2018-10-16 11:00:04 +0900 | [diff] [blame] | 162 | ul.setAttributeNS(android_ns, 'android:required', str(required).lower()) |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 163 | |
| 164 | application.insertBefore(doc.createTextNode(indent), last) |
| 165 | application.insertBefore(ul, last) |
| 166 | |
| 167 | # align the closing tag with the opening tag if it's not |
| 168 | # indented |
| 169 | if application.lastChild.nodeType != minidom.Node.TEXT_NODE: |
| 170 | indent = get_indent(application.previousSibling, 1) |
| 171 | application.appendChild(doc.createTextNode(indent)) |
| 172 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame^] | 173 | |
David Brazdil | d5b7499 | 2018-08-28 12:41:01 +0100 | [diff] [blame] | 174 | def add_uses_non_sdk_api(doc): |
| 175 | """Add android:usesNonSdkApi=true attribute to <application>. |
| 176 | |
| 177 | Args: |
| 178 | doc: The XML document. May be modified by this function. |
| 179 | Raises: |
| 180 | RuntimeError: Invalid manifest |
| 181 | """ |
| 182 | |
| 183 | manifest = parse_manifest(doc) |
| 184 | elems = get_children_with_tag(manifest, 'application') |
| 185 | application = elems[0] if len(elems) == 1 else None |
| 186 | if len(elems) > 1: |
| 187 | raise RuntimeError('found multiple <application> tags') |
| 188 | elif not elems: |
| 189 | application = doc.createElement('application') |
| 190 | indent = get_indent(manifest.firstChild, 1) |
| 191 | first = manifest.firstChild |
| 192 | manifest.insertBefore(doc.createTextNode(indent), first) |
| 193 | manifest.insertBefore(application, first) |
| 194 | |
| 195 | attr = application.getAttributeNodeNS(android_ns, 'usesNonSdkApi') |
| 196 | if attr is None: |
| 197 | attr = doc.createAttributeNS(android_ns, 'android:usesNonSdkApi') |
| 198 | attr.value = 'true' |
| 199 | application.setAttributeNode(attr) |
| 200 | |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 201 | |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 202 | def add_use_embedded_dex(doc): |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 203 | manifest = parse_manifest(doc) |
| 204 | elems = get_children_with_tag(manifest, 'application') |
| 205 | application = elems[0] if len(elems) == 1 else None |
| 206 | if len(elems) > 1: |
| 207 | raise RuntimeError('found multiple <application> tags') |
| 208 | elif not elems: |
| 209 | application = doc.createElement('application') |
| 210 | indent = get_indent(manifest.firstChild, 1) |
| 211 | first = manifest.firstChild |
| 212 | manifest.insertBefore(doc.createTextNode(indent), first) |
| 213 | manifest.insertBefore(application, first) |
| 214 | |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 215 | attr = application.getAttributeNodeNS(android_ns, 'useEmbeddedDex') |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 216 | if attr is None: |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 217 | attr = doc.createAttributeNS(android_ns, 'android:useEmbeddedDex') |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 218 | attr.value = 'true' |
| 219 | application.setAttributeNode(attr) |
| 220 | elif attr.value != 'true': |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 221 | raise RuntimeError('existing attribute mismatches the option of --use-embedded-dex') |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 222 | |
| 223 | |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 224 | def add_extract_native_libs(doc, extract_native_libs): |
| 225 | manifest = parse_manifest(doc) |
| 226 | elems = get_children_with_tag(manifest, 'application') |
| 227 | application = elems[0] if len(elems) == 1 else None |
| 228 | if len(elems) > 1: |
| 229 | raise RuntimeError('found multiple <application> tags') |
| 230 | elif not elems: |
| 231 | application = doc.createElement('application') |
| 232 | indent = get_indent(manifest.firstChild, 1) |
| 233 | first = manifest.firstChild |
| 234 | manifest.insertBefore(doc.createTextNode(indent), first) |
| 235 | manifest.insertBefore(application, first) |
| 236 | |
| 237 | value = str(extract_native_libs).lower() |
| 238 | attr = application.getAttributeNodeNS(android_ns, 'extractNativeLibs') |
| 239 | if attr is None: |
| 240 | attr = doc.createAttributeNS(android_ns, 'android:extractNativeLibs') |
| 241 | attr.value = value |
| 242 | application.setAttributeNode(attr) |
| 243 | elif attr.value != value: |
| 244 | raise RuntimeError('existing attribute extractNativeLibs="%s" conflicts with --extract-native-libs="%s"' % |
| 245 | (attr.value, value)) |
| 246 | |
| 247 | |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 248 | def main(): |
| 249 | """Program entry point.""" |
| 250 | try: |
| 251 | args = parse_args() |
| 252 | |
| 253 | doc = minidom.parse(args.input) |
| 254 | |
| 255 | ensure_manifest_android_ns(doc) |
| 256 | |
Colin Cross | 7b59e7b | 2018-09-10 13:35:13 -0700 | [diff] [blame] | 257 | if args.raise_min_sdk_version: |
| 258 | raise_min_sdk_version(doc, args.min_sdk_version, args.target_sdk_version, args.library) |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 259 | |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 260 | if args.uses_libraries: |
Jiyong Park | fa17afe | 2018-10-16 11:00:04 +0900 | [diff] [blame] | 261 | add_uses_libraries(doc, args.uses_libraries, True) |
| 262 | |
| 263 | if args.optional_uses_libraries: |
| 264 | add_uses_libraries(doc, args.optional_uses_libraries, False) |
Jiyong Park | c08f46f | 2018-06-18 11:01:00 +0900 | [diff] [blame] | 265 | |
David Brazdil | d5b7499 | 2018-08-28 12:41:01 +0100 | [diff] [blame] | 266 | if args.uses_non_sdk_api: |
| 267 | add_uses_non_sdk_api(doc) |
| 268 | |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 269 | if args.use_embedded_dex: |
| 270 | add_use_embedded_dex(doc) |
Victor Hsieh | ce7818e | 2018-10-22 11:16:25 -0700 | [diff] [blame] | 271 | |
Colin Cross | e4246ab | 2019-02-05 21:55:21 -0800 | [diff] [blame] | 272 | if args.extract_native_libs is not None: |
| 273 | add_extract_native_libs(doc, args.extract_native_libs) |
| 274 | |
Colin Cross | 8bb10e8 | 2018-06-07 16:46:02 -0700 | [diff] [blame] | 275 | with open(args.output, 'wb') as f: |
| 276 | write_xml(f, doc) |
| 277 | |
| 278 | # pylint: disable=broad-except |
| 279 | except Exception as err: |
| 280 | print('error: ' + str(err), file=sys.stderr) |
| 281 | sys.exit(-1) |
| 282 | |
| 283 | if __name__ == '__main__': |
| 284 | main() |