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