git.iko.soy/opencode-android
on 15d51d8 help sign in
95 lines 3.2 kB history raw
1#!/usr/bin/env bash
2#
3# Build the debug APK, make sure an emulator is running, then install and launch
4# the app on it.
5#
6# Usage:
7# scripts/run.sh # build + install + launch on an emulator
8# scripts/run.sh -a Pixel_7 # boot a specific AVD if none is running
9# scripts/run.sh --no-build # skip the Gradle build, just install + launch
10# scripts/run.sh --release # build/install the release variant
12set -euo pipefail
14APP_ID="soy.iko.opencode"
15ACTIVITY=".MainActivity"
16VARIANT="debug"
17DO_BUILD=1
18AVD=""
20while [[ $# -gt 0 ]]; do
21 case "$1" in
22 -a|--avd) AVD="$2"; shift 2 ;;
23 --no-build) DO_BUILD=0; shift ;;
24 --release) VARIANT="release"; shift ;;
25 -h|--help)
26 awk 'NR>1 && /^#/ {sub(/^# ?/,""); print; next} NR>1 {exit}' "$0"
27 exit 0 ;;
28 *) echo "Unknown argument: $1" >&2; exit 2 ;;
29 esac
30done
32cd "$(dirname "$0")/.."
34# --- locate the Android SDK tools ------------------------------------------
35SDK="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}}"
36find_tool() {
37 local name="$1"
38 if command -v "$name" >/dev/null 2>&1; then command -v "$name"; return; fi
39 for d in "$SDK/platform-tools" "$SDK/emulator" "$SDK/cmdline-tools/latest/bin"; do
40 [[ -x "$d/$name" ]] && { echo "$d/$name"; return; }
41 done
42 echo ""
44ADB="$(find_tool adb)"
45EMULATOR="$(find_tool emulator)"
46[[ -z "$ADB" ]] && { echo "error: 'adb' not found; set ANDROID_HOME / ANDROID_SDK_ROOT" >&2; exit 1; }
48# --- ensure an emulator/device is connected --------------------------------
49device_ready() {
50 "$ADB" devices | awk 'NR>1 && $2=="device" {found=1} END {exit found?0:1}'
53if ! device_ready; then
54 echo ">> No running device/emulator; booting one..."
55 [[ -z "$EMULATOR" ]] && { echo "error: 'emulator' not found; start a device manually" >&2; exit 1; }
57 if [[ -z "$AVD" ]]; then
58 AVD="$("$EMULATOR" -list-avds | head -n1)"
59 [[ -z "$AVD" ]] && { echo "error: no AVDs found; create one in Android Studio or pass -a <name>" >&2; exit 1; }
60 fi
61 echo ">> Starting AVD: $AVD"
62 "$EMULATOR" -avd "$AVD" -netdelay none -netspeed full >/dev/null 2>&1 &
64 echo ">> Waiting for device..."
65 "$ADB" wait-for-device
66 # Wait until the OS has finished booting.
67 until [[ "$("$ADB" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')" == "1" ]]; do
68 sleep 2
69 done
70 echo ">> Emulator booted."
71fi
73# --- build ------------------------------------------------------------------
74if [[ "$VARIANT" == "release" ]]; then
75 GRADLE_TASK="assembleRelease"
76 APK="app/build/outputs/apk/release/app-release.apk"
77else
78 GRADLE_TASK="assembleDebug"
79 APK="app/build/outputs/apk/debug/app-debug.apk"
80fi
82if [[ "$DO_BUILD" -eq 1 ]]; then
83 echo ">> Building ($GRADLE_TASK)..."
84 ./gradlew "$GRADLE_TASK"
85fi
86[[ -f "$APK" ]] || { echo "error: APK not found at $APK (did the build run?)" >&2; exit 1; }
88# --- install + launch -------------------------------------------------------
89echo ">> Installing $APK..."
90"$ADB" install -r "$APK"
92echo ">> Launching $APP_ID/$ACTIVITY..."
93"$ADB" shell am start -n "$APP_ID/$ACTIVITY"
95echo ">> Done."