Update the build script for Android AAR package (#7229)

* Update the build script for Android AAR package

* Address CR comments
This commit is contained in:
Guoyu Wang 2021-04-05 16:37:22 -07:00 committed by GitHub
parent 9f14af9809
commit c5973fbbac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,11 +7,15 @@ import os
import pathlib
import json
import subprocess
import sys
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
REPO_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, "..", "..", "..", ".."))
BUILD_PY = os.path.normpath(os.path.join(REPO_DIR, "tools", "ci_build", "build.py"))
JAVA_ROOT = os.path.normpath(os.path.join(REPO_DIR, "java"))
BUILD_PY = os.path.join(REPO_DIR, "tools", "ci_build", "build.py")
JAVA_ROOT = os.path.join(REPO_DIR, "java")
sys.path.insert(0, os.path.join(REPO_DIR, "tools", "python"))
from util import is_windows # noqa: E402
# We by default will build all 4 ABIs
DEFAULT_BUILD_ABIS = ["armeabi-v7a", "arm64-v8a", "x86", "x86_64"]
@ -31,8 +35,6 @@ def _parse_build_settings(args):
_build_settings_data = json.load(f)
build_settings = {}
build_settings['android_sdk_path'] = args.android_sdk_path
build_settings['android_ndk_path'] = args.android_ndk_path
if 'build_flavor' in _build_settings_data:
build_settings['build_flavor'] = _build_settings_data['build_flavor']
@ -73,12 +75,12 @@ def _parse_build_settings(args):
def _build_aar(args):
build_settings = _parse_build_settings(args)
build_dir = args.build_dir
build_dir = os.path.abspath(args.build_dir)
# Setup temp environment for building
my_env = os.environ.copy()
my_env['ANDROID_HOME'] = build_settings['android_sdk_path']
my_env['ANDROID_NDK_HOME'] = build_settings['android_ndk_path']
_env = os.environ.copy()
_env['ANDROID_HOME'] = os.path.abspath(args.android_sdk_path)
_env['ANDROID_NDK_HOME'] = os.path.abspath(args.android_ndk_path)
# Temp dirs to hold building results
_intermediates_dir = os.path.join(build_dir, 'intermediates')
@ -100,7 +102,7 @@ def _build_aar(args):
if args.include_ops_by_config is not None:
_build_command += ['--include_ops_by_config=' + args.include_ops_by_config]
subprocess.run(_build_command, env=my_env, shell=False, check=True, cwd=REPO_DIR)
subprocess.run(_build_command, env=_env, shell=False, check=True, cwd=REPO_DIR)
# create symbolic links for libonnxruntime.so and libonnxruntime4j_jni.so
# to jnilibs/[abi] for later compiling the aar package
@ -108,8 +110,10 @@ def _build_aar(args):
os.makedirs(_jnilibs_abi_dir, exist_ok=True)
for lib_name in ['libonnxruntime.so', 'libonnxruntime4j_jni.so']:
_target_lib_name = os.path.join(_jnilibs_abi_dir, lib_name)
# if the symbolic already exists, delete it first
if os.path.exists(_target_lib_name):
# If the symbolic already exists, delete it first
# For some reason, os.path.exists will return false for a symbolic link in Linux,
# add double check with os.path.islink
if os.path.exists(_target_lib_name) or os.path.islink(_target_lib_name):
os.remove(_target_lib_name)
os.symlink(os.path.join(_build_dir, _build_flavor, lib_name), _target_lib_name)
@ -130,10 +134,13 @@ def _build_aar(args):
'-DtargetSdkVer=' + str(build_settings['android_target_sdk_version'])
]
# If not using shell on Window, will not be able to find gradle in path
_shell = True if is_windows() else False
# clean, build, and publish to a local directory
subprocess.run(_gradle_command + ['clean'], env=my_env, shell=False, check=True, cwd=JAVA_ROOT)
subprocess.run(_gradle_command + ['build'], env=my_env, shell=False, check=True, cwd=JAVA_ROOT)
subprocess.run(_gradle_command + ['publish'], env=my_env, shell=False, check=True, cwd=JAVA_ROOT)
subprocess.run(_gradle_command + ['clean'], env=_env, shell=_shell, check=True, cwd=JAVA_ROOT)
subprocess.run(_gradle_command + ['build'], env=_env, shell=_shell, check=True, cwd=JAVA_ROOT)
subprocess.run(_gradle_command + ['publish'], env=_env, shell=_shell, check=True, cwd=JAVA_ROOT)
def parse_args():
@ -141,7 +148,8 @@ def parse_args():
os.path.basename(__file__),
description='''Create Android Archive (AAR) package for one or more Android ABI(s)
and building properties specified in the given build config file, see
tools/ci_build/github/android/default_mobile_aar_build_settings.json for details
tools/ci_build/github/android/default_mobile_aar_build_settings.json for details.
The output of the final AAR package can be found under [build_dir]/aar_out
'''
)
@ -151,7 +159,7 @@ def parse_args():
parser.add_argument("--android_ndk_path", type=str, default=os.environ.get("ANDROID_NDK_HOME", ""),
help="Path to the Android NDK")
parser.add_argument('--build_dir', type=pathlib.Path, default=os.path.join(REPO_DIR, 'build_android_aar'),
parser.add_argument('--build_dir', type=str, default=os.path.join(REPO_DIR, 'build/android_aar'),
help='Provide the root directory for build output')
parser.add_argument(