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.
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).
13 import xml
.etree
.ElementTree
15 from util
import build_utils
17 MANIFEST_TEMPLATE
= """<?xml version="1.0" encoding="utf-8"?>
19 xmlns:android="http://schemas.android.com/apk/res/android"
22 <uses-sdk android:minSdkVersion="21" />
23 <application android:hasCode="%(has_code)s">
29 """Parses command line options.
32 An options object as from optparse.OptionsParser.parse_args()
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')
43 help='Whether the split will contain a .dex file')
45 (options
, args
) = parser
.parse_args()
48 parser
.error('No positional arguments should be given.')
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
)
57 def Build(main_manifest
, split
, has_code
):
58 """Builds a split manifest based on the manifest of the main APK.
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
66 The XML split manifest as a string
69 doc
= xml
.etree
.ElementTree
.fromstring(main_manifest
)
70 package
= doc
.get('package')
72 return MANIFEST_TEMPLATE
% {
74 'split': split
.replace('-', '_'),
75 'has_code': str(has_code
).lower()
81 main_manifest
= file(options
.main_manifest
).read()
82 split_manifest
= Build(
87 with
file(options
.out_manifest
, 'w') as f
:
88 f
.write(split_manifest
)
91 build_utils
.WriteDepfile(
93 [options
.main_manifest
] + build_utils
.GetPythonDependencies())
96 if __name__
== '__main__':