OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 """Signs and zipaligns APK. | 6 """Signs and zipaligns APK. |
7 | 7 |
8 """ | 8 """ |
9 | 9 |
10 import optparse | 10 import optparse |
| 11 import os |
11 import shutil | 12 import shutil |
12 import sys | 13 import sys |
13 import tempfile | 14 import tempfile |
| 15 import zipfile |
| 16 |
| 17 # resource_sizes modifies zipfile for zip64 compatibility. See |
| 18 # https://bugs.python.org/issue14315. |
| 19 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) |
| 20 import resource_sizes # pylint: disable=unused-import |
14 | 21 |
15 from util import build_utils | 22 from util import build_utils |
16 | 23 |
17 def RenameInflateAndAddPageAlignment( | 24 def RenameInflateAndAddPageAlignment( |
18 rezip_apk_jar_path, in_zip_file, out_zip_file): | 25 rezip_apk_jar_path, in_zip_file, out_zip_file): |
19 rezip_apk_cmd = [ | 26 rezip_apk_cmd = [ |
20 'java', | 27 'java', |
21 '-classpath', | 28 '-classpath', |
22 rezip_apk_jar_path, | 29 rezip_apk_jar_path, |
23 'RezipApk', | 30 'RezipApk', |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 apk_to_sign = apk_to_sign_tmp.name | 143 apk_to_sign = apk_to_sign_tmp.name |
137 RenameInflateAndAddPageAlignment( | 144 RenameInflateAndAddPageAlignment( |
138 options.rezip_apk_jar_path, options.unsigned_apk_path, apk_to_sign) | 145 options.rezip_apk_jar_path, options.unsigned_apk_path, apk_to_sign) |
139 else: | 146 else: |
140 apk_to_sign = options.unsigned_apk_path | 147 apk_to_sign = options.unsigned_apk_path |
141 | 148 |
142 signed_apk_path = signed_apk_path_tmp.name | 149 signed_apk_path = signed_apk_path_tmp.name |
143 JarSigner(options.key_path, options.key_name, options.key_passwd, | 150 JarSigner(options.key_path, options.key_name, options.key_passwd, |
144 apk_to_sign, signed_apk_path) | 151 apk_to_sign, signed_apk_path) |
145 | 152 |
| 153 # Make the signing files hermetic. |
| 154 with tempfile.NamedTemporaryFile(suffix='.zip') as hermetic_signed_apk: |
| 155 with zipfile.ZipFile(signed_apk_path, 'r') as zi: |
| 156 with zipfile.ZipFile(hermetic_signed_apk, 'w') as zo: |
| 157 for info in zi.infolist(): |
| 158 # Ignore 'extended local file headers'. Python doesn't write them |
| 159 # properly (see https://bugs.python.org/issue1742205) which causes |
| 160 # zipalign to miscalculate alignment. Since we don't use them except |
| 161 # for alignment anyway, we write a stripped file here and let |
| 162 # zipalign add them properly later. eLFHs are controlled by 'general |
| 163 # purpose bit flag 03' (0x08) so we mask that out. |
| 164 info.flag_bits = info.flag_bits & 0xF7 |
| 165 |
| 166 info.date_time = build_utils.HERMETIC_TIMESTAMP |
| 167 zo.writestr(info, zi.read(info.filename)) |
| 168 |
| 169 shutil.copy(hermetic_signed_apk.name, signed_apk_path) |
| 170 |
146 if options.load_library_from_zip: | 171 if options.load_library_from_zip: |
147 # Reorder the contents of the APK. This re-establishes the canonical | 172 # Reorder the contents of the APK. This re-establishes the canonical |
148 # order which means the library will be back at its page aligned location. | 173 # order which means the library will be back at its page aligned location. |
149 # This step also aligns uncompressed items to 4 bytes. | 174 # This step also aligns uncompressed items to 4 bytes. |
150 ReorderAndAlignApk( | 175 ReorderAndAlignApk( |
151 options.rezip_apk_jar_path, signed_apk_path, options.final_apk_path) | 176 options.rezip_apk_jar_path, signed_apk_path, options.final_apk_path) |
152 else: | 177 else: |
153 # Align uncompressed items to 4 bytes | 178 # Align uncompressed items to 4 bytes |
154 AlignApk(options.zipalign_path, | 179 AlignApk(options.zipalign_path, |
155 options.page_align_shared_libraries, | 180 options.page_align_shared_libraries, |
156 signed_apk_path, | 181 signed_apk_path, |
157 options.final_apk_path) | 182 options.final_apk_path) |
158 | 183 |
159 | 184 |
160 if __name__ == '__main__': | 185 if __name__ == '__main__': |
161 sys.exit(main(sys.argv[1:])) | 186 sys.exit(main(sys.argv[1:])) |
OLD | NEW |