blob: 163b4c34be4b06992d9f4e1cf0112b9627f2ebc8 [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +01001#!/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
8Given the manifest file for the real APK, generates an AndroidManifest.xml with
9the application class changed to IncrementalApplication.
10"""
11
12import argparse
13import os
14import sys
15from xml.etree import ElementTree
16
17sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir, 'gyp'))
18from util import build_utils
19
20_ANDROID_NAMESPACE = 'http://schemas.android.com/apk/res/android'
21ElementTree.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
30def _AddNamespace(name):
31 """Adds the android namespace prefix to the given identifier."""
32 return '{%s}%s' % (_ANDROID_NAMESPACE, name)
33
34def _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
50def _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
56def _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
94def 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
109if __name__ == '__main__':
110 main()