blob: c791e3a5d4ac91c3780089e9a13bf6d69adb869b [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001# Copyright 2015 The Bazel Authors. All rights reserved.
Alex Humeskya4ecde62015-05-21 17:08:42 +00002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Stubifies an AndroidManifest.xml.
16
17Does the following things:
18 - Replaces the Application class in an Android manifest with a stub one
19 - Resolve string and integer resources to their default values
20
21usage: %s [input manifest] [output manifest] [file for old application class]
22
23Writes the old application class into the file designated by the third argument.
24"""
25
26import sys
27from xml.etree import ElementTree
28
29from third_party.py import gflags
30
31
32gflags.DEFINE_string("main_manifest", None, "The main manifest of the app")
33gflags.DEFINE_string("split_manifest", None, "The output manifest")
34gflags.DEFINE_string("override_package", None,
35 "The Android package. Override the one specified in the "
36 "input manifest")
37gflags.DEFINE_string("split", None, "The name of the split")
38gflags.DEFINE_boolean("hascode", False, "Whether this split .apk has dexes")
39
40FLAGS = gflags.FLAGS
41
42MANIFEST_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?>
43<manifest
44 xmlns:android="http://schemas.android.com/apk/res/android"
Alex Humesky3632f302017-01-26 23:05:28 +000045 %(version_code_attribute)s
46 %(version_name_attribute)s
Alex Humeskya4ecde62015-05-21 17:08:42 +000047 package="%(package)s"
48 split="%(split)s">
49 <application android:hasCode="%(hascode)s">
50 </application>
51</manifest>
52"""
53
54
55def BuildSplitManifest(main_manifest, override_package, split, hascode):
56 """Builds a split manifest based on the manifest of the main APK.
57
58 Args:
59 main_manifest: the XML manifest of the main APK as a string
60 override_package: if not None, override the package in the main manifest
61 split: the name of the split as a string
62 hascode: if this split APK will contain .dex files
63
64 Returns:
65 The XML split manifest as a string
66
67 Raises:
68 Exception if something goes wrong.
69 """
70
71 manifest = ElementTree.fromstring(main_manifest)
72 android_namespace_prefix = "{http://schemas.android.com/apk/res/android}"
73
74 if override_package:
75 package = override_package
76 else:
77 package = manifest.get("package")
78
79 version_code = manifest.get(android_namespace_prefix + "versionCode")
80 version_name = manifest.get(android_namespace_prefix + "versionName")
81
82 return MANIFEST_TEMPLATE % {
Alex Humesky3632f302017-01-26 23:05:28 +000083 "version_code_attribute":
84 'android:versionCode="%s"' % version_code if version_code else "",
85 "version_name_attribute":
86 'android:versionName="%s"' % version_name if version_name else "",
Alex Humeskya4ecde62015-05-21 17:08:42 +000087 "package": package,
88 "split": split,
89 "hascode": str(hascode).lower()
90 }
91
92
93def main():
94 split_manifest = BuildSplitManifest(
95 file(FLAGS.main_manifest).read(),
96 FLAGS.override_package,
97 FLAGS.split,
98 FLAGS.hascode)
99
100 with file(FLAGS.split_manifest, "w") as output_xml:
101 output_xml.write(split_manifest)
102
103
104if __name__ == "__main__":
105 FLAGS(sys.argv)
106 main()