1 #!/usr/bin/env python 2 # 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 5 # found in the LICENSE file. 6 """Signs and zipaligns APK. 7 8 """ 9 10 import optparse 11 import shutil 12 import sys 13 import tempfile 14 15 from util import build_utils 16 17 def SignApk(key_path, key_name, key_passwd, unsigned_path, signed_path): 18 shutil.copy(unsigned_path, signed_path) 19 sign_cmd = [ 20 'jarsigner', 21 '-sigalg', 'MD5withRSA', 22 '-digestalg', 'SHA1', 23 '-keystore', key_path, 24 '-storepass', key_passwd, 25 signed_path, 26 key_name, 27 ] 28 build_utils.CheckOutput(sign_cmd) 29 30 31 def AlignApk(zipalign_path, unaligned_path, final_path): 32 align_cmd = [ 33 zipalign_path, 34 '-f', '4', # 4 bytes 35 unaligned_path, 36 final_path, 37 ] 38 build_utils.CheckOutput(align_cmd) 39 40 41 def main(): 42 parser = optparse.OptionParser() 43 44 parser.add_option('--zipalign-path', help='Path to the zipalign tool.') 45 parser.add_option('--unsigned-apk-path', help='Path to input unsigned APK.') 46 parser.add_option('--final-apk-path', 47 help='Path to output signed and aligned APK.') 48 parser.add_option('--key-path', help='Path to keystore for signing.') 49 parser.add_option('--key-passwd', help='Keystore password') 50 parser.add_option('--key-name', help='Keystore name') 51 parser.add_option('--stamp', help='Path to touch on success.') 52 53 options, _ = parser.parse_args() 54 55 with tempfile.NamedTemporaryFile() as intermediate_file: 56 signed_apk_path = intermediate_file.name 57 SignApk(options.key_path, options.key_name, options.key_passwd, 58 options.unsigned_apk_path, signed_apk_path) 59 AlignApk(options.zipalign_path, signed_apk_path, options.final_apk_path) 60 61 if options.stamp: 62 build_utils.Touch(options.stamp) 63 64 65 if __name__ == '__main__': 66 sys.exit(main()) 67 68 69