kepr-build-apk.sh

3.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    }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 183fi8485BADGING_FILE=$(mktemp "${TMPDIR:-/tmp}/peach-apk-badging.XXXXXX")86cleanup_badging() {87    [ -e "$BADGING_FILE" ] && trash "$BADGING_FILE"88}89trap cleanup_badging EXIT HUP INT TERM90"$AAPT2" dump badging "$APK" > "$BADGING_FILE"91if ! validate_release_metadata \92    "$METADATA" \93    "$BADGING_FILE" \94    "$ROOT_VERSION" \95    "com.peachpasswords" \96    "$APK_NAME" \97    "$EXPECTED_ABI"; then98    exit 199fi100101# Only a fully verified artifact reaches the Kepr staging directory. The102# basename includes SemVer + ABI so Kepr's Content-Disposition carries both103# to the browser, matching the {version}/{platform} tokens in kepr.build.104mkdir -p "$ROOT_DIR/build/kepr"105STAGED_NAME="peach-android-${ROOT_VERSION}-${KEPR_TARGET}.apk"106cp "$APK" "$ROOT_DIR/build/kepr/$STAGED_NAME"107echo "release APK verified and staged: build/kepr/$STAGED_NAME"