• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright 2015 The Chromium Authors
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
19from util import manifest_utils
20import action_helpers  # build_utils adds //build to sys.path.
21
22_INCREMENTAL_APP_NAME = 'org.chromium.incrementalinstall.BootstrapApplication'
23_META_DATA_APP_NAME = 'incremental-install-real-app'
24_DEFAULT_APPLICATION_CLASS = 'android.app.Application'
25_META_DATA_INSTRUMENTATION_NAMES = [
26    'incremental-install-real-instrumentation-0',
27    'incremental-install-real-instrumentation-1',
28]
29_INCREMENTAL_INSTRUMENTATION_CLASSES = [
30    'android.app.Instrumentation',
31    'org.chromium.incrementalinstall.SecondInstrumentation',
32]
33
34
35def _AddNamespace(name):
36  """Adds the android namespace prefix to the given identifier."""
37  return '{%s}%s' % (manifest_utils.ANDROID_NAMESPACE, name)
38
39
40def _ParseArgs(args):
41  parser = argparse.ArgumentParser()
42  parser.add_argument('--src-manifest',
43                      required=True,
44                      help='The main manifest of the app.')
45  parser.add_argument('--dst-manifest',
46                      required=True,
47                      help='The output modified manifest.')
48  parser.add_argument('--disable-isolated-processes',
49                      help='Changes all android:isolatedProcess to false. '
50                           'This is required on Android M+',
51                      action='store_true')
52
53  ret = parser.parse_args(build_utils.ExpandFileArgs(args))
54  return ret
55
56
57def _CreateMetaData(parent, name, value):
58  meta_data_node = ElementTree.SubElement(parent, 'meta-data')
59  meta_data_node.set(_AddNamespace('name'), name)
60  meta_data_node.set(_AddNamespace('value'), value)
61
62
63def _ProcessManifest(path, disable_isolated_processes):
64  doc, _, app_node = manifest_utils.ParseManifest(path)
65
66  # Pylint for some reason things app_node is an int.
67  # pylint: disable=no-member
68  real_app_class = app_node.get(_AddNamespace('name'),
69                                _DEFAULT_APPLICATION_CLASS)
70  app_node.set(_AddNamespace('name'), _INCREMENTAL_APP_NAME)
71  # pylint: enable=no-member
72  _CreateMetaData(app_node, _META_DATA_APP_NAME, real_app_class)
73
74  # Seems to be a bug in ElementTree, as doc.find() doesn't work here.
75  instrumentation_nodes = doc.findall('instrumentation')
76  assert len(instrumentation_nodes) <= 2, (
77      'Need to update incremental install to support >2 <instrumentation> tags')
78  for i, instrumentation_node in enumerate(instrumentation_nodes):
79    real_instrumentation_class = instrumentation_node.get(_AddNamespace('name'))
80    instrumentation_node.set(_AddNamespace('name'),
81                             _INCREMENTAL_INSTRUMENTATION_CLASSES[i])
82    _CreateMetaData(app_node, _META_DATA_INSTRUMENTATION_NAMES[i],
83                    real_instrumentation_class)
84
85  ret = ElementTree.tostring(doc.getroot(), encoding='UTF-8')
86  # Disable check for page-aligned native libraries.
87  ret = ret.replace(b'extractNativeLibs="false"', b'extractNativeLibs="true"')
88  if disable_isolated_processes:
89    ret = ret.replace(b'isolatedProcess="true"', b'isolatedProcess="false"')
90    # externalService only matters for isolatedProcess="true". See:
91    # https://developer.android.com/reference/android/R.attr#externalService
92    ret = ret.replace(b'externalService="true"', b'externalService="false"')
93  return ret
94
95
96def main(raw_args):
97  options = _ParseArgs(raw_args)
98
99  new_manifest_data = _ProcessManifest(options.src_manifest,
100                                       options.disable_isolated_processes)
101  with action_helpers.atomic_output(options.dst_manifest) as out_manifest:
102    out_manifest.write(new_manifest_data)
103
104
105if __name__ == '__main__':
106  main(sys.argv[1:])
107