OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright 2013 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 | |
8 Given the manifest file for the main APK, generates an AndroidManifest.xml with | |
9 the value required for a Split APK (package, versionCode, etc). | |
10 """ | |
11 | |
12 import lxml.etree | |
13 import optparse | |
14 | |
15 from util import build_utils | |
16 | |
17 MANIFEST_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?> | |
18 <manifest | |
19 xmlns:android="http://schemas.android.com/apk/res/android" | |
20 android:versionCode="%(version_code)s" | |
21 android:versionName="%(version_name)s" | |
22 package="%(package)s" | |
23 split="%(split)s"> | |
24 <application android:hasCode="%(has_code)s"> | |
25 </application> | |
26 </manifest> | |
27 """ | |
28 | |
29 def ParseArgs(): | |
30 """Parses command line options. | |
31 | |
32 Returns: | |
33 An options object as from optparse.OptionsParser.parse_args() | |
34 """ | |
35 parser = optparse.OptionParser() | |
36 build_utils.AddDepfileOption(parser) | |
37 parser.add_option('--main-manifest', help='The main manifest of the app') | |
38 parser.add_option('--out-manifest', help='The output manifest') | |
39 parser.add_option('--split', help='The name of the split') | |
40 parser.add_option('--version-code', help='value for android:versionCode') | |
41 parser.add_option('--version-name', help='value for android:versionName') | |
42 parser.add_option( | |
43 '--has-code', | |
44 action='store_true', | |
45 help='Whether the split will contain a .dex file') | |
46 | |
47 (options, args) = parser.parse_args() | |
48 | |
49 if args: | |
50 parser.error('No positional arguments should be given.') | |
51 | |
52 # Check that required options have been provided. | |
53 required_options = ('main_manifest', 'out_manifest', 'split') | |
54 build_utils.CheckOptions(options, parser, required=required_options) | |
55 | |
56 return options | |
57 | |
58 | |
59 def Build(main_manifest, split, version_code, version_name, has_code): | |
60 """Builds a split manifest based on the manifest of the main APK. | |
61 | |
62 Args: | |
63 main_manifest: the XML manifest of the main APK as a string | |
64 split: the name of the split as a string | |
65 has_code: whether this split APK will contain .dex files | |
66 | |
67 Returns: | |
68 The XML split manifest as a string | |
69 """ | |
70 | |
71 doc = lxml.etree.fromstring(main_manifest) | |
72 ns = {'android': 'http://schemas.android.com/apk/res/android'} | |
73 package = doc.xpath('/manifest/@package')[0] | |
74 | |
75 if not version_code: | |
76 version_code = doc.xpath('/manifest/@android:versionCode', namespaces=ns)[0] | |
77 if not version_name: | |
78 version_name = doc.xpath('/manifest/@android:versionName', namespaces=ns)[0] | |
79 | |
80 if not version_code: | |
81 raise Exception('android:versionCode not set by main manifest nor by ' | |
82 '--version-code') | |
83 if not version_name: | |
84 raise Exception('android:versionName not set by main manifest nor by ' | |
85 '--version-name') | |
86 | |
87 return MANIFEST_TEMPLATE % { | |
88 'version_code': version_code, | |
89 'version_name': version_name, | |
90 'package': package, | |
91 'split': split, | |
92 'has_code': str(has_code).lower() | |
93 } | |
94 | |
95 | |
96 def main(): | |
97 options = ParseArgs() | |
98 main_manifest = file(options.main_manifest).read() | |
99 split_manifest = Build( | |
100 main_manifest, | |
101 options.split, | |
102 options.version_code, | |
103 options.version_name, | |
104 options.has_code) | |
105 | |
106 with file(options.out_manifest, 'w') as f: | |
107 f.write(split_manifest) | |
108 | |
109 if options.depfile: | |
110 build_utils.WriteDepfile( | |
111 options.depfile, | |
112 build_utils.GetPythonDependencies()) | |
cjhopman
2015/05/07 00:39:52
This list should include the path to the main mani
agrieve
2015/05/12 17:51:43
Done.
| |
113 | |
114 | |
115 if __name__ == '__main__': | |
116 main() | |
OLD | NEW |