kepr-build-apk.sh

4.9 KB · raw

1#!/bin/sh2# Build and publish the release APK for Kepr Builds.3#4# Kepr Beta builds use the established release certificate. Validate the produced5# APK's package, version, ABI, and signing identity before staging it.6set -eu78JAVA_HOME="/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home"9if [ ! -d "$JAVA_HOME" ]; then10    echo "refusing to publish: required Java 17 runtime not found at $JAVA_HOME" >&211    exit 112fi13export JAVA_HOME1415ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)16cd "$ROOT_DIR"1718# Keep the tool-selection and public metadata gates shared with the hermetic19# negative-path test; this script owns build orchestration only.20# shellcheck source=scripts/kepr-build-helpers.sh21. "$ROOT_DIR/scripts/kepr-build-helpers.sh"2223# Resolve the SDK from the environment first, then the non-secret sdk.dir in24# local.properties. Pick the newest *versioned* build-tools directory containing both25# tools; build-tools/latest is intentionally never trusted or consulted.26SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}}"27if [ -z "$SDK_ROOT" ]; then28    SDK_ROOT=$(sed -n 's/^sdk\.dir=//p' "$ROOT_DIR/local.properties" 2>/dev/null | sed -n '1p')29fi30if [ -z "$SDK_ROOT" ] || [ ! -d "$SDK_ROOT/build-tools" ]; then31    echo "refusing to publish: Android SDK build-tools directory not found" >&232    exit 133fi3435BUILD_TOOLS_DIR=$(find_stable_build_tools_dir "$SDK_ROOT" || true)36if [ -z "$BUILD_TOOLS_DIR" ]; then37    echo "refusing to publish: no versioned build-tools directory has aapt2" >&238    exit 139fi40AAPT2="$BUILD_TOOLS_DIR/aapt2"4142ROOT_VERSION=$(read_root_semver "$ROOT_DIR/VERSION" || true)43if [ -z "$ROOT_VERSION" ]; then44    echo "refusing to publish: VERSION is missing or not strict semantic version" >&245    exit 146fi4748"$ROOT_DIR/gradlew" --no-configuration-cache :app:assembleRelease4950# PB-26-113: the merged release manifest is the shipped permission and exported51# component surface. Drift from the checked-in inventory refuses the publish52# rather than reaching a store unnoticed.53python3 "$ROOT_DIR/scripts/check-release-manifest.py" check ||54    {55        echo "refusing to publish: release manifest inventory drift" >&256        exit 157    }5859case "${KEPR_TARGET:-}" in60    android-arm64)61        APK_NAME="app-arm64-v8a-release.apk"62        EXPECTED_ABI="arm64-v8a"63        ;;64    android-x86)65        APK_NAME="app-x86_64-release.apk"66        EXPECTED_ABI="x86_64"67        ;;68    *)69        echo "refusing to publish: unsupported KEPR_TARGET '${KEPR_TARGET:-unset}'" >&270        exit 171        ;;72esac7374APK="$ROOT_DIR/app/build/outputs/apk/release/$APK_NAME"75METADATA="$ROOT_DIR/app/build/outputs/apk/release/output-metadata.json"76if [ ! -f "$APK" ] || [ ! -r "$APK" ]; then77    echo "refusing to publish: release APK was not produced" >&278    exit 179fi80if [ ! -f "$METADATA" ] || [ ! -r "$METADATA" ]; then81    echo "refusing to publish: Gradle output metadata was not produced" >&282    exit 183fi8485# PB-26-123: the inventory is an allow-list of native libraries. It is checked86# against the source set AND against the packaged APK, so a library that87# arrived only through a dependency is refused too.88python3 "$ROOT_DIR/scripts/check-native-provenance.py" "$APK" ||89    {90        echo "refusing to publish: native provenance drift" >&291        exit 192    }9394BADGING_FILE=$(mktemp "${TMPDIR:-/tmp}/peach-apk-badging.XXXXXX")95cleanup_badging() {96    [ -e "$BADGING_FILE" ] && trash "$BADGING_FILE"97}98trap cleanup_badging EXIT HUP INT TERM99"$AAPT2" dump badging "$APK" > "$BADGING_FILE"100if ! validate_release_metadata \101    "$METADATA" \102    "$BADGING_FILE" \103    "$ROOT_VERSION" \104    "com.peachpasswords" \105    "$APK_NAME" \106    "$EXPECTED_ABI"; then107    exit 1108fi109110# PB-26-132: this script claimed to validate the APK's signing identity while111# aapt2 badging reports no signature at all. Compare the APK's signer112# certificate against the configured release keystore's certificate — a build113# environment that produced an artifact signed with a different key is exactly114# the drift that used to reach staging unnoticed.115RELEASE_KEYSTORE=$(sed -n 's/^peachStoreFile=//p' "$ROOT_DIR/local.properties" | sed -n '1p')116RELEASE_STOREPASS=$(sed -n 's/^peachStorePassword=//p' "$ROOT_DIR/local.properties" | sed -n '1p')117RELEASE_ALIAS=$(sed -n 's/^peachKeyAlias=//p' "$ROOT_DIR/local.properties" | sed -n '1p')118if ! assert_release_signer_matches \119    "$(release_certificate_sha256 "$RELEASE_KEYSTORE" "$RELEASE_STOREPASS" "$RELEASE_ALIAS")" \120    "$(apk_signer_sha256 "$BUILD_TOOLS_DIR/apksigner" "$APK")" \121    "$APK_NAME"; then122    exit 1123fi124125# Only a fully verified artifact reaches the Kepr staging directory. The126# basename includes SemVer + ABI so Kepr's Content-Disposition carries both127# to the browser, matching the {version}/{platform} tokens in kepr.build.128mkdir -p "$ROOT_DIR/build/kepr"129STAGED_NAME="peach-android-${ROOT_VERSION}-${KEPR_TARGET}.apk"130cp "$APK" "$ROOT_DIR/build/kepr/$STAGED_NAME"131echo "release APK verified and staged: build/kepr/$STAGED_NAME"