#!/usr/bin/env bash # A cold launch timed on an x86_64 emulator as Play's build runs (23 Nov 2026). # # bash scripts/android-launch-time.sh [--serial emulator-5602] [--runs 5] [--seed ] # # is a registry slug built from apps/android (story, days, notes, messages). is an x86_64 # cotton-android-jni at the `android` profile (`cargo ndk -t x86_64 build --profile android -p cotton-android-jni`, # ~35 minutes on the box) or `android-debug`. The APK is the R8 check's (-Pcotton.r8Check=true): shrunk by R8 as # Play's, not debuggable, unsigned; here it is signed with this machine's debug key, installed on the emulator and # compiled from its profile as an install from Play is. Never a cut: the R8 check refuses a bundle. # # A debug APK is no measure of a launch: debuggable, it runs its 45 MB of dex in the interpreter and verifies it on # every start (two thirds of a debug launch's CPU on the emulator), whatever it is asked to compile. # # Each run prints the `cotton-launch` marks (LaunchMarks: ms since the process began; each core method's first # `call` and `answered`), then the run's first content: Story story.scenes (story.current with no script), Days # days.span, Words notes.list, Comms chat.list. `--seed ` puts an app data folder (`no_backup/cotton`) in # place first, as root (the emulator's `adb root`); a folder another device made may be refused as served elsewhere. set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" app="${1:?app slug}"; core="${2:?x86_64 core .so}"; shift 2 serial="emulator-5602"; runs=5; seed="" while [ $# -gt 0 ]; do case "$1" in --serial) serial="$2"; shift 2 ;; --runs) runs="$2"; shift 2 ;; --seed) seed="$2"; shift 2 ;; *) echo "android-launch-time: unknown $1" >&2; exit 2 ;; esac done A=(adb -s "$serial") tools="$(ls -d "${ANDROID_HOME:-/opt/android-sdk}"/build-tools/* | sort -V | tail -1)" flavour="$(tr '[:lower:]' '[:upper:]' <<< "${app:0:1}")${app:1}" project="$ROOT/apps/android" jni="$project/app/src/debug/jniLibs/x86_64" work="$(mktemp -d "${TMPDIR:-/tmp}/launch-time-XXXXXX")" cleanup() { rm -f "$jni/libcotton_android_jni.so"; rmdir "$jni" 2>/dev/null || true; rm -rf "$work"; } trap cleanup EXIT [ ! -e "$jni/libcotton_android_jni.so" ] || { echo "android-launch-time: $jni already holds a core; move it aside first" >&2; exit 1; } mkdir -p "$jni" && cp "$core" "$jni/libcotton_android_jni.so" (cd "$project" && ./gradlew -q -Pcotton.r8Check=true -Pcotton.walkAbi=x86_64 ":app:assemble${flavour}DevRelease") unsigned="$(ls "$project/app/build/outputs/apk/${app}Dev/release/"*.apk | head -1)" keystore="$HOME/.android/debug.keystore" [ -f "$keystore" ] || keytool -genkeypair -keystore "$keystore" -storepass android -alias androiddebugkey -keypass android \ -dname "CN=Android Debug,O=Android,C=US" -keyalg RSA -validity 10000 >/dev/null 2>&1 "$tools/apksigner" sign --ks "$keystore" --ks-pass pass:android --out "$work/app.apk" "$unsigned" package="$("$tools/aapt" dump badging "$work/app.apk" | sed -n "s/^package: name='\([^']*\)'.*/\1/p")" "${A[@]}" uninstall "$package" >/dev/null 2>&1 || true "${A[@]}" install "$work/app.apk" >/dev/null "${A[@]}" shell cmd package compile -m speed-profile -f "$package" >/dev/null activity="$("${A[@]}" shell cmd package resolve-activity --brief "$package" | tail -1)" # One launch first, so the app has made its folders. "${A[@]}" shell am start -W -n "$activity" >/dev/null COTTON_ALLOW_POLL_LOOP=1 sleep 10 "${A[@]}" shell am force-stop "$package" if [ -n "$seed" ]; then "${A[@]}" root >/dev/null; "${A[@]}" wait-for-device tar -C "$seed" -cf "$work/seed.tar" . && "${A[@]}" push "$work/seed.tar" /data/local/tmp/seed.tar >/dev/null "${A[@]}" shell "d=/data/data/$package/no_backup; o=\$(stat -c %u:%g /data/data/$package); rm -rf \$d/cotton && mkdir -p \$d/cotton && tar -xf /data/local/tmp/seed.tar -C \$d/cotton && chown -R \$o \$d && restorecon -R \$d" fi case "$app" in story) content="answered story.scenes|answered story.current" ;; days) content="answered days.span" ;; notes) content="answered notes.list" ;; messages) content="answered chat.list" ;; *) content="gate-open" ;; esac for run in $(seq 1 "$runs"); do "${A[@]}" shell am force-stop "$package"; "${A[@]}" logcat -c frame="$("${A[@]}" shell am start -W -n "$activity" | sed -n 's/TotalTime: //p' | tr -d '\r')" COTTON_ALLOW_POLL_LOOP=1 sleep 14 marks="$("${A[@]}" logcat -d -s cotton-launch:I | sed -n 's/.*cotton-launch: //p' | tr -d '\r')" first="$(grep -E "^($content) [0-9]+$" <<< "$marks" | head -1 | awk '{print $NF}')" echo "$app run $run: first frame ${frame:-?} ms, first content ${first:-?} ms (load $(cut -d' ' -f1 /proc/loadavg))" tr '\n' ';' <<< "$marks"; echo done