Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 2 | # |
| 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 | |
| 17 | Does 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 | |
| 21 | usage: %s [input manifest] [output manifest] [file for old application class] |
| 22 | |
| 23 | Writes the old application class into the file designated by the third argument. |
| 24 | """ |
| 25 | |
| 26 | import sys |
| 27 | from xml.etree import ElementTree |
| 28 | |
| 29 | from third_party.py import gflags |
| 30 | |
| 31 | |
| 32 | gflags.DEFINE_string("main_manifest", None, "The main manifest of the app") |
| 33 | gflags.DEFINE_string("split_manifest", None, "The output manifest") |
| 34 | gflags.DEFINE_string("override_package", None, |
| 35 | "The Android package. Override the one specified in the " |
| 36 | "input manifest") |
| 37 | gflags.DEFINE_string("split", None, "The name of the split") |
| 38 | gflags.DEFINE_boolean("hascode", False, "Whether this split .apk has dexes") |
| 39 | |
| 40 | FLAGS = gflags.FLAGS |
| 41 | |
| 42 | MANIFEST_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?> |
| 43 | <manifest |
| 44 | xmlns:android="http://schemas.android.com/apk/res/android" |
Alex Humesky | 3632f30 | 2017-01-26 23:05:28 +0000 | [diff] [blame] | 45 | %(version_code_attribute)s |
| 46 | %(version_name_attribute)s |
Alex Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 47 | package="%(package)s" |
| 48 | split="%(split)s"> |
| 49 | <application android:hasCode="%(hascode)s"> |
| 50 | </application> |
| 51 | </manifest> |
| 52 | """ |
| 53 | |
| 54 | |
| 55 | def 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 Humesky | 3632f30 | 2017-01-26 23:05:28 +0000 | [diff] [blame] | 83 | "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 Humesky | a4ecde6 | 2015-05-21 17:08:42 +0000 | [diff] [blame] | 87 | "package": package, |
| 88 | "split": split, |
| 89 | "hascode": str(hascode).lower() |
| 90 | } |
| 91 | |
| 92 | |
| 93 | def 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 | |
| 104 | if __name__ == "__main__": |
| 105 | FLAGS(sys.argv) |
| 106 | main() |