From 772cdf3c9a8d7ad3e6435442e78fb8262dcf40b5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 16:36:20 +0000 Subject: [PATCH] Android signing from CI's secrets The debug and release signing configs take a PKCS12 keystore from ANDROID_KEYSTORE_FILE and its password, alias and key password (the store's if empty) from the environment; without them debug builds keep the SDK's debug key and release builds stay unsigned. CI decodes ANDROID_KEYSTORE_BASE64 into that file: the build job with the repository's (debug) secrets, a new release job (assembleRelease, bundleRelease) with the protected release environment's on main and v* tags, the debug key on a pull request. Fork pull requests, with no secrets, build as before. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRRuUkndpo3FJrWFe84 --- .github/workflows/android.yml | 96 +++++++++++++++++++++++++++++++++++ README.md | 8 +++ app/build.gradle.kts | 25 +++++++++ 3 files changed, 129 insertions(+) diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index bbae92e..cfe4551 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -5,15 +5,27 @@ # device: the instrumented tests on an emulator (x86_64, the one ABI it # builds): the native library loads, binds and answers AT commands. # +# release: the release APK and bundle (R8): signed with the release +# environment's key for main and v* tags, the debug key for a pull request. +# # D-Modem-RE is a private submodule: the DMODEM_TOKEN secret is a token # that can read it (a fine-grained personal access token with Contents: # read on D-Modem/D-Modem). +# +# Signing: ANDROID_KEYSTORE_BASE64 (a PKCS12 keystore, base64), +# ANDROID_KEYSTORE_PASSWORD, ANDROID_KEY_ALIAS and ANDROID_KEY_PASSWORD +# (the store's if empty). The repository's secrets hold the debug +# keystore, the protected "release" environment's the release one (an +# environment's secret wins over the repository's of the same name). A +# fork's pull request has neither: its builds take the SDK's debug key +# (and its release build is unsigned). name: Android # (a branch's commits run once, as its pull request: pushed, only main's) on: push: branches: [main] + tags: ["v*"] pull_request: workflow_dispatch: @@ -65,8 +77,30 @@ jobs: - name: OpenSSH, PuTTY, mksh (the interop tests), gawk (proot's build) run: sudo apt-get install -y -q openssh-client openssh-server putty-tools gawk mksh && sudo mkdir -p /run/sshd + # the keystore from its secret, for the build (app/build.gradle.kts); none on a fork's pull request + - name: Signing keystore + env: + KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }} + run: | + if [ -n "$KEYSTORE_BASE64" ]; then + printf '%s' "$KEYSTORE_BASE64" | tr -d '[:space:]' | base64 -d > "$RUNNER_TEMP/keystore.p12" + echo "ANDROID_KEYSTORE_FILE=$RUNNER_TEMP/keystore.p12" >> "$GITHUB_ENV" + else + echo "No keystore secret: the SDK's debug key (a release build unsigned)" + fi + - name: Build, unit tests, lint run: ./gradlew assembleDebug assembleDebugAndroidTest testDebugUnitTest lintDebug :shared:jvmTest :shared:linuxX64Test + env: + ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }} + ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }} + ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }} + + - name: The APK's signer + if: ${{ !cancelled() && hashFiles('app/build/outputs/apk/debug/app-debug.apk') != '' }} + run: | + tools=$(ls -d "$ANDROID_HOME"/build-tools/* | sort -V | tail -1) + "$tools/apksigner" verify --print-certs app/build/outputs/apk/debug/app-debug.apk # the APK even when a test fails, so a build can still be tried on a phone; as # the .apk itself, not zipped (archive: false, upload-artifact v7: the artifact @@ -85,6 +119,68 @@ jobs: app/build/reports app/build/test-results + release: + runs-on: ubuntu-24.04 + # (a hung build ends here, not at GitHub's 6 hours) + timeout-minutes: 40 + # main and v* tags: the release environment and its key (that environment takes only those); + # anything else (a pull request) none, so the repository's debug key + environment: ${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) && 'release' || '' }} + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + token: ${{ secrets.DMODEM_TOKEN || github.token }} + + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: "21" + + - uses: gradle/actions/setup-gradle@v4 + + - name: SDK packages + run: | + yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses > /dev/null + "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" \ + "ndk;$NDK_VERSION" "cmake;$CMAKE_VERSION" "platforms;android-37.0" + + - name: gawk (proot's build) + run: sudo apt-get install -y -q gawk + + # the keystore from its secret, for the build (app/build.gradle.kts); none on a fork's pull request + - name: Signing keystore + env: + KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }} + run: | + if [ -n "$KEYSTORE_BASE64" ]; then + printf '%s' "$KEYSTORE_BASE64" | tr -d '[:space:]' | base64 -d > "$RUNNER_TEMP/keystore.p12" + echo "ANDROID_KEYSTORE_FILE=$RUNNER_TEMP/keystore.p12" >> "$GITHUB_ENV" + else + echo "No keystore secret: the SDK's debug key (a release build unsigned)" + fi + + - name: Release APK and bundle + run: ./gradlew assembleRelease bundleRelease + env: + ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }} + ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }} + ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }} + + - name: The APK's signer + if: ${{ env.ANDROID_KEYSTORE_FILE != '' }} + run: | + tools=$(ls -d "$ANDROID_HOME"/build-tools/* | sort -V | tail -1) + "$tools/apksigner" verify --print-certs app/build/outputs/apk/release/app-release.apk + + - uses: actions/upload-artifact@v4 + with: + name: release + path: | + app/build/outputs/apk/release/*.apk + app/build/outputs/bundle/release/*.aab + app/build/outputs/mapping/release/mapping.txt + native: runs-on: ubuntu-24.04 # (a hung test ends here, not at GitHub's 6 hours) diff --git a/README.md b/README.md index 7453318..899bda0 100644 --- a/README.md +++ b/README.md @@ -299,6 +299,14 @@ D-Modem-RE's own rule has it. `tools/doc_check.py` lists any that don't CI (`.github/workflows/android.yml`) runs all three, and that check; it needs a `DMODEM_TOKEN` secret that can read the private D-Modem-RE submodule. +Signing comes from the environment: `ANDROID_KEYSTORE_FILE` (a PKCS12 +keystore), `ANDROID_KEYSTORE_PASSWORD`, `ANDROID_KEY_ALIAS` and +`ANDROID_KEY_PASSWORD` (the store's password if unset). Without them a +debug build takes the SDK's debug key and a release build is unsigned. CI +decodes `ANDROID_KEYSTORE_BASE64` into that file: the repository's secrets +hold the debug keystore, the protected `release` environment's (main and +`v*` tags) the release one, which CI's `release` job signs with. + ## Apple's systems: the iOS app The native modem builds for iOS and macOS too: the engine's C API, diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 6717b12..1592e9c 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -26,6 +26,14 @@ val appAbis = providers.gradleProperty("dmodem.abis").orNull ?.split(',')?.map { it.trim() }?.filter { it.isNotEmpty() } ?: listOf("arm64-v8a", "armeabi-v7a", "x86_64", "x86") +// signing from the environment, as CI has it (android.yml): ANDROID_KEYSTORE_FILE (a PKCS12 keystore), +// ANDROID_KEYSTORE_PASSWORD, ANDROID_KEY_ALIAS and ANDROID_KEY_PASSWORD (the store's if empty: PKCS12 has one). +// Without them, debug builds take the SDK's own debug key and release builds are unsigned. +val signingEnv = listOf("ANDROID_KEYSTORE_FILE", "ANDROID_KEYSTORE_PASSWORD", "ANDROID_KEY_ALIAS", "ANDROID_KEY_PASSWORD") + .associateWith { providers.environmentVariable(it).orNull?.takeIf { v -> v.isNotEmpty() } } +val signingKeystore = signingEnv["ANDROID_KEYSTORE_FILE"]?.let { file(it) }?.takeIf { it.isFile } + ?.takeIf { signingEnv["ANDROID_KEYSTORE_PASSWORD"] != null && signingEnv["ANDROID_KEY_ALIAS"] != null } + android { namespace = "org.dmodem.app.android" compileSdk = 37 @@ -74,8 +82,25 @@ android { } } + signingConfigs { + if (signingKeystore != null) { + // the one keystore CI gives both: its debug key for a pull request's builds, the release + // environment's for main's and tags' release build + for (name in listOf("debug", "release")) { + maybeCreate(name).apply { + storeFile = signingKeystore + storeType = "PKCS12" + storePassword = signingEnv["ANDROID_KEYSTORE_PASSWORD"] + keyAlias = signingEnv["ANDROID_KEY_ALIAS"] + keyPassword = signingEnv["ANDROID_KEY_PASSWORD"] ?: signingEnv["ANDROID_KEYSTORE_PASSWORD"] + } + } + } + } + buildTypes { release { + if (signingKeystore != null) signingConfig = signingConfigs.getByName("release") isMinifyEnabled = true isShrinkResources = true proguardFiles(