kepr-build-apk.sh

4.7 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    }58python3 "$ROOT_DIR/scripts/check-native-provenance.py" ||59    {60        echo "refusing to publish: native provenance drift" >&261        exit 162    }6364case "${KEPR_TARGET:-}" in65    android-arm64)66        APK_NAME="app-arm64-v8a-release.apk"67        EXPECTED_ABI="arm64-v8a"68        ;;69    android-x86)70        APK_NAME="app-x86_64-release.apk"71        EXPECTED_ABI="x86_64"72        ;;73    *)74        echo "refusing to publish: unsupported KEPR_TARGET '${KEPR_TARGET:-unset}'" >&275        exit 176        ;;77esac7879APK="$ROOT_DIR/app/build/outputs/apk/release/$APK_NAME"80METADATA="$ROOT_DIR/app/build/outputs/apk/release/output-metadata.json"81if [ ! -f "$APK" ] || [ ! -r "$APK" ]; then82    echo "refusing to publish: release APK was not produced" >&283    exit 184fi85if [ ! -f "$METADATA" ] || [ ! -r "$METADATA" ]; then86    echo "refusing to publish: Gradle output metadata was not produced" >&287    exit 188fi8990BADGING_FILE=$(mktemp "${TMPDIR:-/tmp}/peach-apk-badging.XXXXXX")91cleanup_badging() {92    [ -e "$BADGING_FILE" ] && trash "$BADGING_FILE"93}94trap cleanup_badging EXIT HUP INT TERM95"$AAPT2" dump badging "$APK" > "$BADGING_FILE"96if ! validate_release_metadata \97    "$METADATA" \98    "$BADGING_FILE" \99    "$ROOT_VERSION" \100    "com.peachpasswords" \101    "$APK_NAME" \102    "$EXPECTED_ABI"; then103    exit 1104fi105106# PB-26-132: this script claimed to validate the APK's signing identity while107# aapt2 badging reports no signature at all. Compare the APK's signer108# certificate against the configured release keystore's certificate — a build109# environment that produced an artifact signed with a different key is exactly110# the drift that used to reach staging unnoticed.111RELEASE_KEYSTORE=$(sed -n 's/^peachStoreFile=//p' "$ROOT_DIR/local.properties" | sed -n '1p')112RELEASE_STOREPASS=$(sed -n 's/^peachStorePassword=//p' "$ROOT_DIR/local.properties" | sed -n '1p')113RELEASE_ALIAS=$(sed -n 's/^peachKeyAlias=//p' "$ROOT_DIR/local.properties" | sed -n '1p')114if ! assert_release_signer_matches \115    "$(release_certificate_sha256 "$RELEASE_KEYSTORE" "$RELEASE_STOREPASS" "$RELEASE_ALIAS")" \116    "$(apk_signer_sha256 "$BUILD_TOOLS_DIR/apksigner" "$APK")" \117    "$APK_NAME"; then118    exit 1119fi120121# Only a fully verified artifact reaches the Kepr staging directory. The122# basename includes SemVer + ABI so Kepr's Content-Disposition carries both123# to the browser, matching the {version}/{platform} tokens in kepr.build.124mkdir -p "$ROOT_DIR/build/kepr"125STAGED_NAME="peach-android-${ROOT_VERSION}-${KEPR_TARGET}.apk"126cp "$APK" "$ROOT_DIR/build/kepr/$STAGED_NAME"127echo "release APK verified and staged: build/kepr/$STAGED_NAME"