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