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 | """Removes the resources from a resource APK for incremental deployment. |
| 16 | |
| 17 | The reason this utility exists is that the only way we can build a binary |
| 18 | AndroidManifest.xml is by invoking aapt, which builds a whole resource .apk. |
| 19 | |
| 20 | Thus, in order to build the AndroidManifest.xml for an incremental .apk, we |
| 21 | invoke aapt, then extract AndroidManifest.xml from its output. |
| 22 | """ |
| 23 | |
| 24 | import sys |
| 25 | import zipfile |
| 26 | |
| 27 | from third_party.py import gflags |
| 28 | |
| 29 | |
| 30 | gflags.DEFINE_string("input_resource_apk", None, "The input resource .apk") |
| 31 | gflags.DEFINE_string("output_resource_apk", None, "The output resource .apk") |
| 32 | |
| 33 | FLAGS = gflags.FLAGS |
| 34 | HERMETIC_TIMESTAMP = (2001, 1, 1, 0, 0, 0) |
| 35 | |
| 36 | |
| 37 | def main(): |
| 38 | with zipfile.ZipFile(FLAGS.input_resource_apk) as input_zip: |
| 39 | with input_zip.open("AndroidManifest.xml") as android_manifest_entry: |
| 40 | android_manifest = android_manifest_entry.read() |
| 41 | |
| 42 | with zipfile.ZipFile(FLAGS.output_resource_apk, "w") as output_zip: |
| 43 | # Timestamp is explicitly set so that the resulting zip file is hermetic |
| 44 | zipinfo = zipfile.ZipInfo( |
| 45 | filename="AndroidManifest.xml", |
| 46 | date_time=HERMETIC_TIMESTAMP) |
| 47 | output_zip.writestr(zipinfo, android_manifest) |
| 48 | |
| 49 | |
| 50 | if __name__ == "__main__": |
| 51 | FLAGS(sys.argv) |
| 52 | main() |