Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2015 The Chromium Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | """Creates an AndroidManifest.xml for an incremental APK. |
| 7 | |
| 8 | Given the manifest file for the real APK, generates an AndroidManifest.xml with |
| 9 | the application class changed to IncrementalApplication. |
| 10 | """ |
| 11 | |
| 12 | import argparse |
| 13 | import os |
| 14 | import sys |
| 15 | from xml.etree import ElementTree |
| 16 | |
| 17 | sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir, 'gyp')) |
| 18 | from util import build_utils |
| 19 | |
| 20 | _ANDROID_NAMESPACE = 'http://schemas.android.com/apk/res/android' |
| 21 | ElementTree.register_namespace('android', _ANDROID_NAMESPACE) |
| 22 | |
| 23 | _INCREMENTAL_APP_NAME = 'org.chromium.incrementalinstall.BootstrapApplication' |
| 24 | _META_DATA_APP_NAME = 'incremental-install-real-app' |
| 25 | _META_DATA_INSTRUMENTATION_NAME = 'incremental-install-real-instrumentation' |
| 26 | _DEFAULT_APPLICATION_CLASS = 'android.app.Application' |
| 27 | _DEFAULT_INSTRUMENTATION_CLASS = 'android.app.Instrumentation' |
| 28 | |
| 29 | |
| 30 | def _AddNamespace(name): |
| 31 | """Adds the android namespace prefix to the given identifier.""" |
| 32 | return '{%s}%s' % (_ANDROID_NAMESPACE, name) |
| 33 | |
| 34 | def _ParseArgs(): |
| 35 | parser = argparse.ArgumentParser() |
| 36 | build_utils.AddDepfileOption(parser) |
| 37 | parser.add_argument('--src-manifest', |
| 38 | help='The main manifest of the app', |
| 39 | required=True) |
| 40 | parser.add_argument('--out-manifest', |
| 41 | help='The output manifest', |
| 42 | required=True) |
| 43 | parser.add_argument('--disable-isolated-processes', |
| 44 | help='Changes all android:isolatedProcess to false. ' |
| 45 | 'This is required on Android M+', |
| 46 | action='store_true') |
| 47 | return parser.parse_args() |
| 48 | |
| 49 | |
| 50 | def _CreateMetaData(parent, name, value): |
| 51 | meta_data_node = ElementTree.SubElement(parent, 'meta-data') |
| 52 | meta_data_node.set(_AddNamespace('name'), name) |
| 53 | meta_data_node.set(_AddNamespace('value'), value) |
| 54 | |
| 55 | |
| 56 | def _ProcessManifest(main_manifest, disable_isolated_processes): |
| 57 | """Returns a transformed AndroidManifest.xml for use with _incremental apks. |
| 58 | |
| 59 | Args: |
| 60 | main_manifest: Manifest contents to transform. |
| 61 | disable_isolated_processes: Whether to set all isolatedProcess attributes to |
| 62 | false |
| 63 | |
| 64 | Returns: |
| 65 | The transformed AndroidManifest.xml. |
| 66 | """ |
| 67 | if disable_isolated_processes: |
| 68 | main_manifest = main_manifest.replace('isolatedProcess="true"', |
| 69 | 'isolatedProcess="false"') |
| 70 | |
| 71 | doc = ElementTree.fromstring(main_manifest) |
| 72 | app_node = doc.find('application') |
| 73 | if app_node is None: |
| 74 | app_node = ElementTree.SubElement(doc, 'application') |
| 75 | |
| 76 | real_app_class = app_node.get(_AddNamespace('name'), |
| 77 | _DEFAULT_APPLICATION_CLASS) |
| 78 | app_node.set(_AddNamespace('name'), _INCREMENTAL_APP_NAME) |
| 79 | _CreateMetaData(app_node, _META_DATA_APP_NAME, real_app_class) |
| 80 | |
| 81 | # Seems to be a bug in ElementTree, as doc.find() doesn't work here. |
| 82 | instrumentation_nodes = doc.findall('instrumentation') |
| 83 | if instrumentation_nodes: |
| 84 | instrumentation_node = instrumentation_nodes[0] |
| 85 | real_instrumentation_class = instrumentation_node.get(_AddNamespace('name')) |
| 86 | instrumentation_node.set(_AddNamespace('name'), |
| 87 | _DEFAULT_INSTRUMENTATION_CLASS) |
| 88 | _CreateMetaData(app_node, _META_DATA_INSTRUMENTATION_NAME, |
| 89 | real_instrumentation_class) |
| 90 | |
| 91 | return ElementTree.tostring(doc, encoding='UTF-8') |
| 92 | |
| 93 | |
| 94 | def main(): |
| 95 | options = _ParseArgs() |
| 96 | with open(options.src_manifest) as f: |
| 97 | main_manifest_data = f.read() |
| 98 | new_manifest_data = _ProcessManifest(main_manifest_data, |
| 99 | options.disable_isolated_processes) |
| 100 | with open(options.out_manifest, 'w') as f: |
| 101 | f.write(new_manifest_data) |
| 102 | |
| 103 | if options.depfile: |
| 104 | build_utils.WriteDepfile( |
| 105 | options.depfile, |
| 106 | [options.src_manifest] + build_utils.GetPythonDependencies()) |
| 107 | |
| 108 | |
| 109 | if __name__ == '__main__': |
| 110 | main() |