Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

build(android): sign release APKs with the release key and ship them from the release workflow

wquguru224552e0a4b1
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index ed6c350..db446fb 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -2,7 +2,8 @@ name: release
# One tag, one version for every platform (VERSION). The CHANGELOG.md section says which platforms
# changed (scripts/release_platforms.sh); only those are built and attached, so an app with no
-# change finds no new release for itself. Android waits for release signing (M3).
+# change finds no new release for itself. Android ships only once its signing key is in the
+# secrets (android/scripts/keystore.sh --github).
on:
push:
tags: ["v*.*.*"]
@@ -26,6 +27,7 @@ jobs:
mac: ${{ steps.platforms.outputs.mac }}
windows: ${{ steps.platforms.outputs.windows }}
linux: ${{ steps.platforms.outputs.linux }}
+ android: ${{ steps.platforms.outputs.android }}
steps:
- uses: actions/checkout@v4
with:
@@ -36,11 +38,17 @@ jobs:
[[ "v$VERSION" == "$TAG" ]] || { echo "tag $TAG != VERSION $VERSION"; exit 1; }
- id: platforms
name: Platforms in this release
+ env:
+ HAS_ANDROID_KEY: ${{ secrets.ANDROID_KEYSTORE_BASE64 != '' }}
run: |
shipped=$(scripts/release_platforms.sh "${TAG#v}")
+ if grep -qx android <<< "$shipped" && [[ "$HAS_ANDROID_KEY" != true ]]; then
+ echo "::warning::Android left out: no ANDROID_KEYSTORE_BASE64 secret (android/scripts/keystore.sh --github)"
+ shipped=$(grep -vx android <<< "$shipped" || true)
+ fi
echo "$shipped"
- grep -qxE "mac|windows|linux" <<< "$shipped" || { echo "nothing to build: Android has no release build yet"; exit 1; }
- for p in mac windows linux; do
+ grep -qxE "mac|windows|linux|android" <<< "$shipped" || { echo "nothing to build"; exit 1; }
+ for p in mac windows linux android; do
grep -qx "$p" <<< "$shipped" && echo "$p=true" >> "$GITHUB_OUTPUT" || echo "$p=false" >> "$GITHUB_OUTPUT"
done
@@ -132,8 +140,43 @@ jobs:
name: linux
path: dist/*.tar.gz
+ # Signed with the one release key; a debug-signed APK fails the check.
+ android:
+ needs: plan
+ if: needs.plan.outputs.android == 'true'
+ runs-on: ubuntu-latest
+ defaults:
+ run:
+ working-directory: android
+ steps:
+ - uses: actions/checkout@v4
+ with:
+ ref: ${{ env.TAG }}
+ - uses: actions/setup-java@v4
+ with:
+ distribution: temurin
+ java-version: "21"
+ - uses: gradle/actions/setup-gradle@v4
+ with:
+ gradle-version: "9.8.2"
+ - name: Build and sign
+ env:
+ ANDROID_KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
+ CICADA_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
+ run: |
+ export CICADA_KEYSTORE="$RUNNER_TEMP/release.jks"
+ base64 -d <<< "$ANDROID_KEYSTORE_BASE64" > "$CICADA_KEYSTORE"
+ gradle --console=plain -q :app:assembleRelease
+ mkdir -p ../dist && cp app/build/outputs/apk/release/app-release.apk "../dist/Cicada-${TAG#v}-Android.apk"
+ scripts/check-release.sh "../dist/Cicada-${TAG#v}-Android.apk"
+ rm -f "$CICADA_KEYSTORE"
+ - uses: actions/upload-artifact@v4
+ with:
+ name: android
+ path: dist/*.apk
+
publish:
- needs: [plan, mac, windows, linux]
+ needs: [plan, mac, windows, linux, android]
# Skipped platforms are fine; a failed or cancelled one stops the release.
if: ${{ !failure() && !cancelled() }}
runs-on: ubuntu-latest
diff --git a/.gitignore b/.gitignore
index af68da2..c83259c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,3 +27,7 @@ windows/**/bin/
windows/**/obj/
windows/spike/out/
windows/spike/clips/*.wav
+
+# Signing keys never enter the repo (android/scripts/keystore.sh keeps them in ~/.cicada-android)
+*.jks
+*.keystore
diff --git a/android/README.md b/android/README.md
index a65b47d..ea9a534 100644
--- a/android/README.md
+++ b/android/README.md
@@ -22,7 +22,8 @@ Cicada 的 Android 版:把手机里播放的声音(或面对面时麦克风
```bash
scripts/install-sdk.sh # 一次:JDK、Gradle、Android SDK、模拟器装到 ~/.cicada-android,不用 sudo;版本固定,下载都校验哈希;--old 再装一个 Android 10 模拟器
scripts/test.sh # 单元测试
-scripts/build.sh # 开发版 build/Cicada-dev.apk(com.wquguru.cicada.dev);RELEASE=1 出未签名的 build/Cicada-unsigned.apk(签名在 M3)
+scripts/build.sh # 开发版 build/Cicada-dev.apk(com.wquguru.cicada.dev);RELEASE=1 出签名的 build/Cicada-<版本>-Android.apk
+scripts/keystore.sh # 一次:生成发布签名密钥(~/.cicada-android/release.jks,密码在钥匙串);--github 顺便存进 Actions secrets。文件和密码都要备份,丢了就没法给已装的人升级
python3 scripts/strings.py # 查缺翻译的文案(--write 追加 TODO)
scripts/cli.sh listen ../windows/spike/clips/en.wav --target zh-CN --notes # 真实调用 Ming 和 Ling
scripts/cli.sh roundtrip ~/Library/Application\ Support/LiveSubs/Sessions/*/session.json
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts
index f061d14..01b3de7 100644
--- a/android/app/build.gradle.kts
+++ b/android/app/build.gradle.kts
@@ -5,6 +5,11 @@ plugins { id("com.android.application") }
val appVersion = rootProject.file("../VERSION").readText().trim()
val appVersionCode = appVersion.split(".").map { it.toInt() }.let { (a, b, c) -> a * 10000 + b * 100 + c }
+// Release signing: the key never enters the repo. scripts/keystore.sh makes it once
+// (~/.cicada-android/release.jks, its password in the Keychain); scripts/build.sh and CI hand it
+// over in these variables. Without them the release build stays unsigned: never the debug key.
+val releaseKeystore = System.getenv("CICADA_KEYSTORE")?.let { file(it) }?.takeIf { it.isFile }
+
android {
namespace = "com.wquguru.cicada"
compileSdk = 36
@@ -15,12 +20,23 @@ android {
versionCode = appVersionCode
versionName = appVersion
}
+ signingConfigs {
+ if (releaseKeystore != null) create("release") {
+ storeFile = releaseKeystore
+ // PKCS12 has one password for the store and the key.
+ storePassword = System.getenv("CICADA_KEYSTORE_PASSWORD")
+ keyPassword = System.getenv("CICADA_KEYSTORE_PASSWORD")
+ keyAlias = System.getenv("CICADA_KEY_ALIAS") ?: "cicada"
+ }
+ }
buildTypes {
// The dev build has its own id (so its own records and key), log and the debug receiver
// that scripts/cicadactl.sh drives.
debug { applicationIdSuffix = ".dev"; versionNameSuffix = "-dev" }
- // Unsigned until M3 (release signing): never ship a debug-signed build under the real id.
- release { isMinifyEnabled = false }
+ release {
+ isMinifyEnabled = false
+ if (releaseKeystore != null) signingConfig = signingConfigs.getByName("release")
+ }
}
compileOptions { sourceCompatibility = JavaVersion.VERSION_17; targetCompatibility = JavaVersion.VERSION_17 }
buildFeatures { buildConfig = true }
diff --git a/android/scripts/build.sh b/android/scripts/build.sh
index 3acd520..6306080 100755
--- a/android/scripts/build.sh
+++ b/android/scripts/build.sh
@@ -1,6 +1,7 @@
#!/bin/bash
# Builds the Android app on the Mac: build/Cicada-dev.apk (com.wquguru.cicada.dev, with the debug
-# receiver), or with RELEASE=1 build/Cicada-unsigned.apk (signing is M3). Also regenerates the
+# receiver), or with RELEASE=1 build/Cicada-<version>-Android.apk signed with the release key
+# (scripts/keystore.sh; build/Cicada-unsigned.apk when there is none). Also regenerates the
# onboarding sample from the Mac app's. The launcher icon is vector (res/drawable/ic_launcher_*.xml).
set -euo pipefail
cd "$(dirname "$0")/.."
@@ -9,9 +10,21 @@ root=..
afconvert -f WAVE -d LEI16@16000 -c 1 "$root/Resources/sample/sample.m4a" app/src/main/assets/sample.wav
mkdir -p build
if [ "${RELEASE:-}" = 1 ]; then
- # Unsigned: release signing comes with M3 (docs/android-plan.md).
- scripts/gradle.sh :app:assembleRelease
- cp app/build/outputs/apk/release/app-release-unsigned.apk build/Cicada-unsigned.apk && echo "build/Cicada-unsigned.apk ($(du -h build/Cicada-unsigned.apk | cut -f1))"
+ ks="${CICADA_KEYSTORE:-$HOME/.cicada-android/release.jks}"
+ if [ -f "$ks" ]; then
+ export CICADA_KEYSTORE="$ks"
+ [ -n "${CICADA_KEYSTORE_PASSWORD:-}" ] || CICADA_KEYSTORE_PASSWORD=$(security find-generic-password -s cicada-android-release -w)
+ export CICADA_KEYSTORE_PASSWORD
+ scripts/gradle.sh :app:assembleRelease
+ out="build/Cicada-$(tr -d '[:space:]' < "$root/VERSION")-Android.apk"
+ cp app/build/outputs/apk/release/app-release.apk "$out"
+ scripts/check-release.sh "$out"
+ echo "$out ($(du -h "$out" | cut -f1))"
+ else
+ echo "no release key ($ks): building unsigned; scripts/keystore.sh makes one" >&2
+ scripts/gradle.sh :app:assembleRelease
+ cp app/build/outputs/apk/release/app-release-unsigned.apk build/Cicada-unsigned.apk && echo "build/Cicada-unsigned.apk ($(du -h build/Cicada-unsigned.apk | cut -f1))"
+ fi
else
scripts/gradle.sh :app:assembleDebug
cp app/build/outputs/apk/debug/app-debug.apk build/Cicada-dev.apk && echo "build/Cicada-dev.apk ($(du -h build/Cicada-dev.apk | cut -f1))"
diff --git a/android/scripts/check-release.sh b/android/scripts/check-release.sh
new file mode 100755
index 0000000..6a8ca45
--- /dev/null
+++ b/android/scripts/check-release.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+# Checks a release APK before it ships: it verifies, and it is not signed with a debug key.
+# scripts/check-release.sh build/Cicada-1.1.0-Android.apk
+set -euo pipefail
+apk=${1:?usage: scripts/check-release.sh <apk>}
+sdk="${ANDROID_HOME:-$HOME/.cicada-android/sdk}"
+# apksigner is Java: the toolchain from install-sdk.sh on the Mac, setup-java in CI.
+[ -n "${JAVA_HOME:-}" ] || export JAVA_HOME="$HOME/.cicada-android/jdk/Contents/Home"
+apksigner=$(ls "$sdk"/build-tools/*/apksigner | tail -1)
+certs=$("$apksigner" verify --print-certs "$apk")
+if grep -q "CN=Android Debug" <<< "$certs"; then echo "$apk is signed with a debug key" >&2; exit 1; fi
+grep -m1 "SHA-256 digest" <<< "$certs"
diff --git a/android/scripts/keystore.sh b/android/scripts/keystore.sh
new file mode 100755
index 0000000..1b3ceda
--- /dev/null
+++ b/android/scripts/keystore.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+# Makes the release signing key, once ever: ~/.cicada-android/release.jks (PKCS12, RSA 4096, 30
+# years), its random password in the login Keychain (cicada-android-release). Every release must be
+# signed with this same key, or phones can't update to it: back up the file and the password.
+# --github also puts both in the repo's Actions secrets, for the release workflow.
+set -euo pipefail
+ks="${CICADA_KEYSTORE:-$HOME/.cicada-android/release.jks}"
+keytool="$HOME/.cicada-android/jdk/Contents/Home/bin/keytool"
+if [ -e "$ks" ]; then
+ echo "$ks already exists: never replace it (installed copies could no longer update)" >&2
+else
+ pw=$(openssl rand -hex 24)
+ mkdir -p "$(dirname "$ks")"
+ "$keytool" -genkeypair -keystore "$ks" -storetype PKCS12 -alias cicada -keyalg RSA -keysize 4096 \
+ -validity 10950 -dname "CN=Cicada, O=wquguru" -storepass "$pw" -keypass "$pw" >/dev/null
+ chmod 600 "$ks"
+ security add-generic-password -a "$USER" -s cicada-android-release -w "$pw" -U
+ echo "made $ks; the password is in the Keychain (security find-generic-password -s cicada-android-release -w)"
+fi
+pw=$(security find-generic-password -s cicada-android-release -w)
+"$keytool" -list -v -keystore "$ks" -storepass "$pw" -alias cicada | grep -E "Owner|SHA256:"
+if [ "${1:-}" = --github ]; then
+ base64 -i "$ks" | gh secret set ANDROID_KEYSTORE_BASE64
+ printf %s "$pw" | gh secret set ANDROID_KEYSTORE_PASSWORD
+ echo "set ANDROID_KEYSTORE_BASE64 and ANDROID_KEYSTORE_PASSWORD"
+fi
+echo "back up $ks and its password somewhere safe (a password manager): losing either means a new app id"