Skip to content

Commit

Permalink
Unify Android build logic (#9762)
Browse files Browse the repository at this point in the history
* Unify Android build-from-script to android.py, and build-from-IDE to android_app_ide.sh.

* Restyled by prettier-markdown

* Add Android x86 to all platforms build and update test

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
austinh0 and restyled-commits authored Sep 21, 2021
1 parent 993d0b9 commit 1c540b9
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 67 deletions.
2 changes: 2 additions & 0 deletions .github/.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ ini
init
inlined
instantiation
installDebug
integrations
IntelliSense
InteractionModelVersion
Expand Down Expand Up @@ -527,6 +528,7 @@ ManualPairingCode
ManualTest
ManufacturingDate
masterkey
matterBuildSrcDir
matterc
matterd
MatterLock
Expand Down
69 changes: 60 additions & 9 deletions docs/guides/android_chiptool_building.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ CHIPTool offers the following features:
- [Source files](#source)
- [Requirements for building](#requirements)
- [ABIs and TARGET_CPU](#abi)
- [Building Android CHIPTool](#building)
- [Preparing for build](#preparing)
- [Building Android CHIPTool from scripts](#building-scripts)
- [Building Android CHIPTool from Android Studio](#building-studio)

<hr>

Expand Down Expand Up @@ -54,11 +56,11 @@ architecture:

<hr>

<a name="building"></a>
<a name="preparing"></a>

## Building Android CHIPTool
## Preparing for build

Complete the following steps to build CHIPTool:
Complete the following steps to prepare the CHIP build:

1. Check out the CHIP repository.

Expand All @@ -68,18 +70,57 @@ Complete the following steps to build CHIPTool:
source scripts/bootstrap.sh
```

3. In the command line, run the following command from the top CHIP directory:
3. Choose how you want to build the Android CHIPTool. There are two ways: from
script, or from source within Android Studio.

<a name="building-scripts"></a>

## Building Android CHIPTool from scripts

This is the simplest option. In the command line, run the following command from
the top CHIP directory:

```shell
./scripts/build/build_examples.py --platform android --board arm64 build
```

See the table above for other values of `TARGET_CPU`.

The debug Android package `app-debug.apk` will be generated at
`out/android-$TARGET_CPU-chip_tool/outputs/apk/debug/`, and can be installed
with

```shell
adb install out/android-$TARGET_CPU-chip_tool/outputs/apk/debug/app-debug.apk
```

You can use Android Studio to edit the Android CHIPTool app itself, but you will
not be able to edit CHIP Android code from `src/controller/java`, or other CHIP
C++ code within Android Studio.

<a name="building-studio"></a>

## Building Android CHIPTool from Android Studio

This option allows Android Studio to build the core CHIP code from source, which
allows us to directly edit core CHIP code in-IDE.

1. In the command line, run the following command from the top CHIP directory:

```shell
TARGET_CPU=arm64 ./scripts/examples/android_app.sh
TARGET_CPU=arm64 ./scripts/examples/android_app_ide.sh
```

See the table above for other values of `TARGET_CPU`.

4. Open the project in Android Studio and run **Sync Project with Gradle
2. Modify the `matterBuildSrcDir` variable in
[src/android/CHIPTool/build.gradle](https://github.com/project-chip/connectedhomeip/blob/master/src/android/CHIPTool/build.gradle)
to point to the appropriate output directory (e.g. `out/android_arm64`).

3. Open the project in Android Studio and run **Sync Project with Gradle
Files**.

5. Use one of the following options to build an Android package:
4. Use one of the following options to build an Android package:

- Click **Make Project** in Android Studio.
- Run the following command in the command line:
Expand All @@ -90,4 +131,14 @@ Complete the following steps to build CHIPTool:
```

The debug Android package `app-debug.apk` will be generated at
`src/android/CHIPTool/app/build/outputs/apk/debug/`.
`src/android/CHIPTool/app/build/outputs/apk/debug/`, and can be installed with

```shell
adb install src/android/CHIPTool/app/build/outputs/apk/debug/app-debug.apk
```

or

```shell
(cd src/android/CHIPTool && ./gradlew installDebug)
```
1 change: 1 addition & 0 deletions scripts/build/build/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def Create(self, runner, __board_key: Board, __app_key: Application,
_MATCHERS[Platform.ANDROID].AcceptBoard(Board.ARM, board=AndroidBoard.ARM)
_MATCHERS[Platform.ANDROID].AcceptBoard(Board.ARM64, board=AndroidBoard.ARM64)
_MATCHERS[Platform.ANDROID].AcceptBoard(Board.X64, board=AndroidBoard.X64)
_MATCHERS[Platform.ANDROID].AcceptBoard(Board.X86, board=AndroidBoard.X86)
_MATCHERS[Platform.ANDROID].AcceptApplication(Application.CHIP_TOOL)

_MATCHERS[Platform.INFINEON].AcceptApplication(
Expand Down
1 change: 1 addition & 0 deletions scripts/build/build/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Board(IntEnum):
ARM = auto()
ARM64 = auto()
X64 = auto()
X86 = auto()

# Infineon board
P6BOARD = auto()
Expand Down
10 changes: 6 additions & 4 deletions scripts/build/builders/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AndroidBoard(Enum):
ARM = auto()
ARM64 = auto()
X64 = auto()
X86 = auto()

def TargetCpuName(self):
if self == AndroidBoard.ARM:
Expand All @@ -32,6 +33,8 @@ def TargetCpuName(self):
return 'arm64'
elif self == AndroidBoard.X64:
return 'x64'
elif self == AndroidBoard.X86:
return 'x86'
else:
raise Exception('Unknown board type: %r' % self)

Expand All @@ -42,6 +45,8 @@ def AbiName(self):
return 'arm64-v8a'
elif self == AndroidBoard.X64:
return 'x86_64'
elif self == AndroidBoard.X86:
return 'x86'
else:
raise Exception('Unknown board type: %r' % self)

Expand Down Expand Up @@ -149,7 +154,7 @@ def _build(self):
'%s/src/android/CHIPTool/gradlew' % self.root, '-p',
'%s/src/android/CHIPTool' % self.root,
'-PchipSdkJarDir=%s' % os.path.join(self.output_dir, 'lib'),
'-PbuildDir=%s' % self.output_dir, 'build'
'-PbuildDir=%s' % self.output_dir, 'assembleDebug'
],
title='Building APP ' + self.identifier)

Expand All @@ -162,9 +167,6 @@ def build_outputs(self):
'ChipTool-debug.apk':
os.path.join(self.output_dir, 'outputs', 'apk', 'debug',
'app-debug.apk'),
'ChipTool-release-unsigned.apk':
os.path.join(self.output_dir, 'outputs', 'apk', 'release',
'app-release-unsigned.apk'),

'jni/%s/libSetupPayloadParser.so' % self.board.AbiName():
os.path.join(self.output_dir, 'lib', 'jni',
Expand Down
27 changes: 24 additions & 3 deletions scripts/build/expected_all_platform_commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ gn gen --check --fail-on-unused-args {out}/android-x64-chip_tool '--args=target_
# Accepting NDK licenses
bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null'

# 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
bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null'

# Generating infineon-p6board-lock
gn gen --check --fail-on-unused-args --root={root}/examples/lock-app/p6 '--args=p6_board="CY8CKIT-062S2-43012"' {out}/infineon-p6board-lock

Expand Down Expand Up @@ -220,7 +226,7 @@ cp {out}/android-arm-chip_tool/lib/jni/armeabi-v7a/libCHIPController.so {root}/s
cp {out}/android-arm-chip_tool/lib/jni/armeabi-v7a/libc++_shared.so {root}/src/android/CHIPTool/app/libs/jniLibs/armeabi-v7a/libc++_shared.so

# Building APP android-arm-chip_tool
{root}/src/android/CHIPTool/gradlew -p {root}/src/android/CHIPTool -PchipSdkJarDir={out}/android-arm-chip_tool/lib -PbuildDir={out}/android-arm-chip_tool build
{root}/src/android/CHIPTool/gradlew -p {root}/src/android/CHIPTool -PchipSdkJarDir={out}/android-arm-chip_tool/lib -PbuildDir={out}/android-arm-chip_tool assembleDebug

# Building JNI android-arm64-chip_tool
ninja -C {out}/android-arm64-chip_tool
Expand All @@ -235,7 +241,7 @@ cp {out}/android-arm64-chip_tool/lib/jni/arm64-v8a/libCHIPController.so {root}/s
cp {out}/android-arm64-chip_tool/lib/jni/arm64-v8a/libc++_shared.so {root}/src/android/CHIPTool/app/libs/jniLibs/arm64-v8a/libc++_shared.so

# Building APP android-arm64-chip_tool
{root}/src/android/CHIPTool/gradlew -p {root}/src/android/CHIPTool -PchipSdkJarDir={out}/android-arm64-chip_tool/lib -PbuildDir={out}/android-arm64-chip_tool build
{root}/src/android/CHIPTool/gradlew -p {root}/src/android/CHIPTool -PchipSdkJarDir={out}/android-arm64-chip_tool/lib -PbuildDir={out}/android-arm64-chip_tool assembleDebug

# Building JNI android-x64-chip_tool
ninja -C {out}/android-x64-chip_tool
Expand All @@ -250,7 +256,22 @@ cp {out}/android-x64-chip_tool/lib/jni/x86_64/libCHIPController.so {root}/src/an
cp {out}/android-x64-chip_tool/lib/jni/x86_64/libc++_shared.so {root}/src/android/CHIPTool/app/libs/jniLibs/x86_64/libc++_shared.so

# Building APP android-x64-chip_tool
{root}/src/android/CHIPTool/gradlew -p {root}/src/android/CHIPTool -PchipSdkJarDir={out}/android-x64-chip_tool/lib -PbuildDir={out}/android-x64-chip_tool build
{root}/src/android/CHIPTool/gradlew -p {root}/src/android/CHIPTool -PchipSdkJarDir={out}/android-x64-chip_tool/lib -PbuildDir={out}/android-x64-chip_tool assembleDebug

# Building JNI android-x86-chip_tool
ninja -C {out}/android-x86-chip_tool

# Prepare Native libs android-x86-chip_tool
mkdir -p {root}/src/android/CHIPTool/app/libs/jniLibs/x86

cp {out}/android-x86-chip_tool/lib/jni/x86/libSetupPayloadParser.so {root}/src/android/CHIPTool/app/libs/jniLibs/x86/libSetupPayloadParser.so

cp {out}/android-x86-chip_tool/lib/jni/x86/libCHIPController.so {root}/src/android/CHIPTool/app/libs/jniLibs/x86/libCHIPController.so

cp {out}/android-x86-chip_tool/lib/jni/x86/libc++_shared.so {root}/src/android/CHIPTool/app/libs/jniLibs/x86/libc++_shared.so

# Building APP android-x86-chip_tool
{root}/src/android/CHIPTool/gradlew -p {root}/src/android/CHIPTool -PchipSdkJarDir={out}/android-x86-chip_tool/lib -PbuildDir={out}/android-x86-chip_tool assembleDebug

# Building infineon-p6board-lock
ninja -C {out}/infineon-p6board-lock
Expand Down
51 changes: 0 additions & 51 deletions scripts/examples/android_app.sh

This file was deleted.

41 changes: 41 additions & 0 deletions scripts/examples/android_app_ide.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

#
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -e
set -x
env

if [ -z "$ANDROID_HOME" ]; then
echo "ANDROID_HOME not set!"
exit 1
fi

if [ -z "$ANDROID_NDK_HOME" ]; then
echo "ANDROID_NDK_HOME not set!"
exit 1
fi

if [ -z "$TARGET_CPU" ]; then
echo "TARGET_CPU not set! Candidates: arm, arm64, x86 and x64."
exit 1
fi

source scripts/activate.sh
# Build CMake for Android Studio
echo "build ide"
gn gen --check --fail-on-unused-args out/"android_$TARGET_CPU" --args="target_os=\"android\" target_cpu=\"$TARGET_CPU\" android_ndk_root=\"$ANDROID_NDK_HOME\" android_sdk_root=\"$ANDROID_HOME\" chip_use_clusters_for_ip_commissioning=\"true\"" --ide=json --json-ide-script=//scripts/examples/gn_to_cmakelists.py

0 comments on commit 1c540b9

Please sign in to comment.