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