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
|
2016-08-02 08:34:44 +03:00
|
|
|
import os
|
2013-04-10 02:22:15 +04:00
|
|
|
import shutil
|
|
|
|
import sys
|
2013-06-25 22:42:07 +04:00
|
|
|
import tempfile
|
2016-08-02 08:34:44 +03:00
|
|
|
import zipfile
|
|
|
|
|
|
|
|
# resource_sizes modifies zipfile for zip64 compatibility. See
|
|
|
|
# https://bugs.python.org/issue14315.
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir))
|
|
|
|
import resource_sizes # pylint: disable=unused-import
|
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-10-02 22:20:18 +04:00
|
|
|
def RenameInflateAndAddPageAlignment(
|
|
|
|
rezip_apk_jar_path, in_zip_file, out_zip_file):
|
2014-09-30 21:15:32 +04:00
|
|
|
rezip_apk_cmd = [
|
|
|
|
'java',
|
|
|
|
'-classpath',
|
|
|
|
rezip_apk_jar_path,
|
|
|
|
'RezipApk',
|
2014-10-02 22:20:18 +04:00
|
|
|
'renamealign',
|
2014-06-27 20:59:46 +04:00
|
|
|
in_zip_file,
|
|
|
|
out_zip_file,
|
|
|
|
]
|
2014-09-30 21:15:32 +04:00
|
|
|
build_utils.CheckOutput(rezip_apk_cmd)
|
|
|
|
|
|
|
|
|
|
|
|
def ReorderAndAlignApk(rezip_apk_jar_path, in_zip_file, out_zip_file):
|
|
|
|
rezip_apk_cmd = [
|
|
|
|
'java',
|
|
|
|
'-classpath',
|
|
|
|
rezip_apk_jar_path,
|
|
|
|
'RezipApk',
|
|
|
|
'reorder',
|
|
|
|
in_zip_file,
|
|
|
|
out_zip_file,
|
|
|
|
]
|
|
|
|
build_utils.CheckOutput(rezip_apk_cmd)
|
2014-06-27 20:59:46 +04:00
|
|
|
|
|
|
|
|
2014-09-30 21:15:32 +04:00
|
|
|
def JarSigner(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
|
|
|
|
|
|
|
|
2016-01-26 03:43:10 +03:00
|
|
|
def AlignApk(zipalign_path, package_align, unaligned_path, final_path):
|
2013-04-10 02:22:15 +04:00
|
|
|
align_cmd = [
|
2014-04-19 00:22:43 +04:00
|
|
|
zipalign_path,
|
2016-01-26 03:43:10 +03:00
|
|
|
'-f'
|
|
|
|
]
|
|
|
|
|
|
|
|
if package_align:
|
|
|
|
align_cmd += ['-p']
|
|
|
|
|
|
|
|
align_cmd += [
|
|
|
|
'4', # 4 bytes
|
2013-04-10 02:22:15 +04:00
|
|
|
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
|
|
|
|
|
|
|
|
2015-09-25 04:15:47 +03:00
|
|
|
def main(args):
|
|
|
|
args = build_utils.ExpandFileArgs(args)
|
|
|
|
|
2013-04-10 02:22:15 +04:00
|
|
|
parser = optparse.OptionParser()
|
2014-08-06 11:29:42 +04:00
|
|
|
build_utils.AddDepfileOption(parser)
|
2013-04-10 02:22:15 +04:00
|
|
|
|
2014-09-30 21:15:32 +04:00
|
|
|
parser.add_option('--rezip-apk-jar-path',
|
|
|
|
help='Path to the RezipApk jar file.')
|
2014-04-19 00:22:43 +04:00
|
|
|
parser.add_option('--zipalign-path', help='Path to the zipalign tool.')
|
2016-01-26 03:43:10 +03:00
|
|
|
parser.add_option('--page-align-shared-libraries',
|
|
|
|
action='store_true',
|
|
|
|
help='Page align shared libraries.')
|
2013-04-10 02:22:15 +04:00
|
|
|
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.')
|
2015-05-21 07:38:34 +03:00
|
|
|
parser.add_option('--load-library-from-zip', type='int',
|
2014-06-27 20:59:46 +04:00
|
|
|
help='If non-zero, build the APK such that the library can be loaded ' +
|
|
|
|
'directly from the zip file using the crazy linker. The library ' +
|
|
|
|
'will be renamed, uncompressed and page aligned.')
|
2013-04-10 02:22:15 +04:00
|
|
|
|
|
|
|
options, _ = parser.parse_args()
|
|
|
|
|
2015-09-25 04:15:47 +03:00
|
|
|
input_paths = [
|
|
|
|
options.unsigned_apk_path,
|
|
|
|
options.key_path,
|
|
|
|
]
|
|
|
|
|
|
|
|
if options.load_library_from_zip:
|
|
|
|
input_paths.append(options.rezip_apk_jar_path)
|
2015-05-23 00:32:09 +03:00
|
|
|
|
2015-09-25 04:15:47 +03:00
|
|
|
input_strings = [
|
|
|
|
options.load_library_from_zip,
|
|
|
|
options.key_name,
|
|
|
|
options.key_passwd,
|
2016-01-26 03:43:10 +03:00
|
|
|
options.page_align_shared_libraries,
|
2015-09-25 04:15:47 +03:00
|
|
|
]
|
2015-05-23 00:32:09 +03:00
|
|
|
|
2015-09-25 04:15:47 +03:00
|
|
|
build_utils.CallAndWriteDepfileIfStale(
|
|
|
|
lambda: FinalizeApk(options),
|
|
|
|
options,
|
|
|
|
record_path=options.unsigned_apk_path + '.finalize.md5.stamp',
|
|
|
|
input_paths=input_paths,
|
|
|
|
input_strings=input_strings,
|
|
|
|
output_paths=[options.final_apk_path])
|
2015-05-23 00:32:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
def FinalizeApk(options):
|
2014-06-27 20:59:46 +04:00
|
|
|
with tempfile.NamedTemporaryFile() as signed_apk_path_tmp, \
|
2014-10-02 22:20:18 +04:00
|
|
|
tempfile.NamedTemporaryFile() as apk_to_sign_tmp:
|
2014-06-27 20:59:46 +04:00
|
|
|
|
2015-05-21 07:38:34 +03:00
|
|
|
if options.load_library_from_zip:
|
2014-06-27 20:59:46 +04:00
|
|
|
# We alter the name of the library so that the Android Package Manager
|
|
|
|
# does not extract it into a separate file. This must be done before
|
2014-09-30 21:15:32 +04:00
|
|
|
# signing, as the filename is part of the signed manifest. At the same
|
|
|
|
# time we uncompress the library, which is necessary so that it can be
|
|
|
|
# loaded directly from the APK.
|
|
|
|
# Move the library to a page boundary by adding a page alignment file.
|
2014-10-02 22:20:18 +04:00
|
|
|
apk_to_sign = apk_to_sign_tmp.name
|
|
|
|
RenameInflateAndAddPageAlignment(
|
|
|
|
options.rezip_apk_jar_path, options.unsigned_apk_path, apk_to_sign)
|
2014-06-27 20:59:46 +04:00
|
|
|
else:
|
|
|
|
apk_to_sign = options.unsigned_apk_path
|
|
|
|
|
|
|
|
signed_apk_path = signed_apk_path_tmp.name
|
2014-09-30 21:15:32 +04:00
|
|
|
JarSigner(options.key_path, options.key_name, options.key_passwd,
|
|
|
|
apk_to_sign, signed_apk_path)
|
2014-06-27 20:59:46 +04:00
|
|
|
|
2016-08-02 08:34:44 +03:00
|
|
|
# Make the signing files hermetic.
|
|
|
|
with tempfile.NamedTemporaryFile(suffix='.zip') as hermetic_signed_apk:
|
|
|
|
with zipfile.ZipFile(signed_apk_path, 'r') as zi:
|
|
|
|
with zipfile.ZipFile(hermetic_signed_apk, 'w') as zo:
|
|
|
|
for info in zi.infolist():
|
|
|
|
# Ignore 'extended local file headers'. Python doesn't write them
|
|
|
|
# properly (see https://bugs.python.org/issue1742205) which causes
|
|
|
|
# zipalign to miscalculate alignment. Since we don't use them except
|
|
|
|
# for alignment anyway, we write a stripped file here and let
|
|
|
|
# zipalign add them properly later. eLFHs are controlled by 'general
|
|
|
|
# purpose bit flag 03' (0x08) so we mask that out.
|
|
|
|
info.flag_bits = info.flag_bits & 0xF7
|
|
|
|
|
|
|
|
info.date_time = build_utils.HERMETIC_TIMESTAMP
|
|
|
|
zo.writestr(info, zi.read(info.filename))
|
|
|
|
|
|
|
|
shutil.copy(hermetic_signed_apk.name, signed_apk_path)
|
|
|
|
|
2015-05-21 07:38:34 +03:00
|
|
|
if options.load_library_from_zip:
|
2014-09-30 21:15:32 +04:00
|
|
|
# Reorder the contents of the APK. This re-establishes the canonical
|
|
|
|
# order which means the library will be back at its page aligned location.
|
|
|
|
# This step also aligns uncompressed items to 4 bytes.
|
|
|
|
ReorderAndAlignApk(
|
|
|
|
options.rezip_apk_jar_path, signed_apk_path, options.final_apk_path)
|
2014-06-27 20:59:46 +04:00
|
|
|
else:
|
2014-09-30 21:15:32 +04:00
|
|
|
# Align uncompressed items to 4 bytes
|
2016-01-26 03:43:10 +03:00
|
|
|
AlignApk(options.zipalign_path,
|
|
|
|
options.page_align_shared_libraries,
|
|
|
|
signed_apk_path,
|
|
|
|
options.final_apk_path)
|
2013-04-10 02:22:15 +04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-09-25 04:15:47 +03:00
|
|
|
sys.exit(main(sys.argv[1:]))
|