blob: f45d6744959bcbd632297f4a78f7f94bf813f978 [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"""Removes the resources from a resource APK for incremental deployment.
16
17The reason this utility exists is that the only way we can build a binary
18AndroidManifest.xml is by invoking aapt, which builds a whole resource .apk.
19
20Thus, in order to build the AndroidManifest.xml for an incremental .apk, we
21invoke aapt, then extract AndroidManifest.xml from its output.
22"""
23
24import sys
25import zipfile
26
27from third_party.py import gflags
28
29
30gflags.DEFINE_string("input_resource_apk", None, "The input resource .apk")
31gflags.DEFINE_string("output_resource_apk", None, "The output resource .apk")
32
33FLAGS = gflags.FLAGS
34HERMETIC_TIMESTAMP = (2001, 1, 1, 0, 0, 0)
35
36
37def 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
50if __name__ == "__main__":
51 FLAGS(sys.argv)
52 main()