Skip to content

ProCraft for Android: An Android native painting app written in Rust

Rust

build: version 0.0.1-beta, the release key from the environment, and a release on each version tag

Joshaba81cd66ae3
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
new file mode 100644
index 0000000..c725435
--- /dev/null
+++ b/.github/workflows/release.yml
@@ -0,0 +1,145 @@
+# Release pipeline: pushing a version tag (v0.0.1-beta, v0.2.0, v1.0.0-rc.1, …) builds the APK for
+# 64-bit ARM tablets signed with the release key and makes it a draft GitHub release named after
+# the tag, with an empty "What's Changed" section. Fill that in on GitHub and publish the draft.
+#
+# A manual run (Actions › Release › Run workflow) is a dry run: it builds the APK at the version in
+# gradle.properties and keeps it as the run's artifact, and releases nothing.
+name: Release
+
+on:
+ push:
+ tags: ['v*']
+ workflow_dispatch:
+
+permissions:
+ contents: read
+
+concurrency:
+ group: release-${{ github.ref }}
+ # never cancel a release halfway; a newer push waits its turn
+ cancel-in-progress: false
+
+env:
+ CARGO_TERM_COLOR: always
+ CARGO_INCREMENTAL: 0
+ RUST_VERSION: "1.99.0"
+ NDK_VERSION: "29.0.14206865"
+
+jobs:
+ apk:
+ name: APK
+ runs-on: ubuntu-24.04
+ steps:
+ - uses: actions/checkout@v4
+
+ - id: v
+ name: Resolve version
+ run: |
+ listed="$(sed -n 's/^procraft\.version=//p' apps/procraft-android/android/gradle.properties)"
+ if [ "$GITHUB_REF_TYPE" = tag ]; then v="${GITHUB_REF_NAME#v}"; else v="$listed"; fi
+ if ! printf '%s' "$v" | grep -Eq '^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$'; then
+ echo "::error::'$v' is not a version like 1.2.3 or 1.2.3-beta"
+ exit 1
+ fi
+ # the tag must be the version gradle.properties names
+ if [ "$v" != "$listed" ]; then
+ echo "::error::the tag is v$v but gradle.properties says procraft.version=$listed: set it (and procraft.versionCode) before tagging"
+ exit 1
+ fi
+ echo "version=$v" >>"$GITHUB_OUTPUT"
+ echo "### v$v (\`${GITHUB_SHA::9}\`)" >>"$GITHUB_STEP_SUMMARY"
+
+ - uses: actions/setup-java@v4
+ with:
+ distribution: temurin
+ java-version: "17"
+
+ - uses: dtolnay/rust-toolchain@master
+ with:
+ toolchain: ${{ env.RUST_VERSION }}
+ targets: aarch64-linux-android
+
+ - uses: Swatinem/rust-cache@v2
+ with:
+ key: release-android
+
+ - name: Install cargo-ndk and the Android NDK
+ run: |
+ cargo install cargo-ndk --version "^4" --locked
+ yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --install "ndk;$NDK_VERSION" >/dev/null
+ echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/$NDK_VERSION" >>"$GITHUB_ENV"
+
+ - name: Release key
+ env:
+ KEYSTORE_BASE64: ${{ secrets.PROCRAFT_KEYSTORE_BASE64 }}
+ run: |
+ if [ -n "$KEYSTORE_BASE64" ]; then
+ printf '%s' "$KEYSTORE_BASE64" | base64 -d >"$RUNNER_TEMP/release.p12"
+ echo "PROCRAFT_KEYSTORE=$RUNNER_TEMP/release.p12" >>"$GITHUB_ENV"
+ elif [ "$GITHUB_REF_TYPE" = tag ]; then
+ echo "::error::No release key: a release is never signed with the debug key"
+ exit 1
+ else
+ echo "::warning::No release key: this dry run is signed with the debug key"
+ fi
+
+ - uses: gradle/actions/setup-gradle@v4
+
+ - name: Build
+ working-directory: apps/procraft-android/android
+ env:
+ VERSION: ${{ steps.v.outputs.version }}
+ PROCRAFT_KEYSTORE_PASSWORD: ${{ secrets.PROCRAFT_KEYSTORE_PASSWORD }}
+ PROCRAFT_KEY_ALIAS: ${{ secrets.PROCRAFT_KEY_ALIAS }}
+ PROCRAFT_KEY_PASSWORD: ${{ secrets.PROCRAFT_KEY_PASSWORD }}
+ run: |
+ mkdir -p "$GITHUB_WORKSPACE/dist"
+ ./gradlew --no-daemon :app:assembleRelease -Pprocraft.abis=arm64-v8a "-Pprocraft.version=$VERSION"
+ cp app/build/outputs/apk/release/app-release.apk "$GITHUB_WORKSPACE/dist/procraft-android-$VERSION.apk"
+
+ - name: APK
+ run: |
+ apksigner="$(ls -d "$ANDROID_HOME"/build-tools/*/ | sort -V | tail -n 1)apksigner"
+ echo >>"$GITHUB_STEP_SUMMARY"
+ for apk in dist/*.apk; do
+ certs="$("$apksigner" verify --print-certs "$apk")"
+ if [ "$GITHUB_REF_TYPE" = tag ] && grep -q "CN=Android Debug" <<<"$certs"; then
+ echo "::error::$apk is signed with the debug key"
+ exit 1
+ fi
+ {
+ echo " $(basename "$apk")"
+ grep -E "certificate (DN|SHA-256)" <<<"$certs" | sed 's/^/ /'
+ } >>"$GITHUB_STEP_SUMMARY"
+ done
+
+ - uses: actions/upload-artifact@v4
+ with:
+ name: release-apk
+ path: dist/*
+ if-no-files-found: error
+
+ - name: Remove the release key
+ if: always()
+ run: rm -f "$RUNNER_TEMP/release.p12"
+
+ release:
+ name: GitHub Release
+ needs: apk
+ if: github.ref_type == 'tag'
+ runs-on: ubuntu-24.04
+ permissions:
+ contents: write
+ env:
+ GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ GH_REPO: ${{ github.repository }}
+ steps:
+ - uses: actions/download-artifact@v4
+ with:
+ name: release-apk
+ path: dist
+
+ - name: Draft the release
+ run: |
+ gh release create "$GITHUB_REF_NAME" --draft --verify-tag --title "$GITHUB_REF_NAME" --notes "## What's Changed" -- dist/*
+ echo "Draft **$GITHUB_REF_NAME** is ready: fill in What's Changed and publish it." >>"$GITHUB_STEP_SUMMARY"
diff --git a/apps/procraft-android/android/app/build.gradle.kts b/apps/procraft-android/android/app/build.gradle.kts
index 92e1527..ba9a6e1 100644
--- a/apps/procraft-android/android/app/build.gradle.kts
+++ b/apps/procraft-android/android/app/build.gradle.kts
@@ -13,6 +13,41 @@ val rustAbis: List<String> =
providers.gradleProperty("procraft.abis").getOrElse("arm64-v8a,x86_64").split(',').map { it.trim() }.filter { it.isNotEmpty() }
val minApi = 29
+// The app's version: gradle.properties' procraft.version, or the version tag's when the release
+// workflow builds a release. The version code follows from it (0.0.1-beta → 101, 0.0.1-beta.2 →
+// 102, 0.0.1 → 199, 0.1.0-beta → 10001), so a pre-release comes before its release and every
+// release before the next version's pre-releases; times ten plus the ABI of a one-ABI build
+// (arm64-v8a 2, x86_64 3; 0 for several), so each APK of a release has its own code.
+val appVersion: String = providers.gradleProperty("procraft.version").get()
+val appVersionCode: Int = run {
+ val m = requireNotNull(Regex("""(\d+)\.(\d+)\.(\d+)(-[0-9A-Za-z-]+?(\.(\d+))?)?""").matchEntire(appVersion)) {
+ "procraft.version $appVersion is not a version like 1.2.3, 1.2.3-beta or 1.2.3-beta.4"
+ }
+ val (major, minor, patch) = m.groupValues.subList(1, 4).map { it.toInt() }
+ val pre = if (m.groupValues[4].isEmpty()) null else m.groupValues[6].toIntOrNull() ?: 1
+ require(minor < 100 && patch < 100 && (pre ?: 1) in 1..98) {
+ "procraft.version $appVersion: minor and patch must be under 100, and a pre-release's number 1 to 98"
+ }
+ val base = ((major * 100 + minor) * 100 + patch) * 100 + (pre ?: 99)
+ providers.gradleProperty("procraft.versionCode").orNull?.let { listed ->
+ require(listed.trim().toIntOrNull() == base) {
+ "gradle.properties: procraft.versionCode=$listed, but version $appVersion's is $base (set both before tagging)"
+ }
+ }
+ val abi = when (rustAbis.singleOrNull()) {
+ "arm64-v8a" -> 2
+ "x86_64" -> 3
+ else -> 0
+ }
+ base * 10 + abi
+}
+
+// The release key, when the build is given one (the release workflow passes it from the
+// repository's secrets). Without one a release build is signed with the debug key: it installs,
+// but it can't update a published release; or, with -Pprocraft.unsigned=true, not signed at all.
+val releaseKeystore: String? = providers.environmentVariable("PROCRAFT_KEYSTORE").orNull
+val unsignedRelease: Boolean = providers.gradleProperty("procraft.unsigned").orNull == "true"
+
/** rustup's `cargo` (Android Studio may not see rustup's PATH), else `cargo` from PATH; `CARGO` overrides. */
val cargoExecutable: String = run {
val exe = if (System.getProperty("os.name").lowercase().startsWith("windows")) "cargo.exe" else "cargo"
@@ -86,11 +121,22 @@ android {
applicationId = "io.github.artcraftandroid.procraft"
minSdk = minApi
targetSdk = 36
- versionCode = providers.gradleProperty("procraft.versionCode").get().toInt()
- versionName = providers.gradleProperty("procraft.version").get()
+ versionCode = appVersionCode
+ versionName = appVersion
ndk { abiFilters.addAll(rustAbis) }
}
+ signingConfigs {
+ if (releaseKeystore != null) {
+ create("release") {
+ storeFile = file(releaseKeystore)
+ storePassword = providers.environmentVariable("PROCRAFT_KEYSTORE_PASSWORD").orNull
+ keyAlias = providers.environmentVariable("PROCRAFT_KEY_ALIAS").orNull
+ keyPassword = providers.environmentVariable("PROCRAFT_KEY_PASSWORD").orNull
+ }
+ }
+ }
+
buildTypes {
debug {
applicationIdSuffix = ".debug"
@@ -99,8 +145,7 @@ android {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
- // signed with the debug key until there is a release key
- signingConfig = signingConfigs.getByName("debug")
+ signingConfig = if (unsignedRelease) null else signingConfigs.findByName("release") ?: signingConfigs.getByName("debug")
vcsInfo { include = false }
}
}
diff --git a/apps/procraft-android/android/gradle.properties b/apps/procraft-android/android/gradle.properties
index 2e9a635..657eef5 100644
--- a/apps/procraft-android/android/gradle.properties
+++ b/apps/procraft-android/android/gradle.properties
@@ -10,5 +10,7 @@ kotlin.code.style=official
# e.g. `./gradlew assembleRelease -Pprocraft.abis=arm64-v8a`.
procraft.abis=arm64-v8a,x86_64
-procraft.version=0.0.1
-procraft.versionCode=1
+# The app's version and its version code (before the ABI digit: see app/build.gradle.kts, the build
+# checks the two agree). A release tag v<version> must name this version.
+procraft.version=0.0.1-beta
+procraft.versionCode=101