Skip to content

Commit

Permalink
[android] build the Android App in GH jobs (#3340)
Browse files Browse the repository at this point in the history
* [android] build the Android App in GH jobs

* separate build steps & use only JDK 8

* using 'TARGET_CPU' for ot-commissioner script

* add a build script for Android App

* fix typo in GH job name

* fix style issues

* add gradle build

* remove JAVA_HOME check

* Android SDK Agreements

* update README
  • Loading branch information
wgtdkp authored Oct 28, 2020
1 parent 3bd1a07 commit b9feb3c
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 46 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/android.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ jobs:

env:
BUILD_TYPE: android_${{ matrix.type }}
TARGET_CPU: ${{ matrix.type }}

runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build-android:0.4.12
image: connectedhomeip/chip-build-android:0.4.13
volumes:
- "/tmp/log_output:/tmp/test_logs"

Expand All @@ -43,10 +44,11 @@ jobs:
submodules: true
- name: Bootstrap
run: scripts/build/gn_bootstrap.sh
- name: Setup Build
- name: Build libs
run: |
GN_ARGS="is_clang=true target_os=\"android\" target_cpu=\"${{ matrix.type }}\" android_ndk_root=\"/opt/android/android-ndk-r21b\" android_sdk_root=\"/opt/android/sdk\""
scripts/build/gn_gen.sh --args="$GN_ARGS"
- name: Run Build
./scripts/examples/android_app.sh
- name: Build App
run: |
scripts/build/gn_build.sh
yes | "$ANDROID_HOME"/tools/bin/sdkmanager --licenses
cd src/android/CHIPTool
./gradlew build
47 changes: 47 additions & 0 deletions scripts/examples/android_app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/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 -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

# Build shared CHIP libs
source scripts/activate.sh
gn gen out/"android_$TARGET_CPU" --args="target_os=\"android\" target_cpu=\"$TARGET_CPU\" android_ndk_root=\"$ANDROID_NDK_HOME\" android_sdk_root=\"$ANDROID_HOME\""
ninja -C out/"android_$TARGET_CPU" src/setup_payload/java src/controller/java

rsync -a out/"android_$TARGET_CPU"/lib/*.jar src/android/CHIPTool/app/libs
rsync -a out/"android_$TARGET_CPU"/lib/jni/* src/android/CHIPTool/app/src/main/jniLibs

# Build ot-commissioner libs
git submodule update --init --recursive third_party/ot-commissioner/repo
TARGET_CPU=arm64 ./third_party/ot-commissioner/build-android-libs.sh
36 changes: 6 additions & 30 deletions src/android/CHIPTool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ Pre-conditions: Have Android SDK & NDK downloaded to your machine. Set the
\$ANDROID_NDK_HOME environment variable to point to the NDK package is
downloaded.

Make sure that JAVA_HOME is set to the correct path.
ABIs and corresponding values for `TARGET_CPU`

ABIs and corresponding values for `target_cpu`

| ABI | target_cpu |
| ABI | TARGET_CPU |
| ----------- | ---------- |
| armeabi-v7a | arm |
| arm64-v8a | arm64 |
Expand All @@ -29,33 +27,11 @@ ABIs and corresponding values for `target_cpu`

2. In commandline / Terminal, 'cd' into the top CHIP directory and run

```shell
source scripts/activate.sh
gn gen out/android_arm64 --args="target_os=\"android\" target_cpu=\"arm64\" android_ndk_root=\"${ANDROID_NDK_HOME}\" android_sdk_root=\"${ANDROID_HOME}\""
ninja -C out/android_arm64 src/setup_payload/java src/controller/java
```

See table above for other values of `target_cpu`.

3. You should see the generated SetupPayloadParser.jar under
`out/android_arm64/lib` and libSetupPayloadParser.so under
`out/android_arm64/lib/jni/arm64-v8a` in the output directory.

4. Copy the .jar and .so files into the Android project:

```shell
rsync -a out/android_arm64/lib/*.jar src/android/CHIPTool/app/libs
rsync -a out/android_arm64/lib/jni/* src/android/CHIPTool/app/src/main/jniLibs
```

5. Build OT Commissioner

```shell
sudo apt-get install -y swig # "brew install swig" for macOS.
git submodule update --init --recursive third_party/ot-commissioner/repo
ABI=arm64-v8a API=21 ./third_party/ot-commissioner/build-android-libs.sh
## JAR and .so libraries will be copy to target directories.
TARGET_CPU=arm64 ./scripts/examples/android_app.sh
```

6. 'Gradle sync' the Android project and run.
See table above for other values of `TARGET_CPU`.

3. 'Gradle sync' the Android project and run.
41 changes: 31 additions & 10 deletions third_party/ot-commissioner/build-android-libs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,35 @@ readonly CUR_DIR="$(dirname "$(realpath -s "$0")")"

set -e

case "$TARGET_CPU" in
arm)
TARGET_ABI="armeabi-v7a"
;;
arm64)
TARGET_ABI="arm64-v8a"
;;
x86)
TARGET_ABI="x86"
;;
x64)
TARGET_ABI="x86-64"
;;
*)
echo "invalid TARGET_CPU value: $TARGET_CPU"
exit 1
;;
esac

readonly BUILD_DIR=".build_$TARGET_CPU"

cd "$CUR_DIR"

mkdir -p build && cd build
mkdir -p "$BUILD_DIR" && cd "$BUILD_DIR"
cmake -GNinja \
-DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_HOME"/build/cmake/android.toolchain.cmake \
-DANDROID_ABI="$ABI" \
-DANDROID_ABI="$TARGET_ABI" \
-DANDROID_ARM_NEON=ON \
-DANDROID_NATIVE_API_LEVEL="$API" \
-DANDROID_NATIVE_API_LEVEL=24 \
-DBUILD_SHARED_LIBS=OFF \
-DCMAKE_CXX_STANDARD=11 \
-DCMAKE_CXX_STANDARD_REQUIRED=ON \
Expand All @@ -26,17 +47,17 @@ cmake -GNinja \
ninja
cd ../

rm -rf libs && mkdir -p libs
rm -rf "$BUILD_DIR"/libs && mkdir -p "$BUILD_DIR"/libs

## Create JAR library
javac -source 8 -target 8 build/src/java/io/openthread/commissioner/*.java
javac -source 8 -target 8 "$BUILD_DIR"/src/java/io/openthread/commissioner/*.java

cd build/src/java
find ./io/openthread/commissioner -name "*.class" | xargs jar cvf ../../../libs/libotcommissioner.jar
cd "$BUILD_DIR"/src/java
find ./io/openthread/commissioner -name "*.class" | xargs jar cvf ../../libs/libotcommissioner.jar
cd ../../../

## Copy shared native libraries
cp ./build/src/java/libcommissioner-java.so libs
rsync -a "$BUILD_DIR"/src/java/libcommissioner-java.so "$BUILD_DIR"/libs

cp libs/libotcommissioner.jar ../../src/android/CHIPTool/app/libs
cp libs/*.so ../../src/android/CHIPTool/app/src/main/jniLibs/"$ABI"
rsync -a "$BUILD_DIR"/libs/libotcommissioner.jar ../../src/android/CHIPTool/app/libs
rsync -a "$BUILD_DIR"/libs/*.so ../../src/android/CHIPTool/app/src/main/jniLibs/"$TARGET_ABI"

0 comments on commit b9feb3c

Please sign in to comment.