Skip to content

Commit 4625b6e

Browse files
CI Android build&test (#194)
- for integrationtests: do not use threads for ANDROID to avoid emulator timeout/hanging - CI android tests: run on ubuntu 22.04, build for x86_64, add necessary gradle configuration options and presets - use latest versions of sdk tools to match pre-installed ubuntu 22.04 packages - CI android test: cache android sdk, gradle dependencies, cmake build - trigger android test workflow on comment 'test android' and new tag - testing on emulator run tests only in a background thread to avoid ANR error - move testing script from Taskfile to test_android.sh * upd : build for x86_64 * fix : reduce load test by 25mb * fix : compile only 1 file at a time * fix : define ANDROID to run tests sequentially on emulator * add script, rm templates from taskfile, add env presets * add : cache android sdk, gradle dependencies, cmake build (cpp libs, vcpkg packages) * upd : use gradle-build lib from aar for testing * upd : trigger android test on comment 'test android' and on tag too
1 parent b4df3ea commit 4625b6e

15 files changed

Lines changed: 512 additions & 294 deletions

File tree

.github/actions/build-library-and-upload/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ runs:
1616
- id: build
1717
run: |
1818
rm -rf out
19-
cmake --preset ${{ inputs.preset }} -DBUILD_SHARED_LIBS=ON
19+
cmake --preset ${{ inputs.preset }}
2020
cmake --build ./build/${{ inputs.preset }} --config ${{ inputs.build-type }} --verbose
2121
cmake --install ./build/${{ inputs.preset }} --config ${{ inputs.build-type }}
2222
working-directory: ${{ inputs.nakama-cpp-path }}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: 'Setup Android'
2+
description: 'Sets up Java and Android SDK components'
3+
inputs:
4+
java-version:
5+
description: 'Java version'
6+
required: false
7+
default: '17'
8+
ndk-version:
9+
description: 'Android NDK version'
10+
required: true
11+
cmake-version:
12+
description: 'Android CMake version'
13+
required: true
14+
compile-sdk:
15+
description: 'Android compileSdk version (used to install platforms;android-<compile-sdk>)'
16+
required: false
17+
default: '34'
18+
build-tools-version:
19+
description: 'Android buildToolsVersion (used to install build-tools;<build-tools-version>)'
20+
required: false
21+
default: '36.0.0'
22+
install-emulator:
23+
description: 'Whether to install emulator packages'
24+
required: false
25+
default: 'false'
26+
system-image:
27+
description: 'Android system image package'
28+
required: false
29+
default: ''
30+
31+
runs:
32+
using: "composite"
33+
steps:
34+
- name: Set up JDK
35+
uses: actions/setup-java@v4
36+
with:
37+
distribution: temurin
38+
java-version: ${{ inputs.java-version }}
39+
40+
- name: Install Android SDK packages
41+
shell: bash
42+
run: |
43+
set -euo pipefail
44+
45+
# Initialize ANDROID_HOME if not set (standard on x86, missing on ARM)
46+
if [ -z "${ANDROID_HOME:-}" ]; then
47+
export ANDROID_HOME="$HOME/android-sdk"
48+
mkdir -p "$ANDROID_HOME"
49+
fi
50+
echo "ANDROID_HOME=$ANDROID_HOME" >> "$GITHUB_ENV"
51+
52+
# Install command-line tools if missing
53+
if [ ! -d "$ANDROID_HOME/cmdline-tools/latest" ]; then
54+
echo "Downloading Android Command Line Tools..."
55+
mkdir -p "$ANDROID_HOME/cmdline-tools"
56+
curl -LSs https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip -o cmdline-tools.zip
57+
unzip -q cmdline-tools.zip -d "$ANDROID_HOME/cmdline-tools"
58+
mv "$ANDROID_HOME/cmdline-tools/cmdline-tools" "$ANDROID_HOME/cmdline-tools/latest"
59+
rm cmdline-tools.zip
60+
fi
61+
62+
SDKMANAGER="$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager"
63+
64+
packages=(
65+
"platforms;android-${{ inputs.compile-sdk }}"
66+
"build-tools;${{ inputs.build-tools-version }}"
67+
"ndk;${{ inputs.ndk-version }}"
68+
"cmake;${{ inputs.cmake-version }}"
69+
)
70+
71+
#use emulator for test-android
72+
if [ "${{ inputs.install-emulator }}" = "true" ]; then
73+
packages+=("emulator")
74+
packages+=("platform-tools")
75+
if [ -n "${{ inputs.system-image }}" ]; then
76+
packages+=("${{ inputs.system-image }}")
77+
fi
78+
fi
79+
80+
# Accept licenses
81+
yes | "$SDKMANAGER" --sdk_root="$ANDROID_HOME" --licenses || true
82+
83+
"$SDKMANAGER" --sdk_root="$ANDROID_HOME" "${packages[@]}"
84+
85+
- name: Set Android environment variables
86+
shell: bash
87+
run: |
88+
export NDK_PATH="$ANDROID_HOME/ndk/${{ inputs.ndk-version }}"
89+
echo "ANDROID_NDK=$NDK_PATH" >> "$GITHUB_ENV"
90+
echo "ANDROID_NDK_HOME=$NDK_PATH" >> "$GITHUB_ENV"
91+
echo "ANDROID_NDK_ROOT=$NDK_PATH" >> "$GITHUB_ENV"
92+
echo "ANDROID_HOME=$ANDROID_HOME" >> "$GITHUB_ENV"
93+
echo "$ANDROID_HOME/cmake/${{ inputs.cmake-version }}/bin" >> "$GITHUB_PATH"
94+
echo "$ANDROID_HOME/platform-tools" >> "$GITHUB_PATH"
95+
echo "$ANDROID_HOME/cmdline-tools/latest/bin" >> "$GITHUB_PATH"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: 'Setup vcpkg for Android'
2+
description: 'Setup vcpkg for Android workflow'
3+
inputs:
4+
vcpkg-path:
5+
description: 'Path to vcpkg'
6+
required: true
7+
8+
runs:
9+
using: "composite"
10+
steps:
11+
- name: Export GitHub Actions cache environment variables
12+
uses: actions/github-script@v7
13+
with:
14+
script: |
15+
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
16+
core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
17+
18+
- name: Set VCPKG_ROOT and Binary Sources
19+
shell: bash
20+
run: |
21+
echo "VCPKG_ROOT=${{ github.workspace }}/${{ inputs.vcpkg-path }}" >> $GITHUB_ENV
22+
echo "VCPKG_BINARY_SOURCES=clear;x-gha,readwrite" >> $GITHUB_ENV
23+
24+
- name: Bootstrap vcpkg
25+
shell: bash
26+
run: ./bootstrap-vcpkg.sh -disableMetrics
27+
working-directory: ${{ inputs.vcpkg-path }}
28+
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
name: Build Android
22
on: [workflow_call, workflow_dispatch]
3+
34
jobs:
45
build_android:
56
timeout-minutes: 30
6-
strategy:
7-
matrix:
8-
preset: ["android-arm64-v8a-host_linux-x64", "android-x64-host_linux-x64", "android-armeabi-v7a-host_linux-x64"]
9-
build-type: [MinSizeRel]
107
runs-on: ubuntu-22.04
118
steps:
12-
- uses: actions/checkout@v3
9+
- name: Checkout nakama-cpp
10+
uses: actions/checkout@v4
1311
with:
1412
path: nakama-cpp
15-
- uses: ./nakama-cpp/.github/actions/setup-vcpkg
13+
submodules: false
14+
15+
- name: Checkout vcpkg submodule
16+
run: |
17+
git submodule update --init --filter=blob:none -- submodules/vcpkg
18+
working-directory: nakama-cpp
19+
20+
- uses: ./nakama-cpp/.github/actions/setup-android
1621
with:
17-
github_token: ${{ secrets.github_token }}
18-
vcpkg-path: vcpkg
19-
- uses: ./nakama-cpp/.github/actions/setup-ubuntu
20-
- uses: ./nakama-cpp/.github/actions/build-library-and-upload
22+
ndk-version: 27.2.12479018
23+
cmake-version: 4.0.2
24+
25+
- uses: ./nakama-cpp/.github/actions/setup-vcpkg-android
2126
with:
22-
nakama-cpp-path: nakama-cpp
23-
preset: ${{ matrix.preset }}
24-
build-type: ${{ matrix.build-type }}
25-
- if: failure()
26-
uses: ./nakama-cpp/.github/actions/handle-failure
27+
vcpkg-path: nakama-cpp/submodules/vcpkg
28+
29+
- name : Build with Gradle
30+
run : ./gradlew build --no-daemon
31+
working-directory: nakama-cpp/android
32+
33+
- name: Upload artifacts
34+
uses: actions/upload-artifact@v4
2735
with:
28-
nakama-cpp-path: nakama-cpp
29-
vcpkg-path: vcpkg
36+
name: nakama-sdk-android-aar
37+
path: nakama-cpp/android/nakama-sdk/build/outputs/aar/*.aar
38+
if-no-files-found: error

.github/workflows/test_android.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Test Android
2+
on:
3+
workflow_call:
4+
workflow_dispatch:
5+
issue_comment:
6+
types: [created]
7+
push:
8+
tags:
9+
- 'v*'
10+
11+
jobs:
12+
test_android:
13+
if: |
14+
github.event_name == 'workflow_call' ||
15+
github.event_name == 'workflow_dispatch' ||
16+
(github.event_name == 'push') ||
17+
(github.event.issue.pull_request && contains(github.event.comment.body, '/test android'))
18+
timeout-minutes: 100
19+
runs-on: ubuntu-22.04
20+
env:
21+
ABI: x86_64
22+
CMAKE_PRESET: android-x86_64
23+
24+
steps:
25+
- name: Checkout nakama-cpp
26+
uses: actions/checkout@v4
27+
with:
28+
path: nakama-cpp
29+
submodules: false
30+
31+
- name: Checkout vcpkg submodule
32+
run: |
33+
git submodule update --init --filter=blob:none -- submodules/vcpkg
34+
working-directory: nakama-cpp
35+
36+
- name: Cache Android SDK
37+
uses: actions/cache@v4
38+
with:
39+
path: ~/android-sdk
40+
key: android-sdk-${{ hashFiles('.github/actions/setup-android/action.yml') }}
41+
restore-keys: |
42+
android-sdk-
43+
44+
- name: Cache Gradle
45+
uses: actions/cache@v4
46+
with:
47+
path: ~/.gradle/caches
48+
key: gradle-${{ hashFiles('android/**/*.gradle.kts', 'android/**/*.gradle') }}
49+
restore-keys: |
50+
gradle-
51+
52+
- uses: ./nakama-cpp/.github/actions/setup-android
53+
with:
54+
ndk-version: 27.2.12479018
55+
cmake-version: 4.0.2
56+
install-emulator: true
57+
system-image: "system-images;android-29;google_apis;x86_64"
58+
59+
- uses: ./nakama-cpp/.github/actions/setup-vcpkg-android
60+
with:
61+
vcpkg-path: nakama-cpp/submodules/vcpkg
62+
63+
- name: Cache CMake build
64+
uses: actions/cache@v4
65+
with:
66+
path: nakama-cpp/build
67+
key: cmake-build-${{ env.CMAKE_PRESET }}-${{ hashFiles('CMakeLists.txt', 'vcpkg.json', '**/CMakePresets.json') }}
68+
restore-keys: |
69+
cmake-build-${{ env.CMAKE_PRESET }}-
70+
71+
- name: Start Nakama server
72+
run: docker compose -f integrationtests/server/docker-compose.yml up -d --build --wait
73+
working-directory: nakama-cpp
74+
75+
- name: Install Task
76+
run: npm install -g @go-task/cli
77+
78+
- name: Build Release AAR
79+
run: ./gradlew :nakama-sdk:assembleRelease -Pabi=$ABI
80+
working-directory: nakama-cpp/android
81+
82+
- name: Upload Release AAR
83+
uses: actions/upload-artifact@v4
84+
with:
85+
name: nakama-sdk-android-aar-${{ env.ABI }}
86+
path: nakama-cpp/android/nakama-sdk/build/outputs/aar/*-release.aar
87+
if-no-files-found: error
88+
89+
- name: Run tests on emulator
90+
uses: reactivecircus/android-emulator-runner@e89f39f1abbbd05b1113a29cf4db69e7540cae5a
91+
with:
92+
api-level: 29
93+
target: google_apis
94+
arch: x86_64
95+
force-avd-creation: false
96+
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none -memory 4096 -cores 2
97+
disable-animations: true
98+
timeout: 50
99+
script: |
100+
set -xe && for port in 7349 7350 7351; do adb reverse tcp:$port tcp:$port; done && cd nakama-cpp && sleep 30 && task test-android ABI=$ABI BUILD_TYPE=MinSizeRel

CMakePresets.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@
114114
"ANDROID_ABI": "armeabi-v7a"
115115
}
116116
},
117+
{
118+
"name": "android-x86_64",
119+
"inherits": ["android-default"],
120+
"cacheVariables": {
121+
"VCPKG_TARGET_TRIPLET": "x86-64-android-heroic",
122+
"ANDROID_ABI": "x86_64"
123+
}
124+
},
117125
{
118126
"name": "linux-amd64",
119127
"inherits": ["default"],

0 commit comments

Comments
 (0)