kepr-build-apk.sh

5.4 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 -eu78# PB-26-008: tool paths are discovered from the environment first, then a9# Homebrew default, then fail closed. No silent fallback to an unknown path.10JAVA_HOME="${PEACH_JAVA_HOME:-/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home}"11if [ ! -d "$JAVA_HOME" ]; then12    echo "refusing to publish: Java 17 runtime not found at $JAVA_HOME (set PEACH_JAVA_HOME)" >&213    exit 114fi15export JAVA_HOME1617ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)18cd "$ROOT_DIR"1920# Keep the tool-selection and public metadata gates shared with the hermetic21# negative-path test; this script owns build orchestration only.22# shellcheck source=scripts/kepr-build-helpers.sh23. "$ROOT_DIR/scripts/kepr-build-helpers.sh"2425# Resolve the SDK from the environment first, then the non-secret sdk.dir in26# local.properties. Pick the newest *versioned* build-tools directory containing both27# tools; build-tools/latest is intentionally never trusted or consulted.28SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}}"29if [ -z "$SDK_ROOT" ]; then30    SDK_ROOT=$(sed -n 's/^sdk\.dir=//p' "$ROOT_DIR/local.properties" 2>/dev/null | sed -n '1p')31fi32if [ -z "$SDK_ROOT" ] || [ ! -d "$SDK_ROOT/build-tools" ]; then33    echo "refusing to publish: Android SDK build-tools directory not found" >&234    exit 135fi3637BUILD_TOOLS_DIR=$(find_stable_build_tools_dir "$SDK_ROOT" || true)38if [ -z "$BUILD_TOOLS_DIR" ]; then39    echo "refusing to publish: no versioned build-tools directory has aapt2" >&240    exit 141fi42AAPT2="$BUILD_TOOLS_DIR/aapt2"4344ROOT_VERSION=$(read_root_semver "$ROOT_DIR/VERSION" || true)45if [ -z "$ROOT_VERSION" ]; then46    echo "refusing to publish: VERSION is missing or not strict semantic version" >&247    exit 148fi4950"$ROOT_DIR/gradlew" --no-configuration-cache :app:assembleRelease5152# PB-26-113: the merged release manifest is the shipped permission and exported53# component surface. Drift from the checked-in inventory refuses the publish54# rather than reaching a store unnoticed.55python3 "$ROOT_DIR/scripts/check-release-manifest.py" check ||56    {57        echo "refusing to publish: release manifest inventory drift" >&258        exit 159    }6061case "${KEPR_TARGET:-}" in62    android-arm64)63        APK_NAME="app-arm64-v8a-release.apk"64        EXPECTED_ABI="arm64-v8a"65        ;;66    android-x86)67        APK_NAME="app-x86_64-release.apk"68        EXPECTED_ABI="x86_64"69        ;;70    *)71        echo "refusing to publish: unsupported KEPR_TARGET '${KEPR_TARGET:-unset}'" >&272        exit 173        ;;74esac7576APK="$ROOT_DIR/app/build/outputs/apk/release/$APK_NAME"77METADATA="$ROOT_DIR/app/build/outputs/apk/release/output-metadata.json"78if [ ! -f "$APK" ] || [ ! -r "$APK" ]; then79    echo "refusing to publish: release APK was not produced" >&280    exit 181fi82if [ ! -f "$METADATA" ] || [ ! -r "$METADATA" ]; then83    echo "refusing to publish: Gradle output metadata was not produced" >&284    exit 185fi8687# PB-26-123: the inventory is an allow-list of native libraries. It is checked88# against the source set AND against the packaged APK, so a library that89# arrived only through a dependency is refused too.90python3 "$ROOT_DIR/scripts/check-native-provenance.py" "$APK" ||91    {92        echo "refusing to publish: native provenance drift" >&293        exit 194    }9596BADGING_FILE=$(mktemp "${TMPDIR:-/tmp}/peach-apk-badging.XXXXXX")97cleanup_badging() {98    [ -e "$BADGING_FILE" ] && trash "$BADGING_FILE"99}100trap cleanup_badging EXIT HUP INT TERM101"$AAPT2" dump badging "$APK" > "$BADGING_FILE"102if ! validate_release_metadata \103    "$METADATA" \104    "$BADGING_FILE" \105    "$ROOT_VERSION" \106    "com.peachpasswords" \107    "$APK_NAME" \108    "$EXPECTED_ABI"; then109    exit 1110fi111112# PB-26-001: passwords are no longer in local.properties. Source ~/.secrets113# for the env vars the Gradle signing config also reads, then read the114# non-secret keystore path and alias from local.properties as before.115if [ -f "$HOME/.secrets" ]; then116    . "$HOME/.secrets"117fi118RELEASE_KEYSTORE=$(sed -n 's/^peachStoreFile=//p' "$ROOT_DIR/local.properties" | sed -n '1p')119RELEASE_ALIAS=$(sed -n 's/^peachKeyAlias=//p' "$ROOT_DIR/local.properties" | sed -n '1p')120RELEASE_STOREPASS="${PEACH_STORE_PASSWORD:-}"121if [ -z "$RELEASE_STOREPASS" ]; then122    echo "refusing to publish: PEACH_STORE_PASSWORD is not set (source ~/.secrets)" >&2123    exit 1124fi125if [ -z "$RELEASE_KEYSTORE" ] || [ -z "$RELEASE_ALIAS" ]; then126    echo "refusing to publish: peachStoreFile or peachKeyAlias missing from local.properties" >&2127    exit 1128fi129RELEASE_CERTIFICATE=$(release_certificate_sha256 "$RELEASE_KEYSTORE" "$RELEASE_STOREPASS" "$RELEASE_ALIAS")130if ! assert_established_release_certificate "$RELEASE_CERTIFICATE"; then131    exit 1132fi133if ! assert_release_signer_matches \134    "$RELEASE_CERTIFICATE" \135    "$(apk_signer_sha256 "$BUILD_TOOLS_DIR/apksigner" "$APK")" \136    "$APK_NAME"; then137    exit 1138fi139140# Only a fully verified artifact reaches the Kepr staging directory. The141# basename includes SemVer + ABI so Kepr's Content-Disposition carries both142# to the browser, matching the {version}/{platform} tokens in kepr.build.143mkdir -p "$ROOT_DIR/build/kepr"144STAGED_NAME="peach-android-${ROOT_VERSION}-${KEPR_TARGET}.apk"145cp "$APK" "$ROOT_DIR/build/kepr/$STAGED_NAME"146echo "release APK verified and staged: build/kepr/$STAGED_NAME"