blob: 9cb3bca4b55f894fc1a9293eed7360b7500a0c6f [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 APK split.
7
8Given the manifest file for the main APK, generates an AndroidManifest.xml with
9the value required for a Split APK (package, versionCode, etc).
10"""
11
12import optparse
13import xml.etree.ElementTree
14
15from util import build_utils
16
17MANIFEST_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?>
18<manifest
19 xmlns:android="http://schemas.android.com/apk/res/android"
20 package="%(package)s"
21 split="%(split)s">
22 <uses-sdk android:minSdkVersion="21" />
23 <application android:hasCode="%(has_code)s">
24 </application>
25</manifest>
26"""
27
28def ParseArgs():
29 """Parses command line options.
30
31 Returns:
32 An options object as from optparse.OptionsParser.parse_args()
33 """
34 parser = optparse.OptionParser()
35 build_utils.AddDepfileOption(parser)
36 parser.add_option('--main-manifest', help='The main manifest of the app')
37 parser.add_option('--out-manifest', help='The output manifest')
38 parser.add_option('--split', help='The name of the split')
39 parser.add_option(
40 '--has-code',
41 action='store_true',
42 default=False,
43 help='Whether the split will contain a .dex file')
44
45 (options, args) = parser.parse_args()
46
47 if args:
48 parser.error('No positional arguments should be given.')
49
50 # Check that required options have been provided.
51 required_options = ('main_manifest', 'out_manifest', 'split')
52 build_utils.CheckOptions(options, parser, required=required_options)
53
54 return options
55
56
57def Build(main_manifest, split, has_code):
58 """Builds a split manifest based on the manifest of the main APK.
59
60 Args:
61 main_manifest: the XML manifest of the main APK as a string
62 split: the name of the split as a string
63 has_code: whether this split APK will contain .dex files
64
65 Returns:
66 The XML split manifest as a string
67 """
68
69 doc = xml.etree.ElementTree.fromstring(main_manifest)
70 package = doc.get('package')
71
72 return MANIFEST_TEMPLATE % {
73 'package': package,
74 'split': split.replace('-', '_'),
75 'has_code': str(has_code).lower()
76 }
77
78
79def main():
80 options = ParseArgs()
81 main_manifest = file(options.main_manifest).read()
82 split_manifest = Build(
83 main_manifest,
84 options.split,
85 options.has_code)
86
87 with file(options.out_manifest, 'w') as f:
88 f.write(split_manifest)
89
90 if options.depfile:
91 build_utils.WriteDepfile(
92 options.depfile,
93 [options.main_manifest] + build_utils.GetPythonDependencies())
94
95
96if __name__ == '__main__':
97 main()