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 current_apk_file_path = options.unsigned_apk_path |
56 signed_apk_path = intermediate_file.name | 91 |
92 with tempfile.NamedTemporaryFile() as signed_apk_path_tmp, \ | |
93 tempfile.NamedTemporaryFile() as apk_to_sign_tmp, \ | |
94 tempfile.NamedTemporaryFile() as apk_without_descriptors_tmp, \ | |
95 tempfile.NamedTemporaryFile() as aligned_apk_tmp: | |
96 | |
97 if options.load_library_from_zip_file: | |
rmcilroy
2014/06/27 10:02:15
I still think the flow of this function is a bit u
Anton
2014/06/27 10:32:58
Done.
| |
98 # We alter the name of the library so that the Android Package Manager | |
99 # does not extract it into a separate file. This must be done before | |
100 # signing, as the filename is part of the signed manifest. | |
101 apk_to_sign = apk_to_sign_tmp.name | |
102 RenameLibInApk(options.rezip_path, current_apk_file_path, apk_to_sign) | |
103 current_apk_file_path = apk_to_sign | |
104 | |
105 signed_apk_path = signed_apk_path_tmp.name | |
57 SignApk(options.key_path, options.key_name, options.key_passwd, | 106 SignApk(options.key_path, options.key_name, options.key_passwd, |
58 options.unsigned_apk_path, signed_apk_path) | 107 current_apk_file_path, signed_apk_path) |
59 AlignApk(options.zipalign_path, signed_apk_path, options.final_apk_path) | 108 current_apk_file_path = signed_apk_path |
109 | |
110 if options.load_library_from_zip_file: | |
111 # Signing adds data descriptors to the APK. These are redundant | |
112 # information. We remove them as otherwise they can cause a | |
113 # miscalculation in the page alignment. | |
114 apk_to_align = apk_without_descriptors_tmp.name | |
115 DropDataDescriptorsInApk( | |
116 options.rezip_path, current_apk_file_path, apk_to_align) | |
117 current_apk_file_path = apk_to_align | |
118 aligned_apk = aligned_apk_tmp.name | |
119 else: | |
120 aligned_apk = options.final_apk_path | |
121 | |
122 # Align uncompress items to 4 bytes | |
123 AlignApk(options.zipalign_path, current_apk_file_path, aligned_apk) | |
124 | |
125 if options.load_library_from_zip_file: | |
126 # Uncompress the library and make sure that it is page aligned. | |
127 UncompressLibAndPageAlignInApk( | |
128 options.rezip_path, aligned_apk, options.final_apk_path) | |
60 | 129 |
61 if options.stamp: | 130 if options.stamp: |
62 build_utils.Touch(options.stamp) | 131 build_utils.Touch(options.stamp) |
63 | 132 |
64 | 133 |
65 if __name__ == '__main__': | 134 if __name__ == '__main__': |
66 sys.exit(main()) | 135 sys.exit(main()) |
67 | |
68 | |
OLD | NEW |