Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / install
install742 lines · 26.3 KiBRaw
| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 5 | ROOT="$SCRIPT_DIR" |
| 6 | |
| 7 | print_help() { |
| 8 | cat <<'EOF' |
| 9 | Usage: |
| 10 | ./install [dev|release] <device> <module> [module...] |
| 11 | ./install <device> <module> [module...] # defaults to release |
| 12 | ./install [dev|release] <device> all # installs all app modules |
| 13 | ./install <device> all # same, defaults to release |
| 14 | ./install [dev|release] [8|9] <module> # Pixel 8 or Pixel 9 (by model) |
| 15 | ./install --device=watch <module> # Pixel Watch (by model; merged single-APK apps install their watch experience) |
| 16 | |
| 17 | Description: |
| 18 | Ergonomic wrapper around ./gradlew :module:assembleRelease + adb session install. |
| 19 | - Variant is swappable and optional (defaults to release; dev is almost |
| 20 | never the right install — pass dev explicitly when you need it) |
| 21 | - Supports both slash and colon notations: games/voxels == games:voxels |
| 22 | - Supports games shorthand: ./install voxels -> games:voxels (auto-prefixed if games/<name> is a game) |
| 23 | - Supports personal shorthand: ./install dooraccess -> personal:dooraccess (auto-prefixed if personal/<name> is a personal app) |
| 24 | - Supports "all" keyword: ./install all -> installs every app module |
| 25 | - Supports multiple modules in one call (single gradlew invocation) |
| 26 | - The device argument is REQUIRED: 8|9 selects the Pixel 8 or Pixel 9 by |
| 27 | model, --device=watch selects the Pixel Watch by model (only merged |
| 28 | single-APK apps and :watch:wear make sense there). Also accepts |
| 29 | --device 8|9 / --device=8|9. There is no default device. |
| 30 | - Installs via adb package sessions with the .idsig sidecar staged, so |
| 31 | updates to preinstalled MAOS system apps get fs-verity (plain |
| 32 | installDev/installRelease and `adb install` never stage it and always fail |
| 33 | those updates). Auto-passes -PversionCodeOverride when the on-device |
| 34 | package is at the same versionCode as the build: +1 for system apps, |
| 35 | exactly the on-device version for user apps. |
| 36 | |
| 37 | Examples: |
| 38 | ./install release contacts -> :contacts:assembleRelease + session install |
| 39 | ./install release games:voxels -> :games:voxels:assembleRelease + session install |
| 40 | ./install dev games/voxels -> :games:voxels:assembleDev (slash normalized) |
| 41 | ./install dev :games:voxels: -> :games:voxels:assembleDev (colon trimming) |
| 42 | ./install voxels -> :games:voxels:assembleRelease (shorthand auto games:) |
| 43 | ./install chess -> :games:chess:assembleRelease (shorthand auto games:) |
| 44 | ./install dooraccess -> :personal:dooraccess:assembleRelease (shorthand auto personal:) |
| 45 | ./install all -> :contacts:assembleRelease :games:chess:assembleRelease ... (all modules) |
| 46 | ./install release all -> same as above with release variant |
| 47 | ./install contacts calendar -> :contacts:assembleRelease :calendar:assembleRelease |
| 48 | ./install dev contacts -> :contacts:assembleDev + session install |
| 49 | ./install contacts dev -> :contacts:assembleDev (variant anywhere) |
| 50 | ./install 9 maps -> :maps:assembleRelease + session install to the Pixel 9 |
| 51 | ./install dev 8 maps -> :maps:assembleDev + session install to the Pixel 8 |
| 52 | ./install --help |
| 53 | ./install --dry-run contacts # prints what would run without executing gradle |
| 54 | ./install --dry-run all # preview all modules |
| 55 | |
| 56 | Variant rules: |
| 57 | - release (default): assembleRelease + session install unless explicitly asked for dev |
| 58 | - dev: assembleDev + session install when explicitly requested |
| 59 | - debug: BLOCKED - never use assembleDebug |
| 60 | |
| 61 | Notes: |
| 62 | - NEVER uninstalls an app (no uninstall tasks) |
| 63 | - Validates modules against app modules (those applying common-conventions-app) |
| 64 | - Installs to the first connected device whose serial does not start with 'emulator' |
| 65 | (unless 8|9 selects the Pixel 8 or Pixel 9) |
| 66 | - Set INSTALL_DRY_RUN=1 or pass --dry-run for dry-run mode |
| 67 | |
| 68 | EOF |
| 69 | } |
| 70 | |
| 71 | normalize_module() { |
| 72 | local m="$1" |
| 73 | m="$(echo "$m" | xargs)" |
| 74 | m="$(echo "$m" | sed -e 's|^[/:]*||' -e 's|[/:]*$||')" |
| 75 | m="$(echo "$m" | tr '/' ':')" |
| 76 | while echo "$m" | grep -q "::"; do |
| 77 | m="$(echo "$m" | sed 's/::/:/g')" |
| 78 | done |
| 79 | m="$(echo "$m" | tr '[:upper:]' '[:lower:]')" |
| 80 | echo "$m" |
| 81 | } |
| 82 | |
| 83 | get_app_modules_slash() { |
| 84 | local settings_file="$ROOT/settings.gradle.kts" |
| 85 | if [[ ! -f "$settings_file" ]]; then |
| 86 | return |
| 87 | fi |
| 88 | grep -E 'include\(":[^"]+"\)' "$settings_file" 2>/dev/null \ |
| 89 | | sed -E 's/.*include\(":([^"]+)"\).*/\1/' \ |
| 90 | | tr ':' '/' \ |
| 91 | | while IFS= read -r p; do |
| 92 | case "$p" in "" ) continue;; esac |
| 93 | if [[ -f "$ROOT/$p/build.gradle.kts" ]] && grep -qF 'id("common-conventions-app")' "$ROOT/$p/build.gradle.kts" 2>/dev/null; then |
| 94 | echo "$p" |
| 95 | fi |
| 96 | done | sort -u |
| 97 | } |
| 98 | |
| 99 | print_valid_modules() { |
| 100 | local slash_modules |
| 101 | slash_modules="$(get_app_modules_slash)" |
| 102 | if [[ -z "$slash_modules" ]]; then |
| 103 | echo " (could not discover app modules)" |
| 104 | return |
| 105 | fi |
| 106 | echo "" |
| 107 | echo "Valid app modules (slash notation for filesystem, colon for Gradle):" |
| 108 | echo "" |
| 109 | printf " %-30s %s\n" "Filesystem (slash)" "Gradle (colon)" |
| 110 | printf " %-30s %s\n" "--------------------" "-------------------" |
| 111 | while IFS= read -r sm; do |
| 112 | case "$sm" in "" ) continue;; esac |
| 113 | colon_mod="$(echo "$sm" | tr '/' ':')" |
| 114 | printf " %-30s :%s\n" "$sm" "$colon_mod" |
| 115 | done <<< "$slash_modules" |
| 116 | echo "" |
| 117 | echo "Tip: Use either notation: ./install dev games/voxels OR ./install dev games:voxels" |
| 118 | echo "Shorthand: ./install dev voxels (auto-expands to games:voxels if games/<name> exists)" |
| 119 | echo " ./install dev dooraccess (auto-expands to personal:dooraccess if personal/<name> exists)" |
| 120 | echo "All: ./install all (installs every app module)" |
| 121 | } |
| 122 | |
| 123 | array_contains() { |
| 124 | local needle="$1" |
| 125 | shift |
| 126 | local item |
| 127 | for item in "$@"; do |
| 128 | if [[ "$item" == "$needle" ]]; then |
| 129 | return 0 |
| 130 | fi |
| 131 | done |
| 132 | return 1 |
| 133 | } |
| 134 | |
| 135 | # --- Handle zero args early (no array expansion needed) --- |
| 136 | if [[ $# -eq 0 ]]; then |
| 137 | print_help |
| 138 | echo "" |
| 139 | echo "Error: No modules specified." |
| 140 | print_valid_modules |
| 141 | exit 1 |
| 142 | fi |
| 143 | |
| 144 | # --- Detect --help/-h/help anywhere (direct $@ loop, no array) --- |
| 145 | for arg in "$@"; do |
| 146 | case "$arg" in |
| 147 | --help|-h|help) |
| 148 | print_help |
| 149 | exit 0 |
| 150 | ;; |
| 151 | esac |
| 152 | done |
| 153 | |
| 154 | # --- Early DRY_RUN detection --- |
| 155 | DRY_RUN=0 |
| 156 | if [[ "${INSTALL_DRY_RUN:-0}" == "1" ]]; then |
| 157 | DRY_RUN=1 |
| 158 | fi |
| 159 | for arg in "$@"; do |
| 160 | if [[ "$arg" == "--dry-run" ]]; then |
| 161 | DRY_RUN=1 |
| 162 | fi |
| 163 | done |
| 164 | |
| 165 | # --- Build FILTERED_ARGS (exclude --dry-run) --- |
| 166 | FILTERED_ARGS=() |
| 167 | for arg in "$@"; do |
| 168 | if [[ "$arg" == "--dry-run" ]]; then |
| 169 | continue |
| 170 | fi |
| 171 | FILTERED_ARGS+=("$arg") |
| 172 | done |
| 173 | |
| 174 | # After filtering, if empty (e.g., only --dry-run was passed) |
| 175 | if [[ ${#FILTERED_ARGS[@]} -eq 0 ]]; then |
| 176 | print_help |
| 177 | echo "" |
| 178 | echo "Error: No modules specified (dry-run mode)." |
| 179 | print_valid_modules |
| 180 | exit 1 |
| 181 | fi |
| 182 | |
| 183 | # --- Device hint: bare 8|9 anywhere, or --device 8|9 / --device=8|9 (optional) --- |
| 184 | DEVICE_HINT="" |
| 185 | PRESCAN_ARGS=() |
| 186 | idx=0 |
| 187 | while [[ $idx -lt ${#FILTERED_ARGS[@]} ]]; do |
| 188 | arg="${FILTERED_ARGS[$idx]}" |
| 189 | lc="$(echo "$arg" | tr '[:upper:]' '[:lower:]')" |
| 190 | if [[ "$lc" == "--device" ]]; then |
| 191 | if [[ $((idx + 1)) -ge ${#FILTERED_ARGS[@]} ]]; then |
| 192 | echo "Error: --device requires a value: 8 (Pixel 8), 9 (Pixel 9) or watch (Pixel Watch)." |
| 193 | exit 1 |
| 194 | fi |
| 195 | DEVICE_HINT="$(echo "${FILTERED_ARGS[$((idx + 1))]}" | tr '[:upper:]' '[:lower:]')" |
| 196 | idx=$((idx + 2)) |
| 197 | continue |
| 198 | elif [[ "$lc" == --device=* ]]; then |
| 199 | DEVICE_HINT="${lc#--device=}" |
| 200 | idx=$((idx + 1)) |
| 201 | continue |
| 202 | elif [[ "$lc" == "8" || "$lc" == "9" ]]; then |
| 203 | DEVICE_HINT="$lc" |
| 204 | idx=$((idx + 1)) |
| 205 | continue |
| 206 | elif [[ "$lc" =~ ^[0-9]+$ ]]; then |
| 207 | echo "Error: Invalid device '$arg'. Use 8 (Pixel 8), 9 (Pixel 9) or --device=watch." |
| 208 | exit 1 |
| 209 | fi |
| 210 | PRESCAN_ARGS+=("$arg") |
| 211 | idx=$((idx + 1)) |
| 212 | done |
| 213 | FILTERED_ARGS=("${PRESCAN_ARGS[@]}") |
| 214 | if [[ -n "$DEVICE_HINT" && "$DEVICE_HINT" != "8" && "$DEVICE_HINT" != "9" && "$DEVICE_HINT" != "watch" ]]; then |
| 215 | echo "Error: Invalid device '$DEVICE_HINT'. Use 8 (Pixel 8), 9 (Pixel 9) or watch (Pixel Watch)." |
| 216 | exit 1 |
| 217 | fi |
| 218 | if [[ -z "$DEVICE_HINT" ]]; then |
| 219 | echo "Error: a target device is required: pass 8 (Pixel 8), 9 (Pixel 9) or --device=watch (Pixel Watch); there is no default device." |
| 220 | exit 1 |
| 221 | fi |
| 222 | |
| 223 | # --- Arg parsing: variant detection anywhere --- |
| 224 | VARIANT_LC="" |
| 225 | VARIANT_COUNT=0 |
| 226 | MODULES_RAW=() |
| 227 | |
| 228 | idx=0 |
| 229 | while [[ $idx -lt ${#FILTERED_ARGS[@]} ]]; do |
| 230 | arg="${FILTERED_ARGS[$idx]}" |
| 231 | lc="$(echo "$arg" | tr '[:upper:]' '[:lower:]')" |
| 232 | if [[ "$lc" == "dev" || "$lc" == "release" || "$lc" == "debug" ]]; then |
| 233 | if [[ -z "$VARIANT_LC" ]]; then |
| 234 | VARIANT_LC="$lc" |
| 235 | VARIANT_COUNT=1 |
| 236 | else |
| 237 | if [[ "$VARIANT_LC" != "$lc" ]]; then |
| 238 | echo "Error: Multiple distinct variants specified: '$VARIANT_LC' and '$lc'." |
| 239 | echo "Please specify only one variant: dev or release." |
| 240 | echo "" |
| 241 | print_help |
| 242 | exit 1 |
| 243 | fi |
| 244 | VARIANT_COUNT=$((VARIANT_COUNT + 1)) |
| 245 | fi |
| 246 | else |
| 247 | MODULES_RAW+=("$arg") |
| 248 | fi |
| 249 | idx=$((idx + 1)) |
| 250 | done |
| 251 | |
| 252 | if [[ -z "$VARIANT_LC" ]]; then |
| 253 | VARIANT_LC="release" |
| 254 | fi |
| 255 | |
| 256 | if [[ "$VARIANT_LC" == "debug" ]]; then |
| 257 | echo "Error: 'debug' variant is blocked." |
| 258 | echo "" |
| 259 | echo " - Never assemble or install a debug variant" |
| 260 | echo " - Always install the release build, unless specifically asked for dev" |
| 261 | echo "" |
| 262 | echo "Use 'release' (default) or 'dev' instead:" |
| 263 | echo " ./install <module>" |
| 264 | echo " ./install dev <module>" |
| 265 | exit 1 |
| 266 | fi |
| 267 | |
| 268 | if [[ ${#MODULES_RAW[@]} -eq 0 ]]; then |
| 269 | echo "Error: No modules specified (only variant '$VARIANT_LC' found)." |
| 270 | echo "" |
| 271 | print_help |
| 272 | print_valid_modules |
| 273 | exit 1 |
| 274 | fi |
| 275 | |
| 276 | # --- Normalization + Dedup + Shorthand + "all" + auto games: prefix --- |
| 277 | NORMALIZED_MODULES=() |
| 278 | ALL_EXPANDED=0 |
| 279 | |
| 280 | idx=0 |
| 281 | while [[ $idx -lt ${#MODULES_RAW[@]} ]]; do |
| 282 | raw="${MODULES_RAW[$idx]}" |
| 283 | norm="$(normalize_module "$raw")" |
| 284 | if [[ -z "$norm" ]]; then |
| 285 | echo "Warning: Skipping empty/invalid module token '$raw' after normalization." |
| 286 | idx=$((idx + 1)) |
| 287 | continue |
| 288 | fi |
| 289 | |
| 290 | # Handle "all" keyword - expand to every app module |
| 291 | if [[ "$norm" == "all" ]]; then |
| 292 | if [[ $ALL_EXPANDED -eq 0 ]]; then |
| 293 | echo "Info: Expanding 'all' to all app modules..." |
| 294 | all_slash="$(get_app_modules_slash)" |
| 295 | if [[ -z "$all_slash" ]]; then |
| 296 | echo "Error: Could not discover app modules for 'all' expansion." |
| 297 | print_valid_modules |
| 298 | exit 1 |
| 299 | fi |
| 300 | while IFS= read -r sm; do |
| 301 | case "$sm" in "" ) continue;; esac |
| 302 | colon_mod="$(echo "$sm" | tr '/' ':')" |
| 303 | # dedup for expanded modules |
| 304 | dup=0 |
| 305 | j=0 |
| 306 | while [[ $j -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 307 | if [[ "${NORMALIZED_MODULES[$j]}" == "$colon_mod" ]]; then |
| 308 | dup=1 |
| 309 | break |
| 310 | fi |
| 311 | j=$((j + 1)) |
| 312 | done |
| 313 | if [[ $dup -eq 0 ]]; then |
| 314 | NORMALIZED_MODULES+=("$colon_mod") |
| 315 | fi |
| 316 | done <<< "$all_slash" |
| 317 | ALL_EXPANDED=1 |
| 318 | fi |
| 319 | idx=$((idx + 1)) |
| 320 | continue |
| 321 | fi |
| 322 | |
| 323 | # Auto games:/personal: prefix - if token has no colon and games/<name> or personal/<name> is a valid app module, prefix it |
| 324 | # This allows ./install voxels -> games:voxels, ./install chess -> games:chess, ./install dooraccess -> personal:dooraccess etc. |
| 325 | if [[ "$norm" != *":"* ]]; then |
| 326 | for auto_prefix in games personal; do |
| 327 | pref_path="$ROOT/$auto_prefix/$norm" |
| 328 | if [[ -f "$pref_path/build.gradle.kts" ]] && grep -qF 'id("common-conventions-app")' "$pref_path/build.gradle.kts" 2>/dev/null; then |
| 329 | expanded="$auto_prefix:$norm" |
| 330 | echo "Info: Expanding shorthand '$raw' ($norm) -> $expanded (found $auto_prefix/$norm)" |
| 331 | norm="$expanded" |
| 332 | break |
| 333 | elif [[ ! -e "$ROOT/$norm" && -e "$ROOT/$auto_prefix/$norm" ]]; then |
| 334 | # Fallback: if root/<name> doesn't exist but <prefix>/<name> does (backward compat) |
| 335 | expanded="$auto_prefix:$norm" |
| 336 | echo "Info: Expanding shorthand '$raw' ($norm) -> $expanded (found $auto_prefix/$norm)" |
| 337 | norm="$expanded" |
| 338 | break |
| 339 | fi |
| 340 | done |
| 341 | fi |
| 342 | |
| 343 | # Dedup via index loop (bash 3.2 safe) |
| 344 | dup=0 |
| 345 | j=0 |
| 346 | while [[ $j -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 347 | if [[ "${NORMALIZED_MODULES[$j]}" == "$norm" ]]; then |
| 348 | dup=1 |
| 349 | break |
| 350 | fi |
| 351 | j=$((j + 1)) |
| 352 | done |
| 353 | |
| 354 | if [[ $dup -eq 0 ]]; then |
| 355 | NORMALIZED_MODULES+=("$norm") |
| 356 | fi |
| 357 | idx=$((idx + 1)) |
| 358 | done |
| 359 | |
| 360 | if [[ ${#NORMALIZED_MODULES[@]} -eq 0 ]]; then |
| 361 | echo "Error: No valid modules after normalization." |
| 362 | print_valid_modules |
| 363 | exit 1 |
| 364 | fi |
| 365 | |
| 366 | # --- Validation --- |
| 367 | INVALID_MODULES=() |
| 368 | idx=0 |
| 369 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 370 | mod="${NORMALIZED_MODULES[$idx]}" |
| 371 | fs_path="$(echo "$mod" | tr ':' '/')" |
| 372 | full_path="$ROOT/$fs_path" |
| 373 | if [[ ! -f "$full_path/build.gradle.kts" ]]; then |
| 374 | INVALID_MODULES+=("$mod (not found: $fs_path/build.gradle.kts)") |
| 375 | else |
| 376 | if ! grep -qF 'id("common-conventions-app")' "$full_path/build.gradle.kts" 2>/dev/null; then |
| 377 | INVALID_MODULES+=("$mod (exists but is not an app module - missing common-conventions-app)") |
| 378 | fi |
| 379 | fi |
| 380 | idx=$((idx + 1)) |
| 381 | done |
| 382 | |
| 383 | if [[ ${#INVALID_MODULES[@]} -gt 0 ]]; then |
| 384 | echo "Error: Some modules are invalid:" |
| 385 | idx=0 |
| 386 | while [[ $idx -lt ${#INVALID_MODULES[@]} ]]; do |
| 387 | echo " - ${INVALID_MODULES[$idx]}" |
| 388 | idx=$((idx + 1)) |
| 389 | done |
| 390 | print_valid_modules |
| 391 | exit 1 |
| 392 | fi |
| 393 | |
| 394 | # --- Build Gradle tasks (assemble only; install happens below via adb sessions) --- |
| 395 | VARIANT_CAP="$(echo "$VARIANT_LC" | awk '{print toupper(substr($0,1,1)) substr($0,2)}')" |
| 396 | TASKS=() |
| 397 | idx=0 |
| 398 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 399 | mod="${NORMALIZED_MODULES[$idx]}" |
| 400 | TASKS+=(":${mod}:assemble${VARIANT_CAP}") |
| 401 | idx=$((idx + 1)) |
| 402 | done |
| 403 | |
| 404 | # Safe joining without ${ARRAY[*]} under set -u when empty (should not be empty here, but guard) |
| 405 | modules_str="" |
| 406 | tasks_str="" |
| 407 | idx=0 |
| 408 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 409 | if [[ $idx -gt 0 ]]; then |
| 410 | modules_str="$modules_str ${NORMALIZED_MODULES[$idx]}" |
| 411 | else |
| 412 | modules_str="${NORMALIZED_MODULES[$idx]}" |
| 413 | fi |
| 414 | idx=$((idx + 1)) |
| 415 | done |
| 416 | idx=0 |
| 417 | while [[ $idx -lt ${#TASKS[@]} ]]; do |
| 418 | if [[ $idx -gt 0 ]]; then |
| 419 | tasks_str="$tasks_str ${TASKS[$idx]}" |
| 420 | else |
| 421 | tasks_str="${TASKS[$idx]}" |
| 422 | fi |
| 423 | idx=$((idx + 1)) |
| 424 | done |
| 425 | |
| 426 | echo "Variant: $VARIANT_LC (assemble + adb session install)" |
| 427 | echo "Modules (${#NORMALIZED_MODULES[@]}): $modules_str" |
| 428 | echo "Gradle tasks: $tasks_str" |
| 429 | echo "" |
| 430 | |
| 431 | # --- Device selection: first connected device whose serial doesn't start with 'emulator' --- |
| 432 | ADB_BIN="" |
| 433 | if command -v adb >/dev/null 2>&1; then |
| 434 | ADB_BIN="adb" |
| 435 | else |
| 436 | for adb_base in "${ANDROID_HOME:-}" "${ANDROID_SDK_ROOT:-}" "$HOME/Android/Sdk" "$HOME/Library/Android/sdk"; do |
| 437 | if [[ -n "$adb_base" && -x "$adb_base/platform-tools/adb" ]]; then |
| 438 | ADB_BIN="$adb_base/platform-tools/adb" |
| 439 | break |
| 440 | fi |
| 441 | done |
| 442 | fi |
| 443 | |
| 444 | TARGET_SERIAL="" |
| 445 | if [[ -n "$ADB_BIN" ]]; then |
| 446 | # hint '' = first non-emulator device; '8' = model Pixel_8; |
| 447 | # '9' = model Pixel_9* (covers Pixel_9 and Pixel_9_Pro_XL). |
| 448 | while read -r dev_line; do |
| 449 | dev_serial="$(echo "$dev_line" | awk '{print $1}')" |
| 450 | dev_state="$(echo "$dev_line" | awk '{print $2}')" |
| 451 | [[ "$dev_state" == "device" ]] || continue |
| 452 | case "$dev_serial" in emulator*) continue;; esac |
| 453 | case "$dev_serial" in "List"*) continue;; esac |
| 454 | if [[ -z "$DEVICE_HINT" ]]; then |
| 455 | TARGET_SERIAL="$dev_serial" |
| 456 | break |
| 457 | fi |
| 458 | dev_model="$(echo "$dev_line" | sed -nE 's/.*model:([^[:space:]]+).*/\1/p')" |
| 459 | if [[ "$DEVICE_HINT" == "8" && "$dev_model" == "Pixel_8" ]]; then |
| 460 | TARGET_SERIAL="$dev_serial" |
| 461 | break |
| 462 | fi |
| 463 | if [[ "$DEVICE_HINT" == "9" && "$dev_model" == Pixel_9* ]]; then |
| 464 | TARGET_SERIAL="$dev_serial" |
| 465 | break |
| 466 | fi |
| 467 | if [[ "$DEVICE_HINT" == "watch" && "$dev_model" == *Watch* ]]; then |
| 468 | TARGET_SERIAL="$dev_serial" |
| 469 | break |
| 470 | fi |
| 471 | done < <("$ADB_BIN" devices -l 2>/dev/null) |
| 472 | fi |
| 473 | |
| 474 | if [[ -z "$ADB_BIN" ]]; then |
| 475 | if [[ $DRY_RUN -eq 1 ]]; then |
| 476 | echo "[DRY-RUN] Warning: adb not found; a device could not be selected." |
| 477 | else |
| 478 | echo "Error: adb not found; cannot select a target device." |
| 479 | echo "Install Android platform-tools and put adb on PATH (or set ANDROID_HOME)." |
| 480 | exit 1 |
| 481 | fi |
| 482 | elif [[ -n "$TARGET_SERIAL" ]]; then |
| 483 | export ANDROID_SERIAL="$TARGET_SERIAL" |
| 484 | if [[ -n "$DEVICE_HINT" ]]; then |
| 485 | echo "Target device: $ANDROID_SERIAL (Pixel $DEVICE_HINT)" |
| 486 | else |
| 487 | echo "Target device: $ANDROID_SERIAL (first connected device not starting with 'emulator')" |
| 488 | fi |
| 489 | elif [[ -n "$DEVICE_HINT" ]]; then |
| 490 | echo "Error: No connected device matching '$DEVICE_HINT' found (check 'adb devices')." |
| 491 | exit 1 |
| 492 | elif [[ $DRY_RUN -eq 1 ]]; then |
| 493 | echo "[DRY-RUN] Warning: no non-emulator device connected; ANDROID_SERIAL not set." |
| 494 | else |
| 495 | echo "Error: No non-emulator device connected." |
| 496 | echo "Connect a physical device (check 'adb devices'); refusing to install to an emulator." |
| 497 | exit 1 |
| 498 | fi |
| 499 | |
| 500 | # applicationId for a module (all are string literals in build.gradle.kts). |
| 501 | module_application_id() { |
| 502 | sed -nE 's/^[[:space:]]*applicationId[[:space:]]*=[[:space:]]*"([^"]+)".*/\1/p' \ |
| 503 | "$ROOT/$(echo "$1" | tr ':' '/')/build.gradle.kts" | head -1 |
| 504 | } |
| 505 | |
| 506 | # APK built for a module; output-metadata.json names the file authoritatively |
| 507 | # (nested modules like cast:tv build tv-dev.apk, not cast-tv-dev.apk). |
| 508 | built_apk() { |
| 509 | local mod="$1" fs_path out_dir meta base_name file_name |
| 510 | fs_path="$(echo "$mod" | tr ':' '/')" |
| 511 | out_dir="$ROOT/$fs_path/build/outputs/apk/$VARIANT_LC" |
| 512 | meta="$out_dir/output-metadata.json" |
| 513 | base_name="${mod##*:}" |
| 514 | file_name="$base_name-$VARIANT_LC.apk" |
| 515 | if [[ -f "$meta" ]]; then |
| 516 | local found |
| 517 | found="$(grep -oE '"outputFile"[[:space:]]*:[[:space:]]*"[^"]+"' "$meta" | head -1 | sed -E 's/.*\"([^\"]+)\"$/\1/')" |
| 518 | [[ -n "$found" ]] && file_name="$found" |
| 519 | fi |
| 520 | if [[ -f "$out_dir/$file_name" ]]; then |
| 521 | echo "$out_dir/$file_name" |
| 522 | return 0 |
| 523 | fi |
| 524 | return 1 |
| 525 | } |
| 526 | |
| 527 | # Run one adb command against the target device, riding out a dropped |
| 528 | # network-adb transport: the forwarded endpoint the phone hangs off |
| 529 | # (127.0.0.1:15556) loses the device for a few seconds at a time under |
| 530 | # install load, and without this a single drop fails every remaining app |
| 531 | # in the run. Combined output goes to stdout; reconnect notes and the |
| 532 | # final error go to stderr (callers used to discard adb's stderr entirely, |
| 533 | # which hid exactly this failure). |
| 534 | adb_dev() { |
| 535 | local attempt out |
| 536 | for attempt in 1 2 3 4; do |
| 537 | out="$("$ADB_BIN" -s "$TARGET_SERIAL" "$@" 2>&1)" && { printf '%s' "$out"; return 0; } |
| 538 | if echo "$out" | grep -qiE "device '[^']*' not found|device offline|no devices/emulators"; then |
| 539 | echo " adb: transport to $TARGET_SERIAL lost (attempt $attempt); reconnecting..." >&2 |
| 540 | "$ADB_BIN" connect "$TARGET_SERIAL" >/dev/null 2>&1 |
| 541 | # Bounded wait: a tunnel that stays down must fail this app, not |
| 542 | # freeze the whole run on wait-for-device. |
| 543 | local waited=0 |
| 544 | while [[ $waited -lt 30 ]]; do |
| 545 | [[ "$("$ADB_BIN" -s "$TARGET_SERIAL" get-state 2>/dev/null)" == "device" ]] && break |
| 546 | sleep 2; waited=$((waited + 2)) |
| 547 | done |
| 548 | continue |
| 549 | fi |
| 550 | printf '%s\n' "$out" >&2 |
| 551 | return 1 |
| 552 | done |
| 553 | printf '%s\n' "$out" >&2 |
| 554 | return 1 |
| 555 | } |
| 556 | |
| 557 | # on-device versionCode for a package, empty when not installed. |
| 558 | device_version_code() { |
| 559 | # No `head -1` here: with pipefail, head closing the pipe SIGPIPEs adb and |
| 560 | # the failing assignment kills the script (set -e) after the first module. |
| 561 | adb_dev shell pm list packages --show-versioncode "$1" \ |
| 562 | | sed -nE "s/^package:${1//./\\.}[[:space:]]+versionCode:([0-9]+).*/\\1/p" |
| 563 | } |
| 564 | |
| 565 | # exit 0 when the package is installed as a system app. |
| 566 | is_system_app() { |
| 567 | adb_dev shell pm list packages -s "$1" \ |
| 568 | | tr -d '\r' | grep -qFx "package:$1" |
| 569 | } |
| 570 | |
| 571 | install_apk_via_session() { |
| 572 | local apk="$1" package_id="$2" cur="${3:-}" total="${4:-}" |
| 573 | local apk_name apk_size apk_mb session device_apk idsig device_idsig commit_out prefix |
| 574 | apk_name="$(basename "$apk")" |
| 575 | apk_size="$(wc -c < "$apk" | tr -d ' ')" |
| 576 | apk_mb="$(awk "BEGIN {printf \"%.1f\", $apk_size/1048576}")" |
| 577 | device_apk="/data/local/tmp/install_${package_id}.apk" |
| 578 | if [[ -n "$cur" && -n "$total" ]]; then |
| 579 | prefix="[$cur/$total] " |
| 580 | else |
| 581 | prefix="" |
| 582 | fi |
| 583 | echo "${prefix}Installing $package_id ($apk_name, ${apk_mb} MB)..." |
| 584 | echo " ${prefix}[1/4] Pushing APK to device..." |
| 585 | adb_dev push "$apk" "$device_apk" >/dev/null || { |
| 586 | echo " ${prefix}Error: adb push failed for $apk_name."; return 1 |
| 587 | } |
| 588 | echo " ${prefix}[2/4] Creating install session..." |
| 589 | session="$(adb_dev shell pm install-create -r \ |
| 590 | | sed -nE 's/.*created install session \[([0-9]+)\].*/\1/p')" |
| 591 | if [[ -z "$session" ]]; then |
| 592 | echo " ${prefix}Error: could not create install session." |
| 593 | adb_dev shell rm -f "$device_apk" >/dev/null |
| 594 | return 1 |
| 595 | fi |
| 596 | # Stage the APK first, then its .idsig sidecar (V4 signature) under the |
| 597 | # matching name. PackageManager enables fs-verity from a staged idsig, which |
| 598 | # MAOS/GrapheneOS requires for updates to preinstalled system apps. Plain |
| 599 | # `adb install` never stages the sidecar, so those updates always fail. |
| 600 | echo " ${prefix}[3/4] Staging APK into session $session..." |
| 601 | adb_dev shell pm install-write -S "$apk_size" "$session" base.apk "$device_apk" >/dev/null || { |
| 602 | echo " ${prefix}Error: failed to stage $apk_name into session $session." |
| 603 | adb_dev shell pm install-abandon "$session" >/dev/null |
| 604 | adb_dev shell rm -f "$device_apk" >/dev/null |
| 605 | return 1 |
| 606 | } |
| 607 | idsig="$apk.idsig" |
| 608 | if [[ -f "$idsig" ]]; then |
| 609 | device_idsig="$device_apk.idsig" |
| 610 | echo " ${prefix}Staging .idsig sidecar..." |
| 611 | if adb_dev push "$idsig" "$device_idsig" >/dev/null; then |
| 612 | adb_dev shell pm install-write -S "$(wc -c < "$idsig" | tr -d ' ')" \ |
| 613 | "$session" base.apk.idsig "$device_idsig" >/dev/null || \ |
| 614 | echo " ${prefix}Warning: could not stage .idsig; system-app updates may fail fs-verity." |
| 615 | else |
| 616 | echo " ${prefix}Warning: could not push .idsig; system-app updates may fail fs-verity." |
| 617 | fi |
| 618 | adb_dev shell rm -f "$device_idsig" >/dev/null |
| 619 | else |
| 620 | echo " ${prefix}Warning: no .idsig sidecar next to the APK; system-app updates may fail fs-verity." |
| 621 | fi |
| 622 | echo " ${prefix}[4/4] Committing install session $session..." |
| 623 | commit_out="$(adb_dev shell pm install-commit "$session")" |
| 624 | adb_dev shell rm -f "$device_apk" >/dev/null |
| 625 | # `pm install-commit` prints "Success" but exits 0 either way; fail on absence. |
| 626 | if ! echo "$commit_out" | grep -qi "^Success"; then |
| 627 | echo " ${prefix}Error: install failed for $package_id: $commit_out" |
| 628 | return 1 |
| 629 | fi |
| 630 | if [[ -n "$cur" && -n "$total" ]]; then |
| 631 | echo " Installed $package_id. [$cur/$total done]" |
| 632 | else |
| 633 | echo " Installed $package_id." |
| 634 | fi |
| 635 | } |
| 636 | |
| 637 | # --- Extra Gradle args: --continue so one broken module doesn't block the rest --- |
| 638 | GRADLE_ARGS=(--continue) |
| 639 | gradle_args_str=" --continue" |
| 640 | if [[ "$VARIANT_LC" == "release" && $ALL_EXPANDED -eq 1 ]]; then |
| 641 | GRADLE_ARGS+=(-P"android.r8.maxWorkers=8") |
| 642 | gradle_args_str="$gradle_args_str -Pandroid.r8.maxWorkers=8" |
| 643 | fi |
| 644 | |
| 645 | # --- Big-build heap: gradle.properties defaults to a small daemon heap sized for |
| 646 | # scoped work. A full-repo build ("all", ~100 modules, concurrent D8 dex merge) |
| 647 | # OOMs on it, so escalate here — and only here (ALL_EXPANDED). -D spawns a |
| 648 | # separate big daemon; the small one stays alive for normal builds. |
| 649 | if [[ $ALL_EXPANDED -eq 1 ]]; then |
| 650 | GRADLE_ARGS+=(-D"org.gradle.jvmargs=-Xmx46144m -Xms4512m -XX:MaxMetaspaceSize=1024m -XX:+UseG1GC -Dfile.encoding=UTF-8") |
| 651 | GRADLE_ARGS+=(--max-workers=32) |
| 652 | gradle_args_str="$gradle_args_str -Dorg.gradle.jvmargs=<46g-big-build-heap> --max-workers=32" |
| 653 | fi |
| 654 | |
| 655 | # --- versionCodeOverride: MAOS refuses to update a system package to the same |
| 656 | # versionCode, so an APK built from the same version.txt as the on-device OS image |
| 657 | # can never be installed over it. Query the device once; for system apps bump to |
| 658 | # one above the on-device version, for user apps just match it exactly |
| 659 | # (version.txt itself stays untouched). |
| 660 | REPO_VERSION="$(sed -n '1p' "$ROOT/version.txt" | tr -d '\r' | xargs)" |
| 661 | NEED_BUMP="$REPO_VERSION" |
| 662 | if [[ -n "$TARGET_SERIAL" ]]; then |
| 663 | idx=0 |
| 664 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 665 | pkg="$(module_application_id "${NORMALIZED_MODULES[$idx]}")" |
| 666 | if [[ -n "$pkg" ]]; then |
| 667 | dev_v="$(device_version_code "$pkg")" |
| 668 | if [[ -n "$dev_v" && "$dev_v" -ge "$NEED_BUMP" ]]; then |
| 669 | if is_system_app "$pkg"; then |
| 670 | target=$((dev_v + 1)) |
| 671 | kind="system app" |
| 672 | else |
| 673 | target=$dev_v |
| 674 | kind="user app" |
| 675 | fi |
| 676 | if [[ "$target" -gt "$NEED_BUMP" ]]; then |
| 677 | NEED_BUMP=$target |
| 678 | echo "Info: $pkg is a $kind at versionCode $dev_v on-device (>= repo $REPO_VERSION); will build with -PversionCodeOverride=$target." |
| 679 | fi |
| 680 | fi |
| 681 | fi |
| 682 | idx=$((idx + 1)) |
| 683 | done |
| 684 | fi |
| 685 | if [[ "$NEED_BUMP" -gt "$REPO_VERSION" ]]; then |
| 686 | GRADLE_ARGS+=(-P"versionCodeOverride=$NEED_BUMP") |
| 687 | gradle_args_str="$gradle_args_str -PversionCodeOverride=$NEED_BUMP" |
| 688 | fi |
| 689 | |
| 690 | if [[ $DRY_RUN -eq 1 ]]; then |
| 691 | echo "[DRY-RUN] Would execute: $SCRIPT_DIR/gradlew $tasks_str$gradle_args_str" |
| 692 | idx=0 |
| 693 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 694 | mod="${NORMALIZED_MODULES[$idx]}" |
| 695 | if apk="$(built_apk "$mod")"; then |
| 696 | echo "[DRY-RUN] Would session-install $(module_application_id "$mod") from $apk" |
| 697 | else |
| 698 | echo "[DRY-RUN] Would session-install $(module_application_id "$mod") (APK expected after assemble)" |
| 699 | fi |
| 700 | idx=$((idx + 1)) |
| 701 | done |
| 702 | exit 0 |
| 703 | fi |
| 704 | echo "Running: $SCRIPT_DIR/gradlew $tasks_str$gradle_args_str" |
| 705 | echo "" |
| 706 | |
| 707 | # Execute with all tasks expanded via index loop (preserving word splitting safety). |
| 708 | # --continue: one failing module must not starve the rest of the build, and the |
| 709 | # installer below already session-installs whichever APKs exist on failure. |
| 710 | set +u |
| 711 | "$SCRIPT_DIR/gradlew" --continue "${TASKS[@]}" "${GRADLE_ARGS[@]}" |
| 712 | BUILD_EXIT=$? |
| 713 | echo "" |
| 714 | |
| 715 | # Install whichever APKs were (re)built, even if sibling modules failed (--continue). |
| 716 | FAILURES=0 |
| 717 | if [[ $BUILD_EXIT -ne 0 ]]; then |
| 718 | echo "Warning: assemble reported failures; installing whatever APKs exist." |
| 719 | fi |
| 720 | TOTAL=${#NORMALIZED_MODULES[@]} |
| 721 | echo "" |
| 722 | echo "Installing $TOTAL app(s) to $TARGET_SERIAL..." |
| 723 | idx=0 |
| 724 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 725 | mod="${NORMALIZED_MODULES[$idx]}" |
| 726 | cur=$((idx + 1)) |
| 727 | if apk="$(built_apk "$mod")"; then |
| 728 | install_apk_via_session "$apk" "$(module_application_id "$mod")" "$cur" "$TOTAL" || FAILURES=$((FAILURES + 1)) |
| 729 | else |
| 730 | echo "[$cur/$TOTAL] Error: no APK found for :${mod} (assemble failed?)." |
| 731 | FAILURES=$((FAILURES + 1)) |
| 732 | fi |
| 733 | idx=$((idx + 1)) |
| 734 | done |
| 735 | if [[ $FAILURES -gt 0 ]]; then |
| 736 | echo "" |
| 737 | echo "Install complete: $((TOTAL - FAILURES))/$TOTAL succeeded, $FAILURES failed." |
| 738 | exit 1 |
| 739 | fi |
| 740 | echo "" |
| 741 | echo "Install complete: $TOTAL/$TOTAL installed successfully." |
| 742 | exit $BUILD_EXIT |