2013-04-10 02:22:15 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright 2013 The Chromium Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
"""Signs and zipaligns APK.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
2013-06-25 22:42:07 +04:00
|
|
|
import tempfile
|
2013-04-10 02:22:15 +04:00
|
|
|
|
2014-02-11 05:47:41 +04:00
|
|
|
from util import build_utils
|
2013-04-10 02:22:15 +04:00
|
|
|
|
2014-03-26 04:43:37 +04:00
|
|
|
def SignApk(key_path, key_name, key_passwd, unsigned_path, signed_path):
|
2013-06-25 22:42:07 +04:00
|
|
|
shutil.copy(unsigned_path, signed_path)
|
2013-04-10 02:22:15 +04:00
|
|
|
sign_cmd = [
|
|
|
|
'jarsigner',
|
|
|
|
'-sigalg', 'MD5withRSA',
|
|
|
|
'-digestalg', 'SHA1',
|
2014-03-26 04:43:37 +04:00
|
|
|
'-keystore', key_path,
|
|
|
|
'-storepass', key_passwd,
|
2013-06-25 22:42:07 +04:00
|
|
|
signed_path,
|
2014-03-26 04:43:37 +04:00
|
|
|
key_name,
|
2013-04-10 02:22:15 +04:00
|
|
|
]
|
2013-12-07 03:24:37 +04:00
|
|
|
build_utils.CheckOutput(sign_cmd)
|
2013-04-10 02:22:15 +04:00
|
|
|
|
|
|
|
|
|
|
|
def AlignApk(android_sdk_root, unaligned_path, final_path):
|
|
|
|
align_cmd = [
|
|
|
|
os.path.join(android_sdk_root, 'tools', 'zipalign'),
|
|
|
|
'-f', '4', # 4 bytes
|
|
|
|
unaligned_path,
|
|
|
|
final_path,
|
|
|
|
]
|
2013-12-07 03:24:37 +04:00
|
|
|
build_utils.CheckOutput(align_cmd)
|
2013-04-10 02:22:15 +04:00
|
|
|
|
|
|
|
|
2014-03-21 03:09:28 +04:00
|
|
|
def main():
|
2013-04-10 02:22:15 +04:00
|
|
|
parser = optparse.OptionParser()
|
|
|
|
|
|
|
|
parser.add_option('--android-sdk-root', help='Android sdk root directory.')
|
|
|
|
parser.add_option('--unsigned-apk-path', help='Path to input unsigned APK.')
|
|
|
|
parser.add_option('--final-apk-path',
|
|
|
|
help='Path to output signed and aligned APK.')
|
2014-03-26 04:43:37 +04:00
|
|
|
parser.add_option('--key-path', help='Path to keystore for signing.')
|
|
|
|
parser.add_option('--key-passwd', help='Keystore password')
|
|
|
|
parser.add_option('--key-name', help='Keystore name')
|
2013-04-10 02:22:15 +04:00
|
|
|
parser.add_option('--stamp', help='Path to touch on success.')
|
|
|
|
|
|
|
|
options, _ = parser.parse_args()
|
|
|
|
|
2013-06-25 22:42:07 +04:00
|
|
|
with tempfile.NamedTemporaryFile() as intermediate_file:
|
|
|
|
signed_apk_path = intermediate_file.name
|
2014-03-26 04:43:37 +04:00
|
|
|
SignApk(options.key_path, options.key_name, options.key_passwd,
|
|
|
|
options.unsigned_apk_path, signed_apk_path)
|
2013-06-25 22:42:07 +04:00
|
|
|
AlignApk(options.android_sdk_root, signed_apk_path, options.final_apk_path)
|
2013-04-10 02:22:15 +04:00
|
|
|
|
|
|
|
if options.stamp:
|
|
|
|
build_utils.Touch(options.stamp)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-03-21 03:09:28 +04:00
|
|
|
sys.exit(main())
|
2013-04-10 02:22:15 +04:00
|
|
|
|
|
|
|
|