| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2015 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 | 6 |
| 7 """Adds the code parts to a resource APK.""" | 7 """Adds the code parts to a resource APK.""" |
| 8 | 8 |
| 9 import argparse | 9 import argparse |
| 10 import os | 10 import os |
| 11 import shutil | 11 import shutil |
| 12 import sys | 12 import sys |
| 13 import zipfile | 13 import zipfile |
| 14 | 14 |
| 15 from util import build_utils | 15 from util import build_utils |
| 16 | 16 |
| 17 | 17 |
| 18 def _ParseArgs(args): | 18 def _ParseArgs(args): |
| 19 parser = argparse.ArgumentParser() | 19 parser = argparse.ArgumentParser() |
| 20 build_utils.AddDepfileOption(parser) | 20 build_utils.AddDepfileOption(parser) |
| 21 parser.add_argument('--assets-build-config', |
| 22 help='Path to .build_config containing the asset list.') |
| 21 parser.add_argument('--resource-apk', | 23 parser.add_argument('--resource-apk', |
| 22 help='An .ap_ file built using aapt', | 24 help='An .ap_ file built using aapt', |
| 23 required=True) | 25 required=True) |
| 24 parser.add_argument('--output-apk', | 26 parser.add_argument('--output-apk', |
| 25 help='Path to the output file', | 27 help='Path to the output file', |
| 26 required=True) | 28 required=True) |
| 27 parser.add_argument('--dex-file', | 29 parser.add_argument('--dex-file', |
| 28 help='Path to the classes.dex to use') | 30 help='Path to the classes.dex to use') |
| 29 # TODO(agrieve): Switch this to be a list of files rather than a directory. | 31 # TODO(agrieve): Switch this to be a list of files rather than a directory. |
| 30 parser.add_argument('--native-libs-dir', | 32 parser.add_argument('--native-libs-dir', |
| (...skipping 13 matching lines...) Expand all Loading... |
| 44 | 46 |
| 45 def _ListSubPaths(path): | 47 def _ListSubPaths(path): |
| 46 """Returns a list of full paths to all files in the given path.""" | 48 """Returns a list of full paths to all files in the given path.""" |
| 47 return [os.path.join(path, name) for name in os.listdir(path)] | 49 return [os.path.join(path, name) for name in os.listdir(path)] |
| 48 | 50 |
| 49 | 51 |
| 50 def main(args): | 52 def main(args): |
| 51 args = build_utils.ExpandFileArgs(args) | 53 args = build_utils.ExpandFileArgs(args) |
| 52 options = _ParseArgs(args) | 54 options = _ParseArgs(args) |
| 53 | 55 |
| 56 assets_json = [] |
| 57 if options.assets_build_config: |
| 58 assets_json = build_utils.ReadJson(options.assets_build_config) |
| 59 assets_json = assets_json['merged_assets'] |
| 60 |
| 54 native_libs = [] | 61 native_libs = [] |
| 55 if options.native_libs_dir: | 62 if options.native_libs_dir: |
| 56 native_libs = _ListSubPaths(options.native_libs_dir) | 63 native_libs = _ListSubPaths(options.native_libs_dir) |
| 57 | 64 |
| 58 input_paths = [options.resource_apk] + native_libs | 65 input_paths = [options.resource_apk, __file__] + native_libs |
| 59 if options.dex_file: | 66 if options.dex_file: |
| 60 input_paths.append(options.dex_file) | 67 input_paths.append(options.dex_file) |
| 61 | 68 |
| 69 for asset_entry in assets_json: |
| 70 input_paths.extend(asset_entry['paths']) |
| 71 |
| 62 def on_stale_md5(): | 72 def on_stale_md5(): |
| 63 tmp_apk = options.output_apk + '.tmp' | 73 tmp_apk = options.output_apk + '.tmp' |
| 64 try: | 74 try: |
| 65 # Use a temp file to avoid creating an output if anything goes wrong. | 75 # Use a temp file to avoid creating an output if anything goes wrong. |
| 66 shutil.copyfile(options.resource_apk, tmp_apk) | 76 shutil.copyfile(options.resource_apk, tmp_apk) |
| 67 | 77 |
| 68 # TODO(agrieve): It would be more efficient to combine this step | 78 # TODO(agrieve): It would be more efficient to combine this step |
| 69 # with finalize_apk(), which sometimes aligns and uncompresses the | 79 # with finalize_apk(), which sometimes aligns and uncompresses the |
| 70 # native libraries. | 80 # native libraries. |
| 71 with zipfile.ZipFile(tmp_apk, 'a', zipfile.ZIP_DEFLATED) as apk: | 81 with zipfile.ZipFile(tmp_apk, 'a', zipfile.ZIP_DEFLATED) as apk: |
| 82 for asset_entry in assets_json: |
| 83 compress_type = zipfile.ZIP_STORED |
| 84 if asset_entry['enable_compression']: |
| 85 compress_type = zipfile.ZIP_DEFLATED |
| 86 for path in asset_entry['paths']: |
| 87 apk_path = 'assets/' + os.path.basename(path) |
| 88 try: |
| 89 apk.getinfo(apk_path) |
| 90 # TODO(agrieve): Add support for asset clobbering if needed. |
| 91 raise Exception('Multiple targets specified the asset path: %s' % |
| 92 apk_path) |
| 93 except KeyError: |
| 94 apk.write(path, apk_path, compress_type) |
| 72 for path in native_libs: | 95 for path in native_libs: |
| 73 basename = os.path.basename(path) | 96 basename = os.path.basename(path) |
| 74 apk.write(path, 'lib/%s/%s' % (options.android_abi, basename)) | 97 apk.write(path, 'lib/%s/%s' % (options.android_abi, basename)) |
| 75 if options.create_placeholder_lib: | 98 if options.create_placeholder_lib: |
| 76 # Make it non-empty so that its checksum is non-zero and is not | 99 # Make it non-empty so that its checksum is non-zero and is not |
| 77 # ignored by md5_check. | 100 # ignored by md5_check. |
| 78 apk.writestr('lib/%s/libplaceholder.so' % options.android_abi, ':-)') | 101 apk.writestr('lib/%s/libplaceholder.so' % options.android_abi, ':-)') |
| 79 if options.dex_file: | 102 if options.dex_file: |
| 80 apk.write(options.dex_file, 'classes.dex') | 103 apk.write(options.dex_file, 'classes.dex') |
| 81 | 104 |
| 82 shutil.move(tmp_apk, options.output_apk) | 105 shutil.move(tmp_apk, options.output_apk) |
| 83 finally: | 106 finally: |
| 84 if os.path.exists(tmp_apk): | 107 if os.path.exists(tmp_apk): |
| 85 os.unlink(tmp_apk) | 108 os.unlink(tmp_apk) |
| 86 | 109 |
| 87 build_utils.CallAndWriteDepfileIfStale( | 110 build_utils.CallAndWriteDepfileIfStale( |
| 88 on_stale_md5, | 111 on_stale_md5, |
| 89 options, | 112 options, |
| 90 input_paths=input_paths, | 113 input_paths=input_paths, |
| 91 input_strings=[options.create_placeholder_lib, options.android_abi], | 114 input_strings=[options.create_placeholder_lib, options.android_abi], |
| 92 output_paths=[options.output_apk]) | 115 output_paths=[options.output_apk]) |
| 93 | 116 |
| 94 | 117 |
| 95 if __name__ == '__main__': | 118 if __name__ == '__main__': |
| 96 main(sys.argv[1:]) | 119 main(sys.argv[1:]) |
| OLD | NEW |