ci(android): build the APK+AAR on android/, src/ and crates/ changes #71

Merged
sirius merged 1 commit from ci/android-verify into main 2026-09-14 16:25:55 +00:00
3 changed files with 249 additions and 0 deletions

View file

@ -0,0 +1,236 @@
name: android-ci
# Android changes had no CI at all: ci.yml's path allowlist (src/**, crates/**,
# tests/**, Cargo.toml, Cargo.lock, ci.yml) never matches android/**, and
# android-release.yml is dispatch/tag-driven — so an android-only PR ran zero
# jobs and merged on an empty check list. This is the android side of CI.
#
# It builds the same gradle targets the release builds through the same script
# android-release.yml calls (scripts/build-android-local.sh): `:sdk:assembleRelease`
# + `:app:assembleRelease`, Rust for aarch64-linux-android via cargo-ndk, UniFFI
# bindings, APK packaging with the .so. The only difference from a release is
# signing: ONIONWIRE_REQUIRE_RELEASE_KEY=false and no release keystore, so
# Gradle falls back to the debug key. Nothing here is signed for release,
# nothing is published, no tag is touched.
#
# Why a separate workflow instead of android/** added to ci.yml: the only way
# to keep the Rust jobs from running on android-only diffs is job-level change
# detection, and a gate that mis-detects hands out a green check for a suite
# that never ran. Here the runner's own paths filter decides — same mechanism
# ci.yml already uses.
on:
push:
branches: [main]
paths:
- "android/**"
- "crates/**"
- "src/**"
- "Cargo.toml"
- "Cargo.lock"
- "scripts/build-android-local.sh"
- ".forgejo/workflows/android-ci.yml"
pull_request:
paths:
- "android/**"
- "crates/**"
- "src/**"
- "Cargo.toml"
- "Cargo.lock"
- "scripts/build-android-local.sh"
- ".forgejo/workflows/android-ci.yml"
workflow_dispatch:
# Deliberately the SAME group and the SAME volumes as android-release.yml: the
# APK build is ~9 min with warm volumes (SDK/NDK, cargo registry, cargo target,
# Gradle cache) and a cold copy of those is over a gigabyte of downloads per
# runner. Serializing the two workflows by group name is what keeps them off
# each other's volumes.
concurrency:
group: onionwire-android-release-v2
cancel-in-progress: false
env:
CARGO_TERM_COLOR: never
CARGO_BUILD_JOBS: "1"
ONIONWIRE_ANDROID_ABI: arm64-v8a
ANDROID_IMAGE: debian:bookworm
ANDROID_CMDLINE_TOOLS_URL: https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip
RUST_TOOLCHAIN: "1.91"
DOCKER_STATIC_URL: https://download.docker.com/linux/static/stable/x86_64/docker-27.5.1.tgz
# Distinct container prefix: run ids are per-workflow, so this workflow and
# android-release.yml can both be holding a container named "<prefix>-<n>".
BUILD_CONTAINER: onionwire-android-ci-${{ github.run_id }}
SDK_VOLUME: onionwire-android-sdk
CARGO_VOLUME: onionwire-android-cargo-registry
TARGET_VOLUME: onionwire-android-target
GRADLE_VOLUME: onionwire-android-gradle
jobs:
android:
runs-on: docker-amd64
timeout-minutes: 120
steps:
- name: Checkout
uses: https://code.forgejo.org/actions/checkout@v4
with:
fetch-depth: 0
- name: Record runner resources
run: |
set -euo pipefail
cd "${GITHUB_WORKSPACE}"
echo "workspace=$GITHUB_WORKSPACE hostname=$(hostname) arch=$(uname -m)"
nproc || true
free -h || true
df -h || true
ls -l /var/run/docker.sock || true
- name: Ensure docker CLI
run: |
set -euo pipefail
if ! command -v docker >/dev/null 2>&1; then
echo "docker CLI missing in job image; fetching static client"
curl -fsSL "$DOCKER_STATIC_URL" -o /tmp/docker.tgz
tar -xzf /tmp/docker.tgz -C /tmp
install -m 755 /tmp/docker/docker /usr/local/bin/docker
fi
export DOCKER_HOST="${DOCKER_HOST:-unix:///var/run/docker.sock}"
docker version
docker info >/tmp/docker-info.txt
grep -E 'Server Version|Operating System|Total Memory|Name' /tmp/docker-info.txt || true
- name: Build APK + AAR (debug-signed) in bookworm container
id: build
run: |
set -euo pipefail
cd "${GITHUB_WORKSPACE}"
export DOCKER_HOST="${DOCKER_HOST:-unix:///var/run/docker.sock}"
start=$(date +%s)
cat > .ci-android-verify.sh <<'INNER'
#!/usr/bin/env bash
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y --no-install-recommends \
openjdk-17-jdk-headless python3 unzip curl ca-certificates file build-essential pkg-config git
if [ ! -x /opt/android-sdk/cmdline-tools/latest/bin/sdkmanager ]; then
mkdir -p /opt/android-sdk/cmdline-tools
curl -fsSL "$ANDROID_CMDLINE_TOOLS_URL" -o /tmp/cmdline-tools.zip
rm -rf /tmp/cmdline-tools
unzip -q /tmp/cmdline-tools.zip -d /tmp/cmdline-tools
mkdir -p /opt/android-sdk/cmdline-tools/latest
cp -a /tmp/cmdline-tools/cmdline-tools/. /opt/android-sdk/cmdline-tools/latest/
fi
yes | /opt/android-sdk/cmdline-tools/latest/bin/sdkmanager --sdk_root=/opt/android-sdk --licenses >/tmp/sdk-licenses.log || true
/opt/android-sdk/cmdline-tools/latest/bin/sdkmanager --sdk_root=/opt/android-sdk \
"platform-tools" "platforms;android-36" "build-tools;36.0.0" "ndk;28.2.13676358"
test -d /opt/android-sdk/ndk/28.2.13676358
export ANDROID_HOME=/opt/android-sdk ANDROID_SDK_ROOT=/opt/android-sdk
export ANDROID_NDK_HOME=/opt/android-sdk/ndk/28.2.13676358
export PATH="$ANDROID_HOME/build-tools/36.0.0:$ANDROID_HOME/platform-tools:$PATH"
if [ ! -x /cargo/bin/rustup ]; then
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain "$RUST_TOOLCHAIN" --no-modify-path --profile minimal
fi
export PATH="/cargo/bin:$PATH"
rustup toolchain install "$RUST_TOOLCHAIN"
rustup default "$RUST_TOOLCHAIN"
rustup target add aarch64-linux-android
command -v cargo-ndk >/dev/null || cargo install cargo-ndk --locked
echo "=== toolchain ==="
java -version
rustc --version
cargo ndk --version
du -sh /opt/android-sdk /cargo /cargo-target /gradle || true
# No keystore here on purpose: this is a verification build, not a
# release build. The script warns that Gradle fell back to the debug
# key; that warning is expected in this job.
scripts/build-android-local.sh
version="$(python3 - <<'PY'
import re, pathlib
t = pathlib.Path('android/app/build.gradle.kts').read_text()
m = re.search(r'versionName\s*=.*?:\s*"([^"]+)"', t)
print(m.group(1) if m else '')
PY
)"
abi="${ONIONWIRE_ANDROID_ABI:-arm64-v8a}"
apk="dist/onionwire-$version-android-$abi.apk"
aar="dist/onionwire-sdk-$version.aar"
{
echo "=== file ==="
file "$apk" "$aar"
echo "=== unzip lib ==="
unzip -l "$apk" | grep -E 'lib/.+\.so' || true
unzip -l "$apk" | grep "lib/$abi/libonionwire_sdk.so"
echo "=== classes.dex + Compose entrypoints ==="
unzip -l "$apk" | grep -E 'classes[0-9]*\.dex' | head -n3
echo "=== sha256 ==="
( cd dist && sha256sum -c "onionwire-$version-android-$abi.apk.sha256" "onionwire-sdk-$version.aar.sha256" )
echo "=== apksigner (expect debug cert: this is a verification build) ==="
apksigner verify --print-certs "$apk" | sed -n '1,6p'
echo "=== aapt ==="
aapt dump badging "$apk" | sed -n '1,20p' || true
echo "=== sizes ==="
wc -c "$apk" "$aar"
} | tee dist/android-ci-verify.log
INNER
chmod +x .ci-android-verify.sh
docker volume create "$SDK_VOLUME" >/dev/null
docker volume create "$CARGO_VOLUME" >/dev/null
docker volume create "$TARGET_VOLUME" >/dev/null
docker volume create "$GRADLE_VOLUME" >/dev/null
docker rm -f "$BUILD_CONTAINER" >/dev/null 2>&1 || true
docker pull "$ANDROID_IMAGE"
docker create --name "$BUILD_CONTAINER" -i \
-e CARGO_TERM_COLOR=never \
-e CARGO_BUILD_JOBS="$CARGO_BUILD_JOBS" \
-e CARGO_HOME=/cargo \
-e CARGO_TARGET_DIR=/cargo-target \
-e GRADLE_USER_HOME=/gradle \
-e ANDROID_HOME=/opt/android-sdk \
-e ANDROID_SDK_ROOT=/opt/android-sdk \
-e ANDROID_NDK_HOME=/opt/android-sdk/ndk/28.2.13676358 \
-e ONIONWIRE_ANDROID_ABI="$ONIONWIRE_ANDROID_ABI" \
-e ONIONWIRE_REQUIRE_RELEASE_KEY=false \
-e RUST_TOOLCHAIN="$RUST_TOOLCHAIN" \
-e ANDROID_CMDLINE_TOOLS_URL="$ANDROID_CMDLINE_TOOLS_URL" \
-e GRADLE_OPTS="-Dorg.gradle.daemon=false -Dorg.gradle.jvmargs=-Xmx1024m -XX:MaxMetaspaceSize=384m -Dfile.encoding=UTF-8" \
-v "$SDK_VOLUME":/opt/android-sdk \
-v "$CARGO_VOLUME":/cargo \
-v "$TARGET_VOLUME":/cargo-target \
-v "$GRADLE_VOLUME":/gradle \
-w /src \
"$ANDROID_IMAGE" \
bash -euxc 'mkdir -p /src && tar xzf - -C /src && cd /src && bash /src/.ci-android-verify.sh'
tar czf - \
--exclude=./target --exclude=./.git --exclude=./.worktrees \
--exclude=./android/.gradle --exclude=./android/app/build \
--exclude=./android/sdk/build --exclude=./dist \
--exclude=./crates/onionwire-sdk/target \
. \
| docker start -a -i "$BUILD_CONTAINER"
mkdir -p dist
docker cp "$BUILD_CONTAINER:/src/dist/." dist/
end=$(date +%s)
echo "build_wall_clock_seconds=$((end - start))"
echo "wall_clock_seconds=$((end - start))" >> "$GITHUB_OUTPUT"
docker rm -f "$BUILD_CONTAINER" >/dev/null || true
ls -l dist/
- name: Verify artifacts
run: |
set -euo pipefail
cd "${GITHUB_WORKSPACE}"
abi="$ONIONWIRE_ANDROID_ABI"
ls -l dist/
apk="$(ls dist/onionwire-*-android-$abi.apk)"
aar="$(ls dist/onionwire-sdk-*.aar)"
[ -s "$apk" ] || { echo "missing or empty $apk" >&2; exit 1; }
[ -s "$aar" ] || { echo "missing or empty $aar" >&2; exit 1; }
echo "apk=$apk bytes=$(wc -c < "$apk")"
echo "aar=$aar bytes=$(wc -c < "$aar")"
( cd dist && sha256sum -c ./*.sha256 )
echo "=== android-ci-verify.log (from build container) ==="
cat dist/android-ci-verify.log
echo "=== resources after build ==="
free -h || true
df -h || true

View file

@ -345,6 +345,7 @@ FORGEJO_TOKEN=<pat> scripts/build-release-local.sh vX.Y.Z
Workflows (Forgejo Actions, `.forgejo/workflows/`):
- [`.forgejo/workflows/ci.yml`](.forgejo/workflows/ci.yml) — two jobs on the Pi runner (`docker` label), serialized by `onionwire-ci-pi`. `test`: `cargo test --locked` + `cargo clippy --all-targets -- -D warnings` at the repo root. `sdk`: the same for `crates/onionwire-sdk` (separate cargo workspace — own `Cargo.lock`, root crate by path, `rustls` instead of `native-tls` — so a root signature change cannot silently break the Android SDK). Both run on `main` and PRs when `src/`, `crates/`, `tests/`, `Cargo.toml`, `Cargo.lock`, or this workflow change (ignored Tor-live tests are not run). `workflow_dispatch` always runs the suite.
- [`.forgejo/workflows/android-ci.yml`](.forgejo/workflows/android-ci.yml) — the android side of CI, on the `docker-amd64` runner: builds `:sdk:assembleRelease` + `:app:assembleRelease` with the same script the release uses (`scripts/build-android-local.sh`, Rust for `aarch64-linux-android` via cargo-ndk + UniFFI bindings) and verifies the packaged `libonionwire_sdk.so`, checksums and APK badging. Debug keystore, no publish, no tag. Runs on `main` and PRs when `android/`, `src/`, `crates/`, `Cargo.toml`, `Cargo.lock` or `scripts/build-android-local.sh` change. Same concurrency group **and** the same runner volumes as `android-release.yml`, so a PR build never fights a release build over the SDK/NDK, cargo and Gradle caches.
- [`.forgejo/workflows/release.yml`](.forgejo/workflows/release.yml) — manual, batched release (`workflow_dispatch` only: `dry_run`, `bump`, `version`, `tag`): next tag, aarch64 (`docker`) + x86_64 (`docker-amd64`), one release, fail-closed tag delete on failure. Dispatch [`android-release.yml`](.forgejo/workflows/android-release.yml) with that tag to add the APK + AAR (no automatic trigger fires on this Forgejo; it is `workflow_dispatch` only). Concurrency group `onionwire-ci-pi` (same as `ci.yml`, `cancel-in-progress: false`) so cargo jobs do not overlap on the Pi. Amd64 dind is serialized by runner `capacity=1`.
Scripts:

View file

@ -114,6 +114,18 @@ The Android build runs on the **amd64** runner (the Pi is aarch64 and cannot
produce an APK), then a second job on the Pi attaches the assets. It is the
long pole of a release — the Linux binaries land first.
### Android CI (not a release)
`android-ci.yml` runs the same gradle targets on the same amd64 runner for every
PR and `main` push that touches `android/`, `src/`, `crates/`, `Cargo.toml`,
`Cargo.lock` or `scripts/build-android-local.sh`, so an android-only PR is no
longer merged on an empty check list. It signs with the **debug** key
(`ONIONWIRE_REQUIRE_RELEASE_KEY=false`, no keystore) and publishes nothing —
`apksigner` in that job reports the debug cert, which is expected there and
never in `android-release`. It shares `android-release.yml`'s concurrency group
and volumes, so the PR build is warm (~9 min) instead of re-downloading a
gigabyte of SDK/NDK.
## Rebuilding an existing tag
- **Both** Linux binaries: dispatch `release` with `tag=vX.Y.Z`. The tag's own