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 shutil | 11 import shutil |
12 import sys | 12 import sys |
13 import tempfile | 13 import tempfile |
14 | 14 |
15 from util import build_utils | 15 from util import build_utils |
16 | 16 |
| 17 def RenameLibInApk(rezip_path, in_zip_file, out_zip_file): |
| 18 rename_cmd = [ |
| 19 rezip_path, |
| 20 'rename', |
| 21 in_zip_file, |
| 22 out_zip_file, |
| 23 ] |
| 24 build_utils.CheckOutput(rename_cmd) |
| 25 |
| 26 |
17 def SignApk(key_path, key_name, key_passwd, unsigned_path, signed_path): | 27 def SignApk(key_path, key_name, key_passwd, unsigned_path, signed_path): |
18 shutil.copy(unsigned_path, signed_path) | 28 shutil.copy(unsigned_path, signed_path) |
19 sign_cmd = [ | 29 sign_cmd = [ |
20 'jarsigner', | 30 'jarsigner', |
21 '-sigalg', 'MD5withRSA', | 31 '-sigalg', 'MD5withRSA', |
22 '-digestalg', 'SHA1', | 32 '-digestalg', 'SHA1', |
23 '-keystore', key_path, | 33 '-keystore', key_path, |
24 '-storepass', key_passwd, | 34 '-storepass', key_passwd, |
25 signed_path, | 35 signed_path, |
26 key_name, | 36 key_name, |
27 ] | 37 ] |
28 build_utils.CheckOutput(sign_cmd) | 38 build_utils.CheckOutput(sign_cmd) |
29 | 39 |
30 | 40 |
31 def AlignApk(zipalign_path, unaligned_path, final_path): | 41 def AlignApk(zipalign_path, unaligned_path, final_path): |
32 align_cmd = [ | 42 align_cmd = [ |
33 zipalign_path, | 43 zipalign_path, |
34 '-f', '4', # 4 bytes | 44 '-f', '4', # 4 bytes |
35 unaligned_path, | 45 unaligned_path, |
36 final_path, | 46 final_path, |
37 ] | 47 ] |
38 build_utils.CheckOutput(align_cmd) | 48 build_utils.CheckOutput(align_cmd) |
39 | 49 |
40 | 50 |
| 51 def UncompressLibAndPageAlignInApk(rezip_path, in_zip_file, out_zip_file): |
| 52 rename_cmd = [ |
| 53 rezip_path, |
| 54 'inflatealign', |
| 55 in_zip_file, |
| 56 out_zip_file, |
| 57 ] |
| 58 build_utils.CheckOutput(rename_cmd) |
| 59 |
| 60 |
| 61 def DropDataDescriptorsInApk(rezip_path, in_zip_file, out_zip_file): |
| 62 rename_cmd = [ |
| 63 rezip_path, |
| 64 'dropdescriptors', |
| 65 in_zip_file, |
| 66 out_zip_file, |
| 67 ] |
| 68 build_utils.CheckOutput(rename_cmd) |
| 69 |
| 70 |
41 def main(): | 71 def main(): |
42 parser = optparse.OptionParser() | 72 parser = optparse.OptionParser() |
43 | 73 |
44 parser.add_option('--zipalign-path', help='Path to the zipalign tool.') | 74 parser.add_option('--zipalign-path', help='Path to the zipalign tool.') |
| 75 parser.add_option('--rezip-path', help='Path to the rezip executable.') |
45 parser.add_option('--unsigned-apk-path', help='Path to input unsigned APK.') | 76 parser.add_option('--unsigned-apk-path', help='Path to input unsigned APK.') |
46 parser.add_option('--final-apk-path', | 77 parser.add_option('--final-apk-path', |
47 help='Path to output signed and aligned APK.') | 78 help='Path to output signed and aligned APK.') |
48 parser.add_option('--key-path', help='Path to keystore for signing.') | 79 parser.add_option('--key-path', help='Path to keystore for signing.') |
49 parser.add_option('--key-passwd', help='Keystore password') | 80 parser.add_option('--key-passwd', help='Keystore password') |
50 parser.add_option('--key-name', help='Keystore name') | 81 parser.add_option('--key-name', help='Keystore name') |
51 parser.add_option('--stamp', help='Path to touch on success.') | 82 parser.add_option('--stamp', help='Path to touch on success.') |
| 83 parser.add_option('--load-library-from-zip-file', type='int', |
| 84 help='If non-zero, build the APK such that the library can be loaded ' + |
| 85 'directly from the zip file using the crazy linker. The library ' + |
| 86 'will be renamed, uncompressed and page aligned.') |
52 | 87 |
53 options, _ = parser.parse_args() | 88 options, _ = parser.parse_args() |
54 | 89 |
55 with tempfile.NamedTemporaryFile() as intermediate_file: | 90 with tempfile.NamedTemporaryFile() as signed_apk_path_tmp, \ |
56 signed_apk_path = intermediate_file.name | 91 tempfile.NamedTemporaryFile() as apk_to_sign_tmp, \ |
| 92 tempfile.NamedTemporaryFile() as apk_without_descriptors_tmp, \ |
| 93 tempfile.NamedTemporaryFile() as aligned_apk_tmp: |
| 94 |
| 95 if options.load_library_from_zip_file: |
| 96 # We alter the name of the library so that the Android Package Manager |
| 97 # does not extract it into a separate file. This must be done before |
| 98 # signing, as the filename is part of the signed manifest. |
| 99 apk_to_sign = apk_to_sign_tmp.name |
| 100 RenameLibInApk(options.rezip_path, options.unsigned_apk_path, apk_to_sign) |
| 101 else: |
| 102 apk_to_sign = options.unsigned_apk_path |
| 103 |
| 104 signed_apk_path = signed_apk_path_tmp.name |
57 SignApk(options.key_path, options.key_name, options.key_passwd, | 105 SignApk(options.key_path, options.key_name, options.key_passwd, |
58 options.unsigned_apk_path, signed_apk_path) | 106 apk_to_sign, signed_apk_path) |
59 AlignApk(options.zipalign_path, signed_apk_path, options.final_apk_path) | 107 |
| 108 if options.load_library_from_zip_file: |
| 109 # Signing adds data descriptors to the APK. These are redundant |
| 110 # information. We remove them as otherwise they can cause a |
| 111 # miscalculation in the page alignment. |
| 112 apk_to_align = apk_without_descriptors_tmp.name |
| 113 DropDataDescriptorsInApk( |
| 114 options.rezip_path, signed_apk_path, apk_to_align) |
| 115 aligned_apk = aligned_apk_tmp.name |
| 116 else: |
| 117 apk_to_align = signed_apk_path |
| 118 aligned_apk = options.final_apk_path |
| 119 |
| 120 # Align uncompress items to 4 bytes |
| 121 AlignApk(options.zipalign_path, apk_to_align, aligned_apk) |
| 122 |
| 123 if options.load_library_from_zip_file: |
| 124 # Uncompress the library and make sure that it is page aligned. |
| 125 UncompressLibAndPageAlignInApk( |
| 126 options.rezip_path, aligned_apk, options.final_apk_path) |
60 | 127 |
61 if options.stamp: | 128 if options.stamp: |
62 build_utils.Touch(options.stamp) | 129 build_utils.Touch(options.stamp) |
63 | 130 |
64 | 131 |
65 if __name__ == '__main__': | 132 if __name__ == '__main__': |
66 sys.exit(main()) | 133 sys.exit(main()) |
67 | |
68 | |
OLD | NEW |