Skip to content

Commit

Permalink
Android build on Apple silicon (#11621)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xylophone21 authored and pull[bot] committed May 30, 2023
1 parent 7dc292f commit 9032387
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .github/.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ itemName
iterable
jinja
jlink
jre
JDK
JLink
JLinkExe
JLinkRTTClient
Expand Down Expand Up @@ -669,6 +671,7 @@ Onboarding
onboardingcodes
oneshot
onnetwork
openjdk
OnOff
OnOffClusterTest
OnPlatformEvent
Expand Down Expand Up @@ -970,6 +973,7 @@ unblur
UNBLUR
uncommissioned
unfocus
userguide
Unicast
UniFlash
unpair
Expand Down
3 changes: 3 additions & 0 deletions build/toolchain/android/android_toolchain.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions docs/guides/android_building.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -57,6 +58,20 @@ architecture:
| x86 | x86 |
| x86_64 | x64 |

<a name="jdk"></a>

### 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/
```

<hr>

<a name="preparing"></a>
Expand Down
34 changes: 25 additions & 9 deletions scripts/build/builders/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down
20 changes: 10 additions & 10 deletions scripts/build/testdata/build_all_except_host.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9032387

Please sign in to comment.