From 182f6432bfeb3239b20d559fe40e8b27cd6db930 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Thu, 10 May 2018 20:40:51 +0000 Subject: [PATCH] android: Merge package_apk and finalize_apk steps. This simplifies the process of creating an APK, and will make it easier to create APKs with overlapping files. Bug: 835622 Change-Id: I57f884fc41d86c16ce81ad2adb703ffda64198cc Reviewed-on: https://chromium-review.googlesource.com/1041566 Commit-Queue: Peter Collingbourne Reviewed-by: Egor Pasko Reviewed-by: agrieve Cr-Original-Commit-Position: refs/heads/master@{#557659} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: 52ad8239d50763b4828094927b8bc046ff7d97c8 --- android/gyp/apkbuilder.py | 17 ++++- android/gyp/finalize_apk.py | 29 ------- config/android/internal_rules.gni | 123 +++++++++--------------------- 3 files changed, 51 insertions(+), 118 deletions(-) mode change 100755 => 100644 android/gyp/finalize_apk.py diff --git a/android/gyp/apkbuilder.py b/android/gyp/apkbuilder.py index 75aafc7d0..d385c1c7e 100755 --- a/android/gyp/apkbuilder.py +++ b/android/gyp/apkbuilder.py @@ -13,6 +13,8 @@ import shutil import sys import zipfile +import finalize_apk + from util import build_utils @@ -77,6 +79,16 @@ def _ParseArgs(args): parser.add_argument('--uncompress-shared-libraries', action='store_true', help='Uncompress shared libraries') + parser.add_argument('--apksigner-path', required=True, + help='Path to the apksigner executable.') + parser.add_argument('--zipalign-path', required=True, + help='Path to the zipalign executable.') + parser.add_argument('--key-path', required=True, + help='Path to keystore for signing.') + parser.add_argument('--key-passwd', required=True, + help='Keystore password') + parser.add_argument('--key-name', required=True, + help='Keystore name') options = parser.parse_args(args) options.assets = build_utils.ParseGnList(options.assets) options.uncompressed_assets = build_utils.ParseGnList( @@ -347,7 +359,10 @@ def main(args): if options.apk_res_info_path: _MergeResInfoFiles(options.apk_res_info_path, options.resource_apk) - shutil.move(tmp_apk, options.output_apk) + finalize_apk.FinalizeApk(options.apksigner_path, options.zipalign_path, + tmp_apk, options.output_apk, + options.key_path, options.key_passwd, + options.key_name) finally: if os.path.exists(tmp_apk): os.unlink(tmp_apk) diff --git a/android/gyp/finalize_apk.py b/android/gyp/finalize_apk.py old mode 100755 new mode 100644 index 694e4db13..2440fe40a --- a/android/gyp/finalize_apk.py +++ b/android/gyp/finalize_apk.py @@ -1,5 +1,3 @@ -#!/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. @@ -32,30 +30,3 @@ def FinalizeApk(apksigner_path, zipalign_path, unsigned_apk_path, ]) shutil.move(staging_file.name, final_apk_path) staging_file.delete = False - - -def main(): - parser = argparse.ArgumentParser() - - parser.add_argument('--apksigner-path', required=True, - help='Path to the apksigner executable.') - parser.add_argument('--zipalign-path', required=True, - help='Path to the zipalign executable.') - parser.add_argument('--unsigned-apk-path', required=True, - help='Path to input unsigned APK.') - parser.add_argument('--final-apk-path', required=True, - help='Path to output signed and aligned APK.') - parser.add_argument('--key-path', required=True, - help='Path to keystore for signing.') - parser.add_argument('--key-passwd', required=True, - help='Keystore password') - parser.add_argument('--key-name', required=True, - help='Keystore name') - options = parser.parse_args() - FinalizeApk(options.apksigner_path, options.zipalign_path, - options.unsigned_apk_path, options.final_apk_path, - options.key_path, options.key_passwd, options.key_name) - - -if __name__ == '__main__': - main() diff --git a/config/android/internal_rules.gni b/config/android/internal_rules.gni index 6068bfaf3..6b51ea9e7 100644 --- a/config/android/internal_rules.gni +++ b/config/android/internal_rules.gni @@ -2013,7 +2013,7 @@ if (enable_java_templates) { } } - # Creates an unsigned .apk. + # Creates a signed and aligned .apk. # # Variables # assets_build_config: Path to android_apk .build_config containing merged @@ -2030,6 +2030,9 @@ if (enable_java_templates) { # native_libs_filearg: @FileArg() of additionally native libraries. # write_asset_list: Adds an extra file to the assets, which contains a list of # all other asset files. + # keystore_path: Path to keystore to use for signing. + # keystore_name: Key alias to use. + # keystore_password: Keystore password. template("package_apk") { action(target_name) { forward_variables_from(invoker, @@ -2050,11 +2053,18 @@ if (enable_java_templates) { script = "//build/android/gyp/apkbuilder.py" depfile = "$target_gen_dir/$target_name.d" + _apksigner = "$android_sdk_build_tools/apksigner" + _zipalign = "$android_sdk_build_tools/zipalign" data_deps = [ "//tools/android/md5sum", ] # Used when deploying APKs - inputs = invoker.native_libs + [ invoker.packaged_resources_path ] + inputs = invoker.native_libs + [ + invoker.keystore_path, + invoker.packaged_resources_path, + _apksigner, + _zipalign, + ] if (defined(invoker.dex_path)) { inputs += [ invoker.dex_path ] } @@ -2062,6 +2072,9 @@ if (enable_java_templates) { outputs = [ invoker.output_apk_path, ] + data = [ + invoker.output_apk_path, + ] _rebased_compiled_resources_path = rebase_path(invoker.packaged_resources_path, root_build_dir) @@ -2072,6 +2085,16 @@ if (enable_java_templates) { rebase_path(depfile, root_build_dir), "--resource-apk=$_rebased_compiled_resources_path", "--output-apk=$_rebased_packaged_apk_path", + "--apksigner-path", + rebase_path(_apksigner, root_build_dir), + "--zipalign-path", + rebase_path(_zipalign, root_build_dir), + "--key-path", + rebase_path(invoker.keystore_path, root_build_dir), + "--key-name", + invoker.keystore_name, + "--key-passwd", + invoker.keystore_password, ] if (defined(invoker.assets_build_config)) { inputs += [ invoker.assets_build_config ] @@ -2151,59 +2174,6 @@ if (enable_java_templates) { } } - # Signs & zipaligns an apk. - # - # Variables - # input_apk_path: Path of the .apk to be finalized. - # output_apk_path: Output path for the generated .apk. - # keystore_path: Path to keystore to use for signing. - # keystore_name: Key alias to use. - # keystore_password: Keystore password. - template("finalize_apk") { - action(target_name) { - forward_variables_from(invoker, - [ - "deps", - "data_deps", - "public_deps", - "testonly", - ]) - - script = "//build/android/gyp/finalize_apk.py" - _apksigner = "$android_sdk_build_tools/apksigner" - _zipalign = "$android_sdk_build_tools/zipalign" - inputs = [ - _apksigner, - _zipalign, - invoker.input_apk_path, - invoker.keystore_path, - ] - outputs = [ - invoker.output_apk_path, - ] - data = [ - invoker.output_apk_path, - ] - - args = [ - "--unsigned-apk-path", - rebase_path(invoker.input_apk_path, root_build_dir), - "--final-apk-path", - rebase_path(invoker.output_apk_path, root_build_dir), - "--apksigner-path", - rebase_path(_apksigner, root_build_dir), - "--zipalign-path", - rebase_path(_zipalign, root_build_dir), - "--key-path", - rebase_path(invoker.keystore_path, root_build_dir), - "--key-name", - invoker.keystore_name, - "--key-passwd", - invoker.keystore_password, - ] - } - } - # Packages resources, assets, dex, and native libraries into an apk. Signs and # zipaligns the apk. template("create_apk") { @@ -2244,8 +2214,6 @@ if (enable_java_templates) { } _incremental_compiled_resources_path = "${_base_path}_incremental.ap_" - _packaged_apk_path = "${_base_path}_unsigned.apk" - _incremental_packaged_apk_path = "${_base_path}_incremental_unsigned.apk" _shared_resources = defined(invoker.shared_resources) && invoker.shared_resources assert(_shared_resources || true) # Mark as used. @@ -2299,8 +2267,7 @@ if (enable_java_templates) { } } - _package_target = "${target_name}__package_apk" - package_apk(_package_target) { + package_apk(target_name) { forward_variables_from(invoker, [ "apk_name", @@ -2319,16 +2286,18 @@ if (enable_java_templates) { } deps = _deps native_libs = _native_libs + _native_libs_even_when_incremental + keystore_path = _keystore_path + keystore_name = _keystore_name + keystore_password = _keystore_password if (defined(_dex_path)) { dex_path = _dex_path } - output_apk_path = _packaged_apk_path + output_apk_path = _final_apk_path } - _incremental_package_target = "${target_name}_incremental__package_apk" - package_apk(_incremental_package_target) { + package_apk("${target_name}_incremental") { forward_variables_from(invoker, [ "assets_build_config", @@ -2350,6 +2319,9 @@ if (enable_java_templates) { } native_libs = _native_libs_even_when_incremental + keystore_path = _keystore_path + keystore_name = _keystore_name + keystore_password = _keystore_password # http://crbug.com/384638 _has_native_libs = @@ -2358,33 +2330,8 @@ if (enable_java_templates) { native_lib_placeholders = [ "libfix.crbug.384638.so" ] } - output_apk_path = _incremental_packaged_apk_path - packaged_resources_path = _incremental_compiled_resources_path - } - - finalize_apk(target_name) { - input_apk_path = _packaged_apk_path - output_apk_path = _final_apk_path - keystore_path = _keystore_path - keystore_name = _keystore_name - keystore_password = _keystore_password - - public_deps = [ - # Generator of the _packaged_apk_path this target takes as input. - ":$_package_target", - ] - } - - finalize_apk("${target_name}_incremental") { - input_apk_path = _incremental_packaged_apk_path output_apk_path = _incremental_final_apk_path - keystore_path = _keystore_path - keystore_name = _keystore_name - keystore_password = _keystore_password - - public_deps = [ - ":$_incremental_package_target", - ] + packaged_resources_path = _incremental_compiled_resources_path } }