From 9032387f48c95449466e07a0d48a60aee39e858d Mon Sep 17 00:00:00 2001 From: "Hui.Li-TCL" Date: Fri, 12 Nov 2021 00:28:55 +0800 Subject: [PATCH] Android build on Apple silicon (#11621) * support sdkmanager at cmdline-tools for new systems, old one is not work on apple silicon and also be deprecated https://developer.android.com/studio/releases/sdk-tools * added ndk_host_cpu for apple silicon * added document to fix jdk error * fix restyled-io * fix spellcheck * fix build cmd test issue * remove jdk in wordlist because wordlist may be case insensitive * fix spell errors --- .github/.wordlist.txt | 4 +++ build/toolchain/android/android_toolchain.gni | 3 ++ docs/guides/android_building.md | 15 ++++++++ scripts/build/builders/android.py | 34 ++++++++++++++----- .../build/testdata/build_all_except_host.txt | 20 +++++------ 5 files changed, 57 insertions(+), 19 deletions(-) diff --git a/.github/.wordlist.txt b/.github/.wordlist.txt index 6cac33b9ab1f99..16c303e00307aa 100644 --- a/.github/.wordlist.txt +++ b/.github/.wordlist.txt @@ -488,6 +488,8 @@ itemName iterable jinja jlink +jre +JDK JLink JLinkExe JLinkRTTClient @@ -669,6 +671,7 @@ Onboarding onboardingcodes oneshot onnetwork +openjdk OnOff OnOffClusterTest OnPlatformEvent @@ -970,6 +973,7 @@ unblur UNBLUR uncommissioned unfocus +userguide Unicast UniFlash unpair diff --git a/build/toolchain/android/android_toolchain.gni b/build/toolchain/android/android_toolchain.gni index d54d2184913059..b2c3290a39a50b 100644 --- a/build/toolchain/android/android_toolchain.gni +++ b/build/toolchain/android/android_toolchain.gni @@ -55,6 +55,9 @@ template("android_clang_toolchain") { _ndk_host_cpu = "" if (host_cpu == "x64") { _ndk_host_cpu = "-x86_64" + } else if (host_cpu == "arm64") { + # until NDK 24.0.7856742-beta1, the host cpu on apple silicon is x86_64 + _ndk_host_cpu = "-x86_64" } _ndk_host = _ndk_host_os + _ndk_host_cpu diff --git a/docs/guides/android_building.md b/docs/guides/android_building.md index ed9baeefb15365..6ca2d91dd54a1a 100644 --- a/docs/guides/android_building.md +++ b/docs/guides/android_building.md @@ -18,6 +18,7 @@ There are following Apps on Android - [Source files](#source) - [Requirements for building](#requirements) - [ABIs and TARGET_CPU](#abi) + - [Gradle & JDK Version](#jdk) - [Preparing for build](#preparing) - [Building Android CHIPTool from scripts](#building-scripts) - [Building Android CHIPTool from Android Studio](#building-studio) @@ -57,6 +58,20 @@ architecture: | x86 | x86 | | x86_64 | x64 | + + +### Gradle & JDK Version + +We are using Gradle 7.1.1 for all android project which does not support Java 17 +(https://docs.gradle.org/current/userguide/compatibility.html) while the default +JDK version on MacOS for Apple Silicon is 'openjdk 17.0.1' or above. + +Using JDK bundled with Android Studio will help with that. + +```shell +export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home/ +``` +
diff --git a/scripts/build/builders/android.py b/scripts/build/builders/android.py index 74d47e44850045..71ee5907267ca7 100644 --- a/scripts/build/builders/android.py +++ b/scripts/build/builders/android.py @@ -100,9 +100,13 @@ def validate_build_environment(self): # SDK manager must be runnable to 'accept licenses' sdk_manager = os.path.join(os.environ['ANDROID_HOME'], 'tools', 'bin', 'sdkmanager') - if not (os.path.isfile(sdk_manager) and os.access(sdk_manager, os.X_OK)): - raise Exception("'%s' is not executable by the current user" % - sdk_manager) + + # New SDK manager at cmdline-tools/latest/bin/ + new_sdk_manager = os.path.join(os.environ['ANDROID_HOME'], 'cmdline-tools', 'latest', + 'bin', 'sdkmanager') + if not (os.path.isfile(sdk_manager) and os.access(sdk_manager, os.X_OK)) and not (os.path.isfile(new_sdk_manager) and os.access(new_sdk_manager, os.X_OK)): + raise Exception("'%s' and '%s' is not executable by the current user" % + (sdk_manager, new_sdk_manager)) # In order to accept a license, the licenses folder is updated with the hash of the # accepted license @@ -162,12 +166,24 @@ def generate(self): self._Execute(gn_gen, title='Generating ' + self.identifier) - self._Execute([ - 'bash', '-c', - 'yes | %s/tools/bin/sdkmanager --licenses >/dev/null' % - os.environ['ANDROID_HOME'] - ], - title='Accepting NDK licenses') + new_sdk_manager = os.path.join(os.environ['ANDROID_HOME'], 'cmdline-tools', 'latest', + 'bin', 'sdkmanager') + if (os.path.isfile(new_sdk_manager) and os.access(new_sdk_manager, os.X_OK)): + self._Execute([ + 'bash', '-c', + 'yes | %s --licenses >/dev/null' % + new_sdk_manager + ], + title='Accepting NDK licenses @ cmdline-tools') + else: + sdk_manager = os.path.join(os.environ['ANDROID_HOME'], 'tools', 'bin', + 'sdkmanager') + self._Execute([ + 'bash', '-c', + 'yes | %s --licenses >/dev/null' % + sdk_manager + ], + title='Accepting NDK licenses @ tools') def _build(self): if self.board.IsIde(): diff --git a/scripts/build/testdata/build_all_except_host.txt b/scripts/build/testdata/build_all_except_host.txt index 3b83c1ad24c1ff..3678a22f4717db 100644 --- a/scripts/build/testdata/build_all_except_host.txt +++ b/scripts/build/testdata/build_all_except_host.txt @@ -13,7 +13,7 @@ python3 third_party/android_deps/set_up_android_deps.py # Generating android-androidstudio-arm-chip-tool gn gen --check --fail-on-unused-args {out}/android-androidstudio-arm-chip-tool '--args=target_os="android" target_cpu="arm" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning=true ' --ide=json --json-ide-script=//scripts/examples/gn_to_cmakelists.py -# Accepting NDK licenses +# Accepting NDK licenses @ tools bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null' # Generating JARs for Java build rules test @@ -25,7 +25,7 @@ python3 third_party/android_deps/set_up_android_deps.py # Generating android-androidstudio-arm64-chip-tool gn gen --check --fail-on-unused-args {out}/android-androidstudio-arm64-chip-tool '--args=target_os="android" target_cpu="arm64" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning=true ' --ide=json --json-ide-script=//scripts/examples/gn_to_cmakelists.py -# Accepting NDK licenses +# Accepting NDK licenses @ tools bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null' # Generating JARs for Java build rules test @@ -37,7 +37,7 @@ python3 third_party/android_deps/set_up_android_deps.py # Generating android-androidstudio-x64-chip-tool gn gen --check --fail-on-unused-args {out}/android-androidstudio-x64-chip-tool '--args=target_os="android" target_cpu="x64" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning=true ' --ide=json --json-ide-script=//scripts/examples/gn_to_cmakelists.py -# Accepting NDK licenses +# Accepting NDK licenses @ tools bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null' # Generating JARs for Java build rules test @@ -49,7 +49,7 @@ python3 third_party/android_deps/set_up_android_deps.py # Generating android-androidstudio-x86-chip-tool gn gen --check --fail-on-unused-args {out}/android-androidstudio-x86-chip-tool '--args=target_os="android" target_cpu="x86" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning=true ' --ide=json --json-ide-script=//scripts/examples/gn_to_cmakelists.py -# Accepting NDK licenses +# Accepting NDK licenses @ tools bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null' # Generating JARs for Java build rules test @@ -61,7 +61,7 @@ python3 third_party/android_deps/set_up_android_deps.py # Generating android-arm-chip-tool gn gen --check --fail-on-unused-args {out}/android-arm-chip-tool '--args=target_os="android" target_cpu="arm" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning=true ' -# Accepting NDK licenses +# Accepting NDK licenses @ tools bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null' # Generating JARs for Java build rules test @@ -73,7 +73,7 @@ python3 third_party/android_deps/set_up_android_deps.py # Generating android-arm64-chip-test gn gen --check --fail-on-unused-args {out}/android-arm64-chip-test '--args=target_os="android" target_cpu="arm64" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning=true ' -# Accepting NDK licenses +# Accepting NDK licenses @ tools bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null' # Generating JARs for Java build rules test @@ -85,7 +85,7 @@ python3 third_party/android_deps/set_up_android_deps.py # Generating android-arm64-chip-tool gn gen --check --fail-on-unused-args {out}/android-arm64-chip-tool '--args=target_os="android" target_cpu="arm64" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning=true ' -# Accepting NDK licenses +# Accepting NDK licenses @ tools bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null' # Generating JARs for Java build rules test @@ -97,7 +97,7 @@ python3 third_party/android_deps/set_up_android_deps.py # Generating android-arm64-chip-tvserver gn gen --check --fail-on-unused-args {out}/android-arm64-chip-tvserver '--args=target_os="android" target_cpu="arm64" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning=true chip_config_network_layer_ble=false ' -# Accepting NDK licenses +# Accepting NDK licenses @ tools bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null' # Generating JARs for Java build rules test @@ -109,7 +109,7 @@ python3 third_party/android_deps/set_up_android_deps.py # Generating android-x64-chip-tool gn gen --check --fail-on-unused-args {out}/android-x64-chip-tool '--args=target_os="android" target_cpu="x64" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning=true ' -# Accepting NDK licenses +# Accepting NDK licenses @ tools bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null' # Generating JARs for Java build rules test @@ -121,7 +121,7 @@ python3 third_party/android_deps/set_up_android_deps.py # Generating android-x86-chip-tool gn gen --check --fail-on-unused-args {out}/android-x86-chip-tool '--args=target_os="android" target_cpu="x86" android_ndk_root="TEST_ANDROID_NDK_HOME" android_sdk_root="TEST_ANDROID_HOME" chip_use_clusters_for_ip_commissioning=true ' -# Accepting NDK licenses +# Accepting NDK licenses @ tools bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null' # Generating efr32-brd4161a-light