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 | |
71 def RemoveTmpApk(tmp_apk): | |
72 rm_cmd = [ | |
73 'rm', | |
74 tmp_apk, | |
75 ] | |
76 build_utils.CheckOutput(rm_cmd) | |
77 | |
78 | |
41 def main(): | 79 def main(): |
42 parser = optparse.OptionParser() | 80 parser = optparse.OptionParser() |
43 | 81 |
44 parser.add_option('--zipalign-path', help='Path to the zipalign tool.') | 82 parser.add_option('--zipalign-path', help='Path to the zipalign tool.') |
45 parser.add_option('--unsigned-apk-path', help='Path to input unsigned APK.') | 83 parser.add_option('--unsigned-apk-path', help='Path to input unsigned APK.') |
46 parser.add_option('--final-apk-path', | 84 parser.add_option('--final-apk-path', |
47 help='Path to output signed and aligned APK.') | 85 help='Path to output signed and aligned APK.') |
48 parser.add_option('--key-path', help='Path to keystore for signing.') | 86 parser.add_option('--key-path', help='Path to keystore for signing.') |
49 parser.add_option('--key-passwd', help='Keystore password') | 87 parser.add_option('--key-passwd', help='Keystore password') |
50 parser.add_option('--key-name', help='Keystore name') | 88 parser.add_option('--key-name', help='Keystore name') |
51 parser.add_option('--stamp', help='Path to touch on success.') | 89 parser.add_option('--stamp', help='Path to touch on success.') |
90 parser.add_option('--uncompress-lib', type='int', | |
rmcilroy
2014/06/19 09:46:12
As discussed below, I think you should also add a
Anton
2014/06/19 13:39:45
It is over engineering to implement functionality
rmcilroy
2014/06/20 14:42:59
I disagree, for testing and debugging as stated ab
| |
91 help='If non-zero, build an APK with the library uncompressed') | |
rmcilroy
2014/06/19 09:46:12
Please make this an "action='store_true'" type of
Anton
2014/06/19 13:39:45
This just makes things more complicated with no up
| |
92 parser.add_option('--rezip-path', help='Path to the rezip executable') | |
rmcilroy
2014/06/19 09:46:12
nit - move this up to be below --zipalign-path
Anton
2014/06/19 13:39:45
Done.
| |
52 | 93 |
53 options, _ = parser.parse_args() | 94 options, _ = parser.parse_args() |
54 | 95 |
55 with tempfile.NamedTemporaryFile() as intermediate_file: | 96 with tempfile.NamedTemporaryFile() as intermediate_file: |
56 signed_apk_path = intermediate_file.name | 97 signed_apk_path = intermediate_file.name |
57 SignApk(options.key_path, options.key_name, options.key_passwd, | 98 if options.uncompress_lib: |
58 options.unsigned_apk_path, signed_apk_path) | 99 # In this branch we are building an APK with the shared library |
59 AlignApk(options.zipalign_path, signed_apk_path, options.final_apk_path) | 100 # uncompressed and page aligned, so that it can be loaded directly |
101 # by the android chrome linker. | |
102 # | |
103 # We alter the name of the library so that the Android Package Manager | |
104 # does not extract it into a separate file. This must be done before | |
105 # signing as the filename is part of the signed manifest. | |
106 apk_to_sign = intermediate_file.name + ".librenamed" | |
rmcilroy
2014/06/19 09:46:12
Use the same "with tempfile.NamedTemporaryFile()"
Anton
2014/06/19 13:39:45
Done.
| |
107 RenameLibInApk(options.rezip_path, options.unsigned_apk_path, apk_to_sign) | |
108 SignApk(options.key_path, options.key_name, options.key_passwd, | |
109 apk_to_sign, signed_apk_path) | |
110 RemoveTmpApk(apk_to_sign) | |
111 | |
112 # Signing adds data descriptors to the APK. These are redundant | |
113 # information. We remove them as otherwise they can cause a | |
114 # miscalculation in the page alignment. | |
115 apk_without_descriptors = intermediate_file.name + ".without_descriptors" | |
116 DropDataDescriptorsInApk( | |
117 options.rezip_path, signed_apk_path, apk_without_descriptors) | |
118 | |
119 # Align uncompress items to 4 bytes (as normal for all APKs). | |
120 aligned_apk = intermediate_file.name + ".aligned" | |
121 AlignApk(options.zipalign_path, apk_without_descriptors, aligned_apk) | |
122 RemoveTmpApk(apk_without_descriptors) | |
123 | |
124 # Uncompress the library and make sure that it is page aligned. | |
125 UncompressLibAndPageAlignInApk( | |
126 options.rezip_path, aligned_apk, options.final_apk_path) | |
127 RemoveTmpApk(aligned_apk) | |
128 else: | |
rmcilroy
2014/06/19 09:46:12
I think this is pretty hacky to have two complete
Anton
2014/06/19 13:39:45
Actually I separated it into two branches which ma
rmcilroy
2014/06/20 14:42:59
I disagree entirely - it makes it more difficult t
| |
129 SignApk(options.key_path, options.key_name, options.key_passwd, | |
130 options.unsigned_apk_path, signed_apk_path) | |
131 AlignApk(options.zipalign_path, signed_apk_path, options.final_apk_path) | |
60 | 132 |
61 if options.stamp: | 133 if options.stamp: |
62 build_utils.Touch(options.stamp) | 134 build_utils.Touch(options.stamp) |
63 | 135 |
64 | 136 |
65 if __name__ == '__main__': | 137 if __name__ == '__main__': |
66 sys.exit(main()) | 138 sys.exit(main()) |
67 | |
68 | |
OLD | NEW |