diff --git a/.forgejo/workflows/android-release.yml b/.forgejo/workflows/android-release.yml new file mode 100644 index 0000000..14311a8 --- /dev/null +++ b/.forgejo/workflows/android-release.yml @@ -0,0 +1,316 @@ +name: android-release + +# Build the Android APK + SDK AAR on the amd64 runner and attach them to the +# Forgejo release for the computed (or requested) tag. Standalone on push to +# main, dispatch, or as a reusable workflow. +# +# First automated version with zero tags: v0.1.0. Afterwards, patch+1 from the +# latest v*.*.* tag. Never move an existing tag. + +on: + push: + branches: [main] + paths: + - "android/**" + - "scripts/build-android-local.sh" + - ".forgejo/workflows/android-release.yml" + workflow_dispatch: + inputs: + tag: + description: Existing tag to (re)build and attach (empty = next version) + required: false + type: string + dry_run: + description: Build and checksum without creating a tag or publishing + required: false + type: boolean + default: false + workflow_call: + inputs: + tag: + required: false + type: string + dry_run: + required: false + type: boolean + default: false + +concurrency: + group: onionwire-android-release + cancel-in-progress: false + +env: + CARGO_TERM_COLOR: never + CARGO_BUILD_JOBS: "1" + ONIONWIRE_ANDROID_ABI: arm64-v8a + ONIONWIRE_REQUIRE_RELEASE_KEY: "true" + # The dind pod is capped at 4Gi. Keep Gradle small or the job is 137. + GRADLE_OPTS: "-Dorg.gradle.daemon=false -Dorg.gradle.jvmargs=-Xmx1024m -XX:MaxMetaspaceSize=384m -Dfile.encoding=UTF-8" + ANDROID_CMDLINE_TOOLS_URL: https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip + RUST_TOOLCHAIN: "1.91" + +jobs: + android: + runs-on: docker-amd64 + timeout-minutes: 90 + steps: + - name: Checkout + uses: https://code.forgejo.org/actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ inputs.tag || github.event.inputs.tag || github.sha }} + + - name: Record runner resources + run: | + set -euo pipefail + cd "${GITHUB_WORKSPACE}" + echo "workspace=$GITHUB_WORKSPACE hostname=$(hostname) arch=$(uname -m)" + nproc + free -h || true + df -h || true + + - name: Compute version + id: version + env: + INPUT_TAG: ${{ github.event.inputs.tag || inputs.tag }} + INPUT_DRY_RUN: ${{ github.event.inputs.dry_run }} + CALL_DRY_RUN: ${{ inputs.dry_run }} + run: | + set -euo pipefail + cd "${GITHUB_WORKSPACE}" + git fetch --tags --force origin || true + input_tag="${INPUT_TAG:-}" + dry_run=false + case "${INPUT_DRY_RUN:-}" in true|TRUE|True|1|yes|YES) dry_run=true ;; esac + case "${CALL_DRY_RUN:-}" in true|TRUE|True|1|yes|YES) dry_run=true ;; esac + if [ -n "$input_tag" ]; then + ver="${input_tag#v}" + else + latest="$(git tag -l 'v*.*.*' --sort=-v:refname | head -n1 || true)" + if [ -z "$latest" ]; then + ver="0.1.0" + else + ver="${latest#v}" + major="${ver%%.*}" + rest="${ver#*.}" + minor="${rest%%.*}" + patch="${rest#*.}" + patch=$((patch + 1)) + ver="${major}.${minor}.${patch}" + fi + fi + echo "$ver" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$' || { + echo "not a MAJOR.MINOR.PATCH version: $ver" >&2 + exit 1 + } + major="${ver%%.*}" + rest="${ver#*.}" + minor="${rest%%.*}" + patch="${rest#*.}" + code=$((major * 10000 + minor * 100 + patch)) + tag="v$ver" + { + echo "version=$ver" + echo "tag=$tag" + echo "version_code=$code" + echo "dry_run=$dry_run" + } >> "$GITHUB_OUTPUT" + echo "version=$ver tag=$tag versionCode=$code dry_run=$dry_run" + + - name: Install JDK, Rust, Android SDK, cargo-ndk + run: | + set -euo pipefail + cd "${GITHUB_WORKSPACE}" + export DEBIAN_FRONTEND=noninteractive + # T480 dind timed out on http://deb.debian.org:80 (Fastly). HTTPS works + # for Forgejo checkout; flip the debian mirrors before apt-get update. + if [ -f /etc/apt/sources.list ]; then + sed -i 's|http://deb.debian.org|https://deb.debian.org|g; s|http://security.debian.org|https://security.debian.org|g; s|http://deb.debian.org/debian-security|https://deb.debian.org/debian-security|g' /etc/apt/sources.list + fi + ls /etc/apt/sources.list.d 2>/dev/null || true + cat /etc/apt/sources.list || true + apt-get -o Acquire::Retries=3 -o Acquire::https::Timeout=60 update + apt-get install -y --no-install-recommends \ + openjdk-17-jdk-headless python3 python3-minimal unzip curl ca-certificates \ + file build-essential pkg-config git + java -version + python3 --version + + export ANDROID_HOME="${ANDROID_HOME:-$HOME/android-sdk}" + export ANDROID_SDK_ROOT="$ANDROID_HOME" + mkdir -p "$ANDROID_HOME/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 "$ANDROID_HOME/cmdline-tools/latest" + cp -a /tmp/cmdline-tools/cmdline-tools/. "$ANDROID_HOME/cmdline-tools/latest/" + yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --sdk_root="$ANDROID_HOME" --licenses >/tmp/sdk-licenses.log || true + "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --sdk_root="$ANDROID_HOME" \ + "platform-tools" "platforms;android-36" "build-tools;36.0.0" "ndk;28.2.13676358" + test -d "$ANDROID_HOME/ndk/28.2.13676358" + echo "ANDROID_HOME=$ANDROID_HOME" + du -sh "$ANDROID_HOME" "$ANDROID_HOME/ndk/28.2.13676358" || true + + if [ ! -x "$HOME/.cargo/bin/rustup" ]; then + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain "$RUST_TOOLCHAIN" + fi + # shellcheck disable=SC1091 + source "$HOME/.cargo/env" + rustup toolchain install "$RUST_TOOLCHAIN" + rustup default "$RUST_TOOLCHAIN" + rustup target add aarch64-linux-android + rustc --version + cargo --version + cargo install cargo-ndk --locked + cargo ndk --version + + echo "ANDROID_HOME=$ANDROID_HOME" >> "$GITHUB_ENV" + echo "ANDROID_SDK_ROOT=$ANDROID_HOME" >> "$GITHUB_ENV" + echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/28.2.13676358" >> "$GITHUB_ENV" + echo "$HOME/.cargo/bin" >> "$GITHUB_PATH" + echo "$ANDROID_HOME/cmdline-tools/latest/bin" >> "$GITHUB_PATH" + echo "$ANDROID_HOME/platform-tools" >> "$GITHUB_PATH" + echo "$ANDROID_HOME/build-tools/36.0.0" >> "$GITHUB_PATH" + echo "=== resources after SDK install ===" + free -h || true + df -h || true + du -sh "$HOME/android-sdk" "$HOME/.cargo" 2>/dev/null || true + + - name: Decode release keystore + env: + ANDROID_KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }} + ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }} + ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }} + ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }} + run: | + set -euo pipefail + cd "${GITHUB_WORKSPACE}" + : "${ANDROID_KEYSTORE_BASE64:?ANDROID_KEYSTORE_BASE64 secret is missing}" + : "${ANDROID_KEYSTORE_PASSWORD:?ANDROID_KEYSTORE_PASSWORD secret is missing}" + : "${ANDROID_KEY_ALIAS:?ANDROID_KEY_ALIAS secret is missing}" + : "${ANDROID_KEY_PASSWORD:?ANDROID_KEY_PASSWORD secret is missing}" + mkdir -p "$HOME/.android" + chmod 700 "$HOME/.android" + ks="$HOME/.android/onionwire-release.jks" + envf="$HOME/.android/onionwire-release.env" + printf '%s' "$ANDROID_KEYSTORE_BASE64" | base64 -d > "$ks" + chmod 600 "$ks" + test -s "$ks" + umask 077 + printf 'ANDROID_KEYSTORE_FILE=%s\nANDROID_KEYSTORE_PASSWORD=%s\nANDROID_KEY_ALIAS=%s\nANDROID_KEY_PASSWORD=%s\n' \ + "$ks" "$ANDROID_KEYSTORE_PASSWORD" "$ANDROID_KEY_ALIAS" "$ANDROID_KEY_PASSWORD" > "$envf" + chmod 600 "$envf" + echo "keystore bytes=$(wc -c < "$ks") written under ~/.android (not logged)" + + - name: Build APK + AAR + run: | + set -euo pipefail + cd "${GITHUB_WORKSPACE}" + # shellcheck disable=SC1091 + source "$HOME/.cargo/env" 2>/dev/null || true + export ANDROID_HOME="${ANDROID_HOME:?}" + export ANDROID_SDK_ROOT="$ANDROID_HOME" + export ANDROID_NDK_HOME="${ANDROID_NDK_HOME:?}" + export ONIONWIRE_VERSION_CODE="${{ steps.version.outputs.version_code }}" + export ONIONWIRE_REQUIRE_RELEASE_KEY=true + start=$(date +%s) + scripts/build-android-local.sh "${{ steps.version.outputs.version }}" + end=$(date +%s) + echo "build_wall_clock_seconds=$((end - start))" + + - name: Verify artifacts + run: | + set -euo pipefail + cd "${GITHUB_WORKSPACE}" + version="${{ steps.version.outputs.version }}" + abi="$ONIONWIRE_ANDROID_ABI" + apk="dist/onionwire-$version-android-$abi.apk" + aar="dist/onionwire-sdk-$version.aar" + [ -s "$apk" ] || { echo "missing or empty $apk" >&2; exit 1; } + [ -s "$aar" ] || { echo "missing or empty $aar" >&2; exit 1; } + apk_bytes=$(wc -c < "$apk") + aar_bytes=$(wc -c < "$aar") + echo "apk_bytes=$apk_bytes aar_bytes=$aar_bytes" + [ "$apk_bytes" -gt 0 ] + [ "$aar_bytes" -gt 0 ] + file "$apk" "$aar" + echo "=== unzip -l lib ===" + unzip -l "$apk" | grep -E 'lib/.+\.so|AndroidManifest.xml' || true + unzip -l "$apk" | grep "lib/$abi/libonionwire_sdk.so" + echo "=== sha256 ===" + ( cd dist && sha256sum -c "onionwire-$version-android-$abi.apk.sha256" "onionwire-sdk-$version.aar.sha256" ) + echo "=== apksigner ===" + apksigner verify --print-certs "$apk" + echo "=== aapt dump badging ===" + aapt dump badging "$apk" | head -n 20 + echo "=== resources after build ===" + free -h || true + df -h || true + + - name: Publish (create-or-join) + if: steps.version.outputs.dry_run != 'true' + env: + FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }} + REPO_API: ${{ github.server_url }}/api/v1/repos/${{ github.repository }} + EVENT_SHA: ${{ github.sha }} + TAG: ${{ steps.version.outputs.tag }} + VERSION: ${{ steps.version.outputs.version }} + run: | + set -euo pipefail + cd "${GITHUB_WORKSPACE}" + : "${FORGEJO_TOKEN:?FORGEJO_TOKEN secret is missing}" + apk="dist/onionwire-$VERSION-android-$ONIONWIRE_ANDROID_ABI.apk" + aar="dist/onionwire-sdk-$VERSION.aar" + for f in "$apk" "$apk.sha256" "$aar" "$aar.sha256"; do + bytes=$(wc -c < "$f") + [ "$bytes" -gt 0 ] || { echo "refusing to publish empty $f" >&2; exit 1; } + done + + # Give the core workflow ~60s to create the tag first. If it exists, + # join that release and never send target_commitish (would move it). + tag_exists=0 + i=0 + while [ "$i" -lt 6 ]; do + code=$(curl -sS --http1.1 -o /tmp/tag.json -w '%{http_code}' \ + -H "Authorization: Bearer $FORGEJO_TOKEN" \ + "$REPO_API/tags/$TAG" || true) + if [ "$code" = "200" ]; then + tag_exists=1 + echo "tag $TAG already exists; joining" + break + fi + i=$((i + 1)) + echo "tag $TAG not found yet (http $code), wait 10s ($i/6)" + sleep 10 + done + + if [ "$tag_exists" = "1" ]; then + unset TARGET_COMMITISH || true + else + export TARGET_COMMITISH="$EVENT_SHA" + echo "creating tag $TAG at $TARGET_COMMITISH" + fi + + published=0 + attempt=1 + while [ "$attempt" -le 5 ]; do + if scripts/publish-release.sh \ + "$TAG" "OnionWire $TAG" \ + scripts/release-body.md \ + "$apk" "$apk.sha256" "$aar" "$aar.sha256"; then + published=1 + break + fi + echo "publish attempt $attempt failed; retry in 10s" + unset TARGET_COMMITISH || true + attempt=$((attempt + 1)) + sleep 10 + done + [ "$published" = "1" ] || { echo "publish failed after retries" >&2; exit 1; } + + - name: Scrub keystore + if: always() + run: | + set -euo pipefail + rm -f "$HOME/.android/onionwire-release.jks" "$HOME/.android/onionwire-release.env" "$HOME/onionwire-release.jks" + echo "keystore removed from runner home" diff --git a/android/README.md b/android/README.md index 1667ae6..890ba71 100644 --- a/android/README.md +++ b/android/README.md @@ -48,7 +48,7 @@ Or from the repo root, with checksums and an optional publish step: ```bash scripts/build-android-local.sh # -> dist/onionwire--android-arm64-v8a.apk -PUBLISH_TAG=v0.3.0 scripts/build-android-local.sh +PUBLISH_TAG=v0.1.0 scripts/build-android-local.sh ``` ## Decisions you should know about @@ -96,12 +96,20 @@ encrypted at rest; identity keys are not — that is the locked v1 posture. ### Signing -Both `debug` and `release` are signed with the **standard debug keystore** -(`~/.android/debug.keystore`, auto-created by AGP). This is what makes the -release APK installable for sideloading. It is **not** a Play upload key; -nothing here should be uploaded to Play. When a real upload key exists, put it -in `~/.gradle/gradle.properties` or CI secrets and reference it from -`signingConfigs` — never in this repository. +Release APKs are signed with a **repo-owned keystore**, not the debug key. + +* CI reads `ANDROID_KEYSTORE_BASE64`, `ANDROID_KEYSTORE_PASSWORD`, + `ANDROID_KEY_ALIAS`, and `ANDROID_KEY_PASSWORD` from Forgejo repo secrets + and writes them to `~/.android/onionwire-release.env` for the build. +* Locally, `scripts/build-android-local.sh` sources that same env file if + present (`~/.android/onionwire-release.jks` + `.env`, mode 600). Local and + CI APKs then verify as the same cert and can upgrade in place. +* If the env file is missing, Gradle **warns** and signs with the debug + keystore so a laptop build still installs. CI sets + `ONIONWIRE_REQUIRE_RELEASE_KEY=true` and **fails closed** instead — a + per-machine debug key would make every CI APK uninstallable over the last. +* Never commit a keystore, password, or `android/local.properties`. This is + not a Play upload key; nothing here should be uploaded to Play. ### Release builds are not minified @@ -113,10 +121,17 @@ deobfuscation, not before. ## CI -`.forgejo/workflows/release.yml` runs on an aarch64 Linux container on the Pi. -It cannot build an APK. Android artifacts are built on a host with the SDK/NDK -via `scripts/build-android-local.sh`, exactly like the x86_64 Linux binary is -built via `scripts/build-release-local.sh`. +`.forgejo/workflows/android-release.yml` runs on `docker-amd64` (T480 dind). +Any push to `main` that touches `android/**` (or the local build script / this +workflow) builds `onionwire--android-arm64-v8a.apk` and +`onionwire-sdk-.aar`, verifies them, and attaches them to the Forgejo +release for that tag. Version is the latest `v*.*.*` tag plus patch, or +**v0.1.0** when there are no tags. `versionName` matches the tag; +`versionCode` is `major*10000 + minor*100 + patch` (so 0.1.0 → 100). + +`workflow_dispatch` with `dry_run=true` builds and checksums without creating +a tag or publishing. `scripts/build-android-local.sh` remains the offline +fallback; it does not create tags. ## Layout diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 8482400..0c5f844 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -6,6 +6,9 @@ plugins { alias(libs.plugins.kotlin.compose) } +fun envOrNull(name: String): String? = + System.getenv(name)?.takeIf { it.isNotBlank() } + android { namespace = "com.siriusdevops.onionwire" compileSdk = 36 @@ -21,13 +24,30 @@ android { applicationId = "com.siriusdevops.onionwire" minSdk = 26 targetSdk = 36 - versionCode = 1 - versionName = "0.1.0" + // CI/local release scripts override both via -P. Committed values are + // the offline default and match the first automated tag (v0.1.0 → 100). + versionCode = (findProperty("onionwire.versionCode") as String?)?.toIntOrNull() ?: 100 + versionName = (findProperty("onionwire.versionName") as String?) ?: "0.1.0" ndk { abiFilters += onionwireAbis } } + signingConfigs { + create("release") { + val ksPath = envOrNull("ANDROID_KEYSTORE_FILE") + if (ksPath != null) { + storeFile = file(ksPath) + storePassword = envOrNull("ANDROID_KEYSTORE_PASSWORD") + ?: error("ANDROID_KEYSTORE_PASSWORD is required when ANDROID_KEYSTORE_FILE is set") + keyAlias = envOrNull("ANDROID_KEY_ALIAS") + ?: error("ANDROID_KEY_ALIAS is required when ANDROID_KEYSTORE_FILE is set") + keyPassword = envOrNull("ANDROID_KEY_PASSWORD") + ?: error("ANDROID_KEY_PASSWORD is required when ANDROID_KEYSTORE_FILE is set") + } + } + } + buildTypes { debug { // Sideload / device-test builds. Signed with the standard debug key. @@ -43,9 +63,28 @@ android { // :sdk/consumer-rules.pro. isMinifyEnabled = false isShrinkResources = false - // Signed with the debug keystore so the release APK installs. - // This is NOT a Play upload key — see android/README.md. - signingConfig = signingConfigs.getByName("debug") + // Repo-owned release keystore via ANDROID_KEYSTORE_* env (Forgejo + // secrets in CI, ~/.android/onionwire-release.env locally). CI sets + // ONIONWIRE_REQUIRE_RELEASE_KEY=true and fails closed if missing — + // the debug keystore is per-machine and would break in-place upgrades. + val requireReleaseKey = + (findProperty("onionwire.requireReleaseKey") as String?) == "true" || + envOrNull("ONIONWIRE_REQUIRE_RELEASE_KEY") == "true" + val releaseSigning = signingConfigs.getByName("release") + val releaseStore = releaseSigning.storeFile + signingConfig = when { + releaseStore != null && releaseStore.isFile -> releaseSigning + requireReleaseKey -> error( + "Release keystore required but ANDROID_KEYSTORE_FILE is missing or not a file" + ) + else -> { + logger.warn( + "No ANDROID_KEYSTORE_FILE; signing the release APK with the debug keystore. " + + "CI must not do this — in-place upgrades will break." + ) + signingConfigs.getByName("debug") + } + } } } diff --git a/scripts/build-android-local.sh b/scripts/build-android-local.sh index dd4ad64..006d2bf 100755 --- a/scripts/build-android-local.sh +++ b/scripts/build-android-local.sh @@ -1,31 +1,55 @@ #!/usr/bin/env bash -# Build the Android artifacts on this host and publish them to a Forgejo -# release. There is no Android toolchain in CI: .forgejo/workflows/release.yml -# runs on an aarch64 Linux container and cannot produce an APK. This is the -# Android path, exactly like scripts/build-release-local.sh is the x86_64 path -# for the Linux binary. +# Build the Android artifacts on this host. Offline fallback for +# .forgejo/workflows/android-release.yml (which builds on docker-amd64). # # Usage: # scripts/build-android-local.sh [version] # build + checksums only -# PUBLISH_TAG=v0.3.0 scripts/build-android-local.sh # also upload +# PUBLISH_TAG=v0.1.0 scripts/build-android-local.sh # also upload # # Requires: ANDROID_HOME (or android/local.properties), an installed NDK, the # Rust android targets, cargo-ndk, JDK 17+, and a working `cargo` on PATH. -# Does not create or push tags: creating the tag is a human decision. +# Does not create or push tags: CI owns tag creation. This script only uploads +# when PUBLISH_TAG is set. +# +# Signing: if ~/.android/onionwire-release.env exists it is sourced so local +# and CI APKs share the repo-owned keystore. Without it, Gradle falls back to +# the debug keystore and warns. Never commit a keystore or password. set -euo pipefail root="$(cd "$(dirname "$0")/.." && pwd)" cd "$root" -version="${1:-$(sed -n 's/.*versionName = "\(.*\)".*/\1/p' android/app/build.gradle.kts | head -1)}" +if [ -f "$HOME/.android/onionwire-release.env" ]; then + # shellcheck disable=SC1091 + set -a + # shellcheck disable=SC1090 + source "$HOME/.android/onionwire-release.env" + set +a +fi + +# Arg wins; otherwise the committed Gradle default (today: 0.1.0). +version="${1:-}" +if [ -z "$version" ]; then + version="$(python3 -c " +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 '') +")" +fi [ -n "$version" ] || { echo "could not determine version" >&2; exit 1; } -abi="${ONIONWIRE_ANDROID_ABI:-arm64-v8a}" -gradlew="$root/android/gradlew" +if [ -z "${ONIONWIRE_VERSION_CODE:-}" ]; then + ONIONWIRE_VERSION_CODE="$(python3 -c "m,n,p=map(int,'$version'.split('.')); print(m*10000+n*100+p)")" +fi -echo "==> android build: version $version, abi $abi" +abi="${ONIONWIRE_ANDROID_ABI:-arm64-v8a}" + +echo "==> android build: version $version (versionCode $ONIONWIRE_VERSION_CODE), abi $abi" ( cd android && "./gradlew" --no-daemon \ "-Ponionwire.abis=$abi" \ + "-Ponionwire.versionName=$version" \ + "-Ponionwire.versionCode=$ONIONWIRE_VERSION_CODE" \ :sdk:assembleRelease :app:assembleRelease ) mkdir -p dist