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('--uncompress-lib', type='int', | |
84 help='If non-zero, build an APK with the library uncompressed.') | |
rmcilroy
2014/06/25 17:25:19
update help comment to mention libraries are renam
Anton
2014/06/26 09:45:43
Done.
| |
52 | 85 |
53 options, _ = parser.parse_args() | 86 options, _ = parser.parse_args() |
54 | 87 |
55 with tempfile.NamedTemporaryFile() as intermediate_file: | 88 in_apk = options.unsigned_apk_path |
rmcilroy
2014/06/25 17:25:19
current_apk_file_path
Anton
2014/06/26 09:45:43
Done.
| |
89 | |
90 with tempfile.NamedTemporaryFile() as intermediate_file, \ | |
rmcilroy
2014/06/25 17:25:19
rename intermediate_file appropriately (signed_apk
Anton
2014/06/26 09:45:43
Done.
| |
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.uncompress_lib: | |
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, in_apk, apk_to_sign) | |
101 in_apk = apk_to_sign | |
102 | |
56 signed_apk_path = intermediate_file.name | 103 signed_apk_path = intermediate_file.name |
57 SignApk(options.key_path, options.key_name, options.key_passwd, | 104 SignApk(options.key_path, options.key_name, options.key_passwd, |
58 options.unsigned_apk_path, signed_apk_path) | 105 in_apk, signed_apk_path) |
59 AlignApk(options.zipalign_path, signed_apk_path, options.final_apk_path) | 106 in_apk = signed_apk_path |
107 | |
108 if options.uncompress_lib: | |
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(options.rezip_path, in_apk, apk_to_align) | |
114 in_apk = apk_to_align | |
115 aligned_apk = aligned_apk_tmp.name | |
116 else: | |
117 aligned_apk = options.final_apk_path | |
rmcilroy
2014/06/25 17:25:19
Do the temp name tracking one way throughout the f
Anton
2014/06/26 09:45:43
This is not the current_apk_file_path
| |
118 | |
119 # Align uncompress items to 4 bytes | |
120 AlignApk(options.zipalign_path, in_apk, aligned_apk) | |
rmcilroy
2014/06/25 17:25:19
you need to move the aligned_apk to options.final_
Anton
2014/06/26 09:45:44
This is what L117 is doing.
| |
121 | |
122 if options.uncompress_lib: | |
123 # Uncompress the library and make sure that it is page aligned. | |
124 UncompressLibAndPageAlignInApk( | |
125 options.rezip_path, aligned_apk, options.final_apk_path) | |
60 | 126 |
61 if options.stamp: | 127 if options.stamp: |
62 build_utils.Touch(options.stamp) | 128 build_utils.Touch(options.stamp) |
63 | 129 |
64 | 130 |
65 if __name__ == '__main__': | 131 if __name__ == '__main__': |
66 sys.exit(main()) | 132 sys.exit(main()) |
67 | |
68 | |
OLD | NEW |