kepr-build-apk.sh

5.5 KB · raw

1#!/bin/sh2# Build and publish the signed release APK for Kepr Builds.3#4# The release keystore is read from local.properties (koh-ignored). The script5# checks that configuration before starting Gradle, then validates the produced6# APK's package, version, APK Signature Scheme, and pinned release certificate7# before copying anything to the Kepr staging directory.8set -eu910JAVA_HOME="${JAVA_HOME:-/Applications/Android Studio.app/Contents/jbr/Contents/Home}"11export JAVA_HOME1213ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)14cd "$ROOT_DIR"1516# Keep the tool-selection and public metadata gates shared with the hermetic17# negative-path test; this script owns signing/build orchestration only.18# shellcheck source=scripts/kepr-build-helpers.sh19. "$ROOT_DIR/scripts/kepr-build-helpers.sh"2021PROPERTIES_FILE="$ROOT_DIR/local.properties"22if [ ! -r "$PROPERTIES_FILE" ]; then23    echo "refusing to publish: local.properties is missing or unreadable" >&224    exit 125fi2627property() {28    # Values are inspected only for presence/path checks; never print this file29    # because it contains the release passwords.30    sed -n "s/^$1=//p" "$PROPERTIES_FILE" | sed -n '1p'31}3233STORE_FILE_PROPERTY=$(property peachStoreFile)34STORE_PASSWORD=$(property peachStorePassword)35KEY_ALIAS=$(property peachKeyAlias)36KEY_PASSWORD=$(property peachKeyPassword)3738if [ -z "$STORE_FILE_PROPERTY" ] || [ -z "$STORE_PASSWORD" ] ||39    [ -z "$KEY_ALIAS" ] || [ -z "$KEY_PASSWORD" ]; then40    echo "refusing to publish: incomplete release signing configuration" >&241    exit 142fi4344case "$STORE_FILE_PROPERTY" in45    /*) STORE_FILE="$STORE_FILE_PROPERTY" ;;46    *) STORE_FILE="$ROOT_DIR/$STORE_FILE_PROPERTY" ;;47esac48if [ ! -f "$STORE_FILE" ] || [ ! -r "$STORE_FILE" ]; then49    echo "refusing to publish: configured release keystore is missing or unreadable" >&250    exit 151fi5253# Resolve the SDK from the environment first, then the same local.properties54# Gradle uses. Pick the newest *versioned* build-tools directory containing both55# tools; build-tools/latest is intentionally never trusted or consulted.56SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}}"57if [ -z "$SDK_ROOT" ]; then58    SDK_ROOT=$(property sdk.dir)59fi60if [ -z "$SDK_ROOT" ] || [ ! -d "$SDK_ROOT/build-tools" ]; then61    echo "refusing to publish: Android SDK build-tools directory not found" >&262    exit 163fi6465BUILD_TOOLS_DIR=$(find_stable_build_tools_dir "$SDK_ROOT" || true)66if [ -z "$BUILD_TOOLS_DIR" ]; then67    echo "refusing to publish: no versioned build-tools directory has apksigner and aapt2" >&268    exit 169fi70APKSIGNER="$BUILD_TOOLS_DIR/apksigner"71AAPT2="$BUILD_TOOLS_DIR/aapt2"7273ROOT_VERSION=$(read_root_semver "$ROOT_DIR/VERSION" || true)74if [ -z "$ROOT_VERSION" ]; then75    echo "refusing to publish: VERSION is missing or not strict semantic version" >&276    exit 177fi7879"$ROOT_DIR/gradlew" --no-configuration-cache :app:assembleRelease8081case "${KEPR_TARGET:-}" in82    android-arm64)83        APK_NAME="app-arm64-v8a-release.apk"84        EXPECTED_ABI="arm64-v8a"85        ;;86    android-x86)87        APK_NAME="app-x86_64-release.apk"88        EXPECTED_ABI="x86_64"89        ;;90    *)91        echo "refusing to publish: unsupported KEPR_TARGET '${KEPR_TARGET:-unset}'" >&292        exit 193        ;;94esac9596APK="$ROOT_DIR/app/build/outputs/apk/release/$APK_NAME"97METADATA="$ROOT_DIR/app/build/outputs/apk/release/output-metadata.json"98if [ ! -f "$APK" ] || [ ! -r "$APK" ]; then99    echo "refusing to publish: release APK was not produced" >&2100    exit 1101fi102if [ ! -f "$METADATA" ] || [ ! -r "$METADATA" ]; then103    echo "refusing to publish: Gradle output metadata was not produced" >&2104    exit 1105fi106107BADGING_FILE=$(mktemp "${TMPDIR:-/tmp}/peach-apk-badging.XXXXXX")108cleanup_badging() {109    [ -e "$BADGING_FILE" ] && trash "$BADGING_FILE"110}111trap cleanup_badging EXIT HUP INT TERM112"$AAPT2" dump badging "$APK" > "$BADGING_FILE"113if ! validate_release_metadata \114    "$METADATA" \115    "$BADGING_FILE" \116    "$ROOT_VERSION" \117    "com.peachpasswords" \118    "$APK_NAME" \119    "$EXPECTED_ABI"; then120    exit 1121fi122123# apksigner's plain verify accepts v1-only APKs, so require an explicit v2/v3/v4124# success line before publication. Capture output in memory; it contains only125# public certificate metadata and is never written with signing secrets.126SIGNING_OUTPUT=$("$APKSIGNER" verify --verbose --print-certs "$APK" 2>/dev/null) || {127    echo "refusing to publish: apksigner verification failed" >&2128    exit 1129}130if ! printf '%s\n' "$SIGNING_OUTPUT" | grep -Eq 'Verified using v[234] scheme.*: true'; then131    echo "refusing to publish: APK is not signed with APK Signature Scheme v2 or newer" >&2132    exit 1133fi134135PINNED_RELEASE_CERT_SHA256="e10b975178f0bee7fb5a4802519da70ec2988b6ef7d8c9401fbbcff5a1137668"136CERT_DIGEST_COUNT=$(printf '%s\n' "$SIGNING_OUTPUT" | grep -c 'certificate SHA-256 digest:' || true)137if [ "$CERT_DIGEST_COUNT" -ne 1 ]; then138    echo "refusing to publish: release APK must have exactly one signer certificate" >&2139    exit 1140fi141CERT_DIGEST=$(printf '%s\n' "$SIGNING_OUTPUT" |142    sed -n 's/.*certificate SHA-256 digest:[[:space:]]*//p' |143    sed -n '1p' | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]')144if [ "$CERT_DIGEST" != "$PINNED_RELEASE_CERT_SHA256" ]; then145    echo "refusing to publish: APK signer certificate does not match the pinned Peach release digest" >&2146    exit 1147fi148149# Only a fully verified artifact reaches the Kepr staging directory.150mkdir -p "$ROOT_DIR/build/kepr"151cp "$APK" "$ROOT_DIR/build/kepr/peach-android.apk"152echo "release APK verified and staged: build/kepr/peach-android.apk"