Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / install
install973 lines · 37.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 | - Skips the push+install for an app whose built APK is byte-identical to |
| 37 | the one already installed (size + SHA-256 compared against the on-device |
| 38 | base.apk, probed for every package in one device query). Identical bytes |
| 39 | mean identical versionCode and signature, so skipping cannot bypass the |
| 40 | versionCodeOverride or fs-verity rules; any doubt (not installed, split |
| 41 | APKs, probe/hash failure) installs normally. Skipped apps are reported |
| 42 | separately from installed ones. INSTALL_SKIP_UNCHANGED=0 disables this. |
| 43 | |
| 44 | Examples: |
| 45 | ./install release contacts -> :contacts:assembleRelease + session install |
| 46 | ./install release games:voxels -> :games:voxels:assembleRelease + session install |
| 47 | ./install dev games/voxels -> :games:voxels:assembleDev (slash normalized) |
| 48 | ./install dev :games:voxels: -> :games:voxels:assembleDev (colon trimming) |
| 49 | ./install voxels -> :games:voxels:assembleRelease (shorthand auto games:) |
| 50 | ./install chess -> :games:chess:assembleRelease (shorthand auto games:) |
| 51 | ./install dooraccess -> :personal:dooraccess:assembleRelease (shorthand auto personal:) |
| 52 | ./install all -> :contacts:assembleRelease :games:chess:assembleRelease ... (all modules) |
| 53 | ./install release all -> same as above with release variant |
| 54 | ./install contacts calendar -> :contacts:assembleRelease :calendar:assembleRelease |
| 55 | ./install dev contacts -> :contacts:assembleDev + session install |
| 56 | ./install contacts dev -> :contacts:assembleDev (variant anywhere) |
| 57 | ./install 9 maps -> :maps:assembleRelease + session install to the Pixel 9 |
| 58 | ./install dev 8 maps -> :maps:assembleDev + session install to the Pixel 8 |
| 59 | ./install --help |
| 60 | ./install --dry-run contacts # prints what would run without executing gradle |
| 61 | ./install --dry-run all # preview all modules |
| 62 | |
| 63 | Variant rules: |
| 64 | - release (default): assembleRelease + session install unless explicitly asked for dev |
| 65 | - dev: assembleDev + session install when explicitly requested |
| 66 | - debug: BLOCKED - never use assembleDebug |
| 67 | |
| 68 | Notes: |
| 69 | - NEVER uninstalls an app (no uninstall tasks) |
| 70 | - Validates modules against app modules (those applying common-conventions-app) |
| 71 | - Installs to the first connected device whose serial does not start with 'emulator' |
| 72 | (unless 8|9 selects the Pixel 8 or Pixel 9) |
| 73 | - Set INSTALL_DRY_RUN=1 or pass --dry-run for dry-run mode |
| 74 | - INSTALL_JOBS=N: parallel session installs (default 4). INSTALL_SKIP_UNCHANGED=0: |
| 75 | always push+install, never skip byte-identical APKs. |
| 76 | |
| 77 | EOF |
| 78 | } |
| 79 | |
| 80 | normalize_module() { |
| 81 | local m="$1" |
| 82 | m="$(echo "$m" | xargs)" |
| 83 | m="$(echo "$m" | sed -e 's|^[/:]*||' -e 's|[/:]*$||')" |
| 84 | m="$(echo "$m" | tr '/' ':')" |
| 85 | while echo "$m" | grep -q "::"; do |
| 86 | m="$(echo "$m" | sed 's/::/:/g')" |
| 87 | done |
| 88 | m="$(echo "$m" | tr '[:upper:]' '[:lower:]')" |
| 89 | echo "$m" |
| 90 | } |
| 91 | |
| 92 | # Filesystem dir (relative to $ROOT) for a colon Gradle module. Most modules |
| 93 | # live at their Gradle path (games:voxels -> games/voxels), but a module may |
| 94 | # be remapped in settings.gradle.kts, e.g. |
| 95 | # project(":watch:wear").projectDir = file("watch-wear") |
| 96 | # so resolve through those remappings instead of assuming path == Gradle path. |
| 97 | module_fs_path() { |
| 98 | local mod="$1" settings_file="$ROOT/settings.gradle.kts" remapped="" |
| 99 | if [[ -f "$settings_file" ]]; then |
| 100 | remapped="$(grep -F "project(\":$mod\").projectDir" "$settings_file" 2>/dev/null \ |
| 101 | | sed -nE 's/.*file\("([^"]+)"\).*/\1/p' | head -1)" |
| 102 | fi |
| 103 | if [[ -n "$remapped" ]]; then |
| 104 | echo "$remapped" |
| 105 | else |
| 106 | echo "$mod" | tr ':' '/' |
| 107 | fi |
| 108 | } |
| 109 | |
| 110 | # Inverse of module_fs_path: filesystem dir -> colon Gradle module. |
| 111 | fs_path_to_module() { |
| 112 | local fs_path="$1" settings_file="$ROOT/settings.gradle.kts" remapped="" |
| 113 | if [[ -f "$settings_file" ]]; then |
| 114 | remapped="$(grep -E 'project\(":[^"]+"\)\.projectDir' "$settings_file" 2>/dev/null \ |
| 115 | | sed -nE 's/.*project\(":([^"]+)"\).*file\("([^"]+)"\).*/\2 \1/p' \ |
| 116 | | awk -v p="$fs_path" '$1 == p { print $2; exit }')" |
| 117 | fi |
| 118 | if [[ -n "$remapped" ]]; then |
| 119 | echo "$remapped" |
| 120 | else |
| 121 | echo "$fs_path" | tr '/' ':' |
| 122 | fi |
| 123 | } |
| 124 | |
| 125 | # True when a build file applies an installable app convention (phone app or |
| 126 | # its Wear OS companion, e.g. :watch:wear via common-conventions-wear). |
| 127 | is_installable_build_file() { |
| 128 | grep -qF 'id("common-conventions-app")' "$1" 2>/dev/null \ |
| 129 | || grep -qF 'id("common-conventions-wear")' "$1" 2>/dev/null |
| 130 | } |
| 131 | |
| 132 | get_app_modules_slash() { |
| 133 | local settings_file="$ROOT/settings.gradle.kts" |
| 134 | if [[ ! -f "$settings_file" ]]; then |
| 135 | return |
| 136 | fi |
| 137 | grep -E 'include\(":[^"]+"\)' "$settings_file" 2>/dev/null \ |
| 138 | | sed -E 's/.*include\(":([^"]+)"\).*/\1/' \ |
| 139 | | while IFS= read -r colon_mod; do |
| 140 | case "$colon_mod" in "" ) continue;; esac |
| 141 | p="$(module_fs_path "$colon_mod")" |
| 142 | if [[ -f "$ROOT/$p/build.gradle.kts" ]] && is_installable_build_file "$ROOT/$p/build.gradle.kts"; then |
| 143 | echo "$p" |
| 144 | fi |
| 145 | done | sort -u |
| 146 | } |
| 147 | |
| 148 | print_valid_modules() { |
| 149 | local slash_modules |
| 150 | slash_modules="$(get_app_modules_slash)" |
| 151 | if [[ -z "$slash_modules" ]]; then |
| 152 | echo " (could not discover app modules)" |
| 153 | return |
| 154 | fi |
| 155 | echo "" |
| 156 | echo "Valid app modules (slash notation for filesystem, colon for Gradle):" |
| 157 | echo "" |
| 158 | printf " %-30s %s\n" "Filesystem (slash)" "Gradle (colon)" |
| 159 | printf " %-30s %s\n" "--------------------" "-------------------" |
| 160 | while IFS= read -r sm; do |
| 161 | case "$sm" in "" ) continue;; esac |
| 162 | colon_mod="$(fs_path_to_module "$sm")" |
| 163 | printf " %-30s :%s\n" "$sm" "$colon_mod" |
| 164 | done <<< "$slash_modules" |
| 165 | echo "" |
| 166 | echo "Tip: Use either notation: ./install dev games/voxels OR ./install dev games:voxels" |
| 167 | echo "Shorthand: ./install dev voxels (auto-expands to games:voxels if games/<name> exists)" |
| 168 | echo " ./install dev dooraccess (auto-expands to personal:dooraccess if personal/<name> exists)" |
| 169 | echo "All: ./install all (installs every app module)" |
| 170 | } |
| 171 | |
| 172 | array_contains() { |
| 173 | local needle="$1" |
| 174 | shift |
| 175 | local item |
| 176 | for item in "$@"; do |
| 177 | if [[ "$item" == "$needle" ]]; then |
| 178 | return 0 |
| 179 | fi |
| 180 | done |
| 181 | return 1 |
| 182 | } |
| 183 | |
| 184 | # --- Handle zero args early (no array expansion needed) --- |
| 185 | if [[ $# -eq 0 ]]; then |
| 186 | print_help |
| 187 | echo "" |
| 188 | echo "Error: No modules specified." |
| 189 | print_valid_modules |
| 190 | exit 1 |
| 191 | fi |
| 192 | |
| 193 | # --- Detect --help/-h/help anywhere (direct $@ loop, no array) --- |
| 194 | for arg in "$@"; do |
| 195 | case "$arg" in |
| 196 | --help|-h|help) |
| 197 | print_help |
| 198 | exit 0 |
| 199 | ;; |
| 200 | esac |
| 201 | done |
| 202 | |
| 203 | # --- Early DRY_RUN detection --- |
| 204 | DRY_RUN=0 |
| 205 | if [[ "${INSTALL_DRY_RUN:-0}" == "1" ]]; then |
| 206 | DRY_RUN=1 |
| 207 | fi |
| 208 | for arg in "$@"; do |
| 209 | if [[ "$arg" == "--dry-run" ]]; then |
| 210 | DRY_RUN=1 |
| 211 | fi |
| 212 | done |
| 213 | |
| 214 | # --- Build FILTERED_ARGS (exclude --dry-run) --- |
| 215 | FILTERED_ARGS=() |
| 216 | for arg in "$@"; do |
| 217 | if [[ "$arg" == "--dry-run" ]]; then |
| 218 | continue |
| 219 | fi |
| 220 | FILTERED_ARGS+=("$arg") |
| 221 | done |
| 222 | |
| 223 | # After filtering, if empty (e.g., only --dry-run was passed) |
| 224 | if [[ ${#FILTERED_ARGS[@]} -eq 0 ]]; then |
| 225 | print_help |
| 226 | echo "" |
| 227 | echo "Error: No modules specified (dry-run mode)." |
| 228 | print_valid_modules |
| 229 | exit 1 |
| 230 | fi |
| 231 | |
| 232 | # --- Device hint: bare 8|9 anywhere, or --device 8|9 / --device=8|9 (optional) --- |
| 233 | DEVICE_HINT="" |
| 234 | PRESCAN_ARGS=() |
| 235 | idx=0 |
| 236 | while [[ $idx -lt ${#FILTERED_ARGS[@]} ]]; do |
| 237 | arg="${FILTERED_ARGS[$idx]}" |
| 238 | lc="$(echo "$arg" | tr '[:upper:]' '[:lower:]')" |
| 239 | if [[ "$lc" == "--device" ]]; then |
| 240 | if [[ $((idx + 1)) -ge ${#FILTERED_ARGS[@]} ]]; then |
| 241 | echo "Error: --device requires a value: 8 (Pixel 8), 9 (Pixel 9) or watch (Pixel Watch)." |
| 242 | exit 1 |
| 243 | fi |
| 244 | DEVICE_HINT="$(echo "${FILTERED_ARGS[$((idx + 1))]}" | tr '[:upper:]' '[:lower:]')" |
| 245 | idx=$((idx + 2)) |
| 246 | continue |
| 247 | elif [[ "$lc" == --device=* ]]; then |
| 248 | DEVICE_HINT="${lc#--device=}" |
| 249 | idx=$((idx + 1)) |
| 250 | continue |
| 251 | elif [[ "$lc" == "8" || "$lc" == "9" ]]; then |
| 252 | DEVICE_HINT="$lc" |
| 253 | idx=$((idx + 1)) |
| 254 | continue |
| 255 | elif [[ "$lc" =~ ^[0-9]+$ ]]; then |
| 256 | echo "Error: Invalid device '$arg'. Use 8 (Pixel 8), 9 (Pixel 9) or --device=watch." |
| 257 | exit 1 |
| 258 | fi |
| 259 | PRESCAN_ARGS+=("$arg") |
| 260 | idx=$((idx + 1)) |
| 261 | done |
| 262 | FILTERED_ARGS=("${PRESCAN_ARGS[@]}") |
| 263 | if [[ -n "$DEVICE_HINT" && "$DEVICE_HINT" != "8" && "$DEVICE_HINT" != "9" && "$DEVICE_HINT" != "watch" ]]; then |
| 264 | echo "Error: Invalid device '$DEVICE_HINT'. Use 8 (Pixel 8), 9 (Pixel 9) or watch (Pixel Watch)." |
| 265 | exit 1 |
| 266 | fi |
| 267 | if [[ -z "$DEVICE_HINT" ]]; then |
| 268 | echo "Error: a target device is required: pass 8 (Pixel 8), 9 (Pixel 9) or --device=watch (Pixel Watch); there is no default device." |
| 269 | exit 1 |
| 270 | fi |
| 271 | |
| 272 | # --- Arg parsing: variant detection anywhere --- |
| 273 | VARIANT_LC="" |
| 274 | VARIANT_COUNT=0 |
| 275 | MODULES_RAW=() |
| 276 | |
| 277 | idx=0 |
| 278 | while [[ $idx -lt ${#FILTERED_ARGS[@]} ]]; do |
| 279 | arg="${FILTERED_ARGS[$idx]}" |
| 280 | lc="$(echo "$arg" | tr '[:upper:]' '[:lower:]')" |
| 281 | if [[ "$lc" == "dev" || "$lc" == "release" || "$lc" == "debug" ]]; then |
| 282 | if [[ -z "$VARIANT_LC" ]]; then |
| 283 | VARIANT_LC="$lc" |
| 284 | VARIANT_COUNT=1 |
| 285 | else |
| 286 | if [[ "$VARIANT_LC" != "$lc" ]]; then |
| 287 | echo "Error: Multiple distinct variants specified: '$VARIANT_LC' and '$lc'." |
| 288 | echo "Please specify only one variant: dev or release." |
| 289 | echo "" |
| 290 | print_help |
| 291 | exit 1 |
| 292 | fi |
| 293 | VARIANT_COUNT=$((VARIANT_COUNT + 1)) |
| 294 | fi |
| 295 | else |
| 296 | MODULES_RAW+=("$arg") |
| 297 | fi |
| 298 | idx=$((idx + 1)) |
| 299 | done |
| 300 | |
| 301 | if [[ -z "$VARIANT_LC" ]]; then |
| 302 | VARIANT_LC="release" |
| 303 | fi |
| 304 | |
| 305 | if [[ "$VARIANT_LC" == "debug" ]]; then |
| 306 | echo "Error: 'debug' variant is blocked." |
| 307 | echo "" |
| 308 | echo " - Never assemble or install a debug variant" |
| 309 | echo " - Always install the release build, unless specifically asked for dev" |
| 310 | echo "" |
| 311 | echo "Use 'release' (default) or 'dev' instead:" |
| 312 | echo " ./install <module>" |
| 313 | echo " ./install dev <module>" |
| 314 | exit 1 |
| 315 | fi |
| 316 | |
| 317 | if [[ ${#MODULES_RAW[@]} -eq 0 ]]; then |
| 318 | echo "Error: No modules specified (only variant '$VARIANT_LC' found)." |
| 319 | echo "" |
| 320 | print_help |
| 321 | print_valid_modules |
| 322 | exit 1 |
| 323 | fi |
| 324 | |
| 325 | # --- Normalization + Dedup + Shorthand + "all" + auto games: prefix --- |
| 326 | NORMALIZED_MODULES=() |
| 327 | ALL_EXPANDED=0 |
| 328 | |
| 329 | idx=0 |
| 330 | while [[ $idx -lt ${#MODULES_RAW[@]} ]]; do |
| 331 | raw="${MODULES_RAW[$idx]}" |
| 332 | norm="$(normalize_module "$raw")" |
| 333 | if [[ -z "$norm" ]]; then |
| 334 | echo "Warning: Skipping empty/invalid module token '$raw' after normalization." |
| 335 | idx=$((idx + 1)) |
| 336 | continue |
| 337 | fi |
| 338 | |
| 339 | # Handle "all" keyword - expand to every app module |
| 340 | if [[ "$norm" == "all" ]]; then |
| 341 | if [[ $ALL_EXPANDED -eq 0 ]]; then |
| 342 | echo "Info: Expanding 'all' to all app modules..." |
| 343 | all_slash="$(get_app_modules_slash)" |
| 344 | if [[ -z "$all_slash" ]]; then |
| 345 | echo "Error: Could not discover app modules for 'all' expansion." |
| 346 | print_valid_modules |
| 347 | exit 1 |
| 348 | fi |
| 349 | while IFS= read -r sm; do |
| 350 | case "$sm" in "" ) continue;; esac |
| 351 | colon_mod="$(fs_path_to_module "$sm")" |
| 352 | # dedup for expanded modules |
| 353 | dup=0 |
| 354 | j=0 |
| 355 | while [[ $j -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 356 | if [[ "${NORMALIZED_MODULES[$j]}" == "$colon_mod" ]]; then |
| 357 | dup=1 |
| 358 | break |
| 359 | fi |
| 360 | j=$((j + 1)) |
| 361 | done |
| 362 | if [[ $dup -eq 0 ]]; then |
| 363 | NORMALIZED_MODULES+=("$colon_mod") |
| 364 | fi |
| 365 | done <<< "$all_slash" |
| 366 | ALL_EXPANDED=1 |
| 367 | fi |
| 368 | idx=$((idx + 1)) |
| 369 | continue |
| 370 | fi |
| 371 | |
| 372 | # Auto games:/personal: prefix - if token has no colon and games/<name> or personal/<name> is a valid app module, prefix it |
| 373 | # This allows ./install voxels -> games:voxels, ./install chess -> games:chess, ./install dooraccess -> personal:dooraccess etc. |
| 374 | if [[ "$norm" != *":"* ]]; then |
| 375 | for auto_prefix in games personal; do |
| 376 | pref_path="$ROOT/$auto_prefix/$norm" |
| 377 | if [[ -f "$pref_path/build.gradle.kts" ]] && grep -qF 'id("common-conventions-app")' "$pref_path/build.gradle.kts" 2>/dev/null; then |
| 378 | expanded="$auto_prefix:$norm" |
| 379 | echo "Info: Expanding shorthand '$raw' ($norm) -> $expanded (found $auto_prefix/$norm)" |
| 380 | norm="$expanded" |
| 381 | break |
| 382 | elif [[ ! -e "$ROOT/$norm" && -e "$ROOT/$auto_prefix/$norm" ]]; then |
| 383 | # Fallback: if root/<name> doesn't exist but <prefix>/<name> does (backward compat) |
| 384 | expanded="$auto_prefix:$norm" |
| 385 | echo "Info: Expanding shorthand '$raw' ($norm) -> $expanded (found $auto_prefix/$norm)" |
| 386 | norm="$expanded" |
| 387 | break |
| 388 | fi |
| 389 | done |
| 390 | fi |
| 391 | |
| 392 | # Dedup via index loop (bash 3.2 safe) |
| 393 | dup=0 |
| 394 | j=0 |
| 395 | while [[ $j -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 396 | if [[ "${NORMALIZED_MODULES[$j]}" == "$norm" ]]; then |
| 397 | dup=1 |
| 398 | break |
| 399 | fi |
| 400 | j=$((j + 1)) |
| 401 | done |
| 402 | |
| 403 | if [[ $dup -eq 0 ]]; then |
| 404 | NORMALIZED_MODULES+=("$norm") |
| 405 | fi |
| 406 | idx=$((idx + 1)) |
| 407 | done |
| 408 | |
| 409 | if [[ ${#NORMALIZED_MODULES[@]} -eq 0 ]]; then |
| 410 | echo "Error: No valid modules after normalization." |
| 411 | print_valid_modules |
| 412 | exit 1 |
| 413 | fi |
| 414 | |
| 415 | # --- Validation --- |
| 416 | INVALID_MODULES=() |
| 417 | idx=0 |
| 418 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 419 | mod="${NORMALIZED_MODULES[$idx]}" |
| 420 | fs_path="$(module_fs_path "$mod")" |
| 421 | full_path="$ROOT/$fs_path" |
| 422 | if [[ ! -f "$full_path/build.gradle.kts" ]]; then |
| 423 | INVALID_MODULES+=("$mod (not found: $fs_path/build.gradle.kts)") |
| 424 | else |
| 425 | if ! is_installable_build_file "$full_path/build.gradle.kts"; then |
| 426 | INVALID_MODULES+=("$mod (exists but is not an app module - missing common-conventions-app)") |
| 427 | fi |
| 428 | fi |
| 429 | idx=$((idx + 1)) |
| 430 | done |
| 431 | |
| 432 | if [[ ${#INVALID_MODULES[@]} -gt 0 ]]; then |
| 433 | echo "Error: Some modules are invalid:" |
| 434 | idx=0 |
| 435 | while [[ $idx -lt ${#INVALID_MODULES[@]} ]]; do |
| 436 | echo " - ${INVALID_MODULES[$idx]}" |
| 437 | idx=$((idx + 1)) |
| 438 | done |
| 439 | print_valid_modules |
| 440 | exit 1 |
| 441 | fi |
| 442 | |
| 443 | # --- Build Gradle tasks (assemble only; install happens below via adb sessions) --- |
| 444 | VARIANT_CAP="$(echo "$VARIANT_LC" | awk '{print toupper(substr($0,1,1)) substr($0,2)}')" |
| 445 | TASKS=() |
| 446 | idx=0 |
| 447 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 448 | mod="${NORMALIZED_MODULES[$idx]}" |
| 449 | TASKS+=(":${mod}:assemble${VARIANT_CAP}") |
| 450 | idx=$((idx + 1)) |
| 451 | done |
| 452 | |
| 453 | # Safe joining without ${ARRAY[*]} under set -u when empty (should not be empty here, but guard) |
| 454 | modules_str="" |
| 455 | tasks_str="" |
| 456 | idx=0 |
| 457 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 458 | if [[ $idx -gt 0 ]]; then |
| 459 | modules_str="$modules_str ${NORMALIZED_MODULES[$idx]}" |
| 460 | else |
| 461 | modules_str="${NORMALIZED_MODULES[$idx]}" |
| 462 | fi |
| 463 | idx=$((idx + 1)) |
| 464 | done |
| 465 | idx=0 |
| 466 | while [[ $idx -lt ${#TASKS[@]} ]]; do |
| 467 | if [[ $idx -gt 0 ]]; then |
| 468 | tasks_str="$tasks_str ${TASKS[$idx]}" |
| 469 | else |
| 470 | tasks_str="${TASKS[$idx]}" |
| 471 | fi |
| 472 | idx=$((idx + 1)) |
| 473 | done |
| 474 | |
| 475 | echo "Variant: $VARIANT_LC (assemble + adb session install)" |
| 476 | echo "Modules (${#NORMALIZED_MODULES[@]}): $modules_str" |
| 477 | echo "Gradle tasks: $tasks_str" |
| 478 | echo "" |
| 479 | |
| 480 | # --- Device selection: first connected device whose serial doesn't start with 'emulator' --- |
| 481 | ADB_BIN="" |
| 482 | if command -v adb >/dev/null 2>&1; then |
| 483 | ADB_BIN="adb" |
| 484 | else |
| 485 | for adb_base in "${ANDROID_HOME:-}" "${ANDROID_SDK_ROOT:-}" "$HOME/Android/Sdk" "$HOME/Library/Android/sdk"; do |
| 486 | if [[ -n "$adb_base" && -x "$adb_base/platform-tools/adb" ]]; then |
| 487 | ADB_BIN="$adb_base/platform-tools/adb" |
| 488 | break |
| 489 | fi |
| 490 | done |
| 491 | fi |
| 492 | |
| 493 | TARGET_SERIAL="" |
| 494 | if [[ -n "$ADB_BIN" ]]; then |
| 495 | # hint '' = first non-emulator device; '8' = model Pixel_8; |
| 496 | # '9' = model Pixel_9* (covers Pixel_9 and Pixel_9_Pro_XL). |
| 497 | while read -r dev_line; do |
| 498 | dev_serial="$(echo "$dev_line" | awk '{print $1}')" |
| 499 | dev_state="$(echo "$dev_line" | awk '{print $2}')" |
| 500 | [[ "$dev_state" == "device" ]] || continue |
| 501 | case "$dev_serial" in emulator*) continue;; esac |
| 502 | case "$dev_serial" in "List"*) continue;; esac |
| 503 | if [[ -z "$DEVICE_HINT" ]]; then |
| 504 | TARGET_SERIAL="$dev_serial" |
| 505 | break |
| 506 | fi |
| 507 | dev_model="$(echo "$dev_line" | sed -nE 's/.*model:([^[:space:]]+).*/\1/p')" |
| 508 | if [[ "$DEVICE_HINT" == "8" && "$dev_model" == "Pixel_8" ]]; then |
| 509 | TARGET_SERIAL="$dev_serial" |
| 510 | break |
| 511 | fi |
| 512 | if [[ "$DEVICE_HINT" == "9" && "$dev_model" == Pixel_9* ]]; then |
| 513 | TARGET_SERIAL="$dev_serial" |
| 514 | break |
| 515 | fi |
| 516 | if [[ "$DEVICE_HINT" == "watch" && "$dev_model" == *Watch* ]]; then |
| 517 | TARGET_SERIAL="$dev_serial" |
| 518 | break |
| 519 | fi |
| 520 | done < <("$ADB_BIN" devices -l 2>/dev/null) |
| 521 | fi |
| 522 | |
| 523 | if [[ -z "$ADB_BIN" ]]; then |
| 524 | if [[ $DRY_RUN -eq 1 ]]; then |
| 525 | echo "[DRY-RUN] Warning: adb not found; a device could not be selected." |
| 526 | else |
| 527 | echo "Error: adb not found; cannot select a target device." |
| 528 | echo "Install Android platform-tools and put adb on PATH (or set ANDROID_HOME)." |
| 529 | exit 1 |
| 530 | fi |
| 531 | elif [[ -n "$TARGET_SERIAL" ]]; then |
| 532 | export ANDROID_SERIAL="$TARGET_SERIAL" |
| 533 | if [[ -n "$DEVICE_HINT" ]]; then |
| 534 | echo "Target device: $ANDROID_SERIAL (Pixel $DEVICE_HINT)" |
| 535 | else |
| 536 | echo "Target device: $ANDROID_SERIAL (first connected device not starting with 'emulator')" |
| 537 | fi |
| 538 | elif [[ -n "$DEVICE_HINT" ]]; then |
| 539 | echo "Error: No connected device matching '$DEVICE_HINT' found (check 'adb devices')." |
| 540 | exit 1 |
| 541 | elif [[ $DRY_RUN -eq 1 ]]; then |
| 542 | echo "[DRY-RUN] Warning: no non-emulator device connected; ANDROID_SERIAL not set." |
| 543 | else |
| 544 | echo "Error: No non-emulator device connected." |
| 545 | echo "Connect a physical device (check 'adb devices'); refusing to install to an emulator." |
| 546 | exit 1 |
| 547 | fi |
| 548 | |
| 549 | # applicationId for a module (all are string literals in build.gradle.kts). |
| 550 | module_application_id() { |
| 551 | sed -nE 's/^[[:space:]]*applicationId[[:space:]]*=[[:space:]]*"([^"]+)".*/\1/p' \ |
| 552 | "$ROOT/$(module_fs_path "$1")/build.gradle.kts" | head -1 |
| 553 | } |
| 554 | |
| 555 | # APK built for a module; output-metadata.json names the file authoritatively |
| 556 | # (nested modules like cast:tv build tv-dev.apk, not cast-tv-dev.apk). |
| 557 | built_apk() { |
| 558 | local mod="$1" fs_path out_dir meta base_name file_name |
| 559 | fs_path="$(module_fs_path "$mod")" |
| 560 | out_dir="$ROOT/$fs_path/build/outputs/apk/$VARIANT_LC" |
| 561 | meta="$out_dir/output-metadata.json" |
| 562 | base_name="${mod##*:}" |
| 563 | file_name="$base_name-$VARIANT_LC.apk" |
| 564 | if [[ -f "$meta" ]]; then |
| 565 | local found |
| 566 | found="$(grep -oE '"outputFile"[[:space:]]*:[[:space:]]*"[^"]+"' "$meta" | head -1 | sed -E 's/.*\"([^\"]+)\"$/\1/')" |
| 567 | [[ -n "$found" ]] && file_name="$found" |
| 568 | fi |
| 569 | if [[ -f "$out_dir/$file_name" ]]; then |
| 570 | echo "$out_dir/$file_name" |
| 571 | return 0 |
| 572 | fi |
| 573 | return 1 |
| 574 | } |
| 575 | |
| 576 | # Run one adb command against the target device, riding out a dropped |
| 577 | # network-adb transport: the forwarded endpoint the phone hangs off |
| 578 | # (127.0.0.1:15556) loses the device for a few seconds at a time under |
| 579 | # install load, and without this a single drop fails every remaining app |
| 580 | # in the run. Combined output goes to stdout; reconnect notes and the |
| 581 | # final error go to stderr (callers used to discard adb's stderr entirely, |
| 582 | # which hid exactly this failure). |
| 583 | adb_dev() { |
| 584 | local attempt out |
| 585 | for attempt in 1 2 3 4; do |
| 586 | out="$("$ADB_BIN" -s "$TARGET_SERIAL" "$@" 2>&1)" && { printf '%s' "$out"; return 0; } |
| 587 | if echo "$out" | grep -qiE "device '[^']*' not found|device offline|no devices/emulators"; then |
| 588 | echo " adb: transport to $TARGET_SERIAL lost (attempt $attempt); reconnecting..." >&2 |
| 589 | "$ADB_BIN" connect "$TARGET_SERIAL" >/dev/null 2>&1 |
| 590 | # Bounded wait: a tunnel that stays down must fail this app, not |
| 591 | # freeze the whole run on wait-for-device. |
| 592 | local waited=0 |
| 593 | while [[ $waited -lt 30 ]]; do |
| 594 | [[ "$("$ADB_BIN" -s "$TARGET_SERIAL" get-state 2>/dev/null)" == "device" ]] && break |
| 595 | sleep 2; waited=$((waited + 2)) |
| 596 | done |
| 597 | continue |
| 598 | fi |
| 599 | printf '%s\n' "$out" >&2 |
| 600 | return 1 |
| 601 | done |
| 602 | printf '%s\n' "$out" >&2 |
| 603 | return 1 |
| 604 | } |
| 605 | |
| 606 | # on-device versionCode for a package, empty when not installed. |
| 607 | device_version_code() { |
| 608 | # No `head -1` here: with pipefail, head closing the pipe SIGPIPEs adb and |
| 609 | # the failing assignment kills the script (set -e) after the first module. |
| 610 | adb_dev shell pm list packages --show-versioncode "$1" \ |
| 611 | | sed -nE "s/^package:${1//./\\.}[[:space:]]+versionCode:([0-9]+).*/\\1/p" |
| 612 | } |
| 613 | |
| 614 | # exit 0 when the package is installed as a system app. |
| 615 | is_system_app() { |
| 616 | adb_dev shell pm list packages -s "$1" \ |
| 617 | | tr -d '\r' | grep -qFx "package:$1" |
| 618 | } |
| 619 | |
| 620 | install_apk_via_session() { |
| 621 | local apk="$1" package_id="$2" cur="${3:-}" total="${4:-}" mod="${5:-}" |
| 622 | local apk_name apk_size apk_mb session device_apk idsig device_idsig commit_out prefix |
| 623 | local t_start t_pushed t_done push_secs total_secs push_rate mod_slug |
| 624 | apk_name="$(basename "$apk")" |
| 625 | apk_size="$(wc -c < "$apk" | tr -d ' ')" |
| 626 | apk_mb="$(awk "BEGIN {printf \"%.1f\", $apk_size/1048576}")" |
| 627 | # Stage under a per-module name: :watch and :watch:wear deliberately share |
| 628 | # applicationId com.vayunmathur.watch, so a package-only staging name made |
| 629 | # the two overwrite each other's pushed APK whenever they landed in the |
| 630 | # same parallel wave (and the loser's cleanup rm deleted the winner's file |
| 631 | # mid-install). The module slug keeps every staging file unique. |
| 632 | mod_slug="$(printf '%s' "${mod:-$package_id}" | tr ':/' '__')" |
| 633 | device_apk="/data/local/tmp/install_${package_id}_${mod_slug}.apk" |
| 634 | device_idsig="$device_apk.idsig" |
| 635 | t_start="$(date +%s)" |
| 636 | if [[ -n "$cur" && -n "$total" ]]; then |
| 637 | prefix="[$cur/$total] " |
| 638 | else |
| 639 | prefix="" |
| 640 | fi |
| 641 | echo "${prefix}Installing $package_id ($apk_name, ${apk_mb} MB)..." |
| 642 | echo " ${prefix}[1/4] Pushing APK to device..." |
| 643 | adb_dev push "$apk" "$device_apk" >/dev/null || { |
| 644 | echo " ${prefix}Error: adb push failed for $apk_name."; return 1 |
| 645 | } |
| 646 | t_pushed="$(date +%s)" |
| 647 | echo " ${prefix}[2/4] Creating install session..." |
| 648 | session="$(adb_dev shell pm install-create -r \ |
| 649 | | sed -nE 's/.*created install session \[([0-9]+)\].*/\1/p')" |
| 650 | if [[ -z "$session" ]]; then |
| 651 | echo " ${prefix}Error: could not create install session." |
| 652 | adb_dev shell rm -f "$device_apk" >/dev/null |
| 653 | return 1 |
| 654 | fi |
| 655 | # Stage the APK first, then its .idsig sidecar (V4 signature) under the |
| 656 | # matching name. PackageManager enables fs-verity from a staged idsig, which |
| 657 | # MAOS/GrapheneOS requires for updates to preinstalled system apps. Plain |
| 658 | # `adb install` never stages the sidecar, so those updates always fail. |
| 659 | echo " ${prefix}[3/4] Staging APK into session $session..." |
| 660 | adb_dev shell pm install-write -S "$apk_size" "$session" base.apk "$device_apk" >/dev/null || { |
| 661 | echo " ${prefix}Error: failed to stage $apk_name into session $session." |
| 662 | adb_dev shell pm install-abandon "$session" >/dev/null |
| 663 | adb_dev shell rm -f "$device_apk" >/dev/null |
| 664 | return 1 |
| 665 | } |
| 666 | idsig="$apk.idsig" |
| 667 | if [[ -f "$idsig" ]]; then |
| 668 | echo " ${prefix}Staging .idsig sidecar..." |
| 669 | if adb_dev push "$idsig" "$device_idsig" >/dev/null; then |
| 670 | adb_dev shell pm install-write -S "$(wc -c < "$idsig" | tr -d ' ')" \ |
| 671 | "$session" base.apk.idsig "$device_idsig" >/dev/null || \ |
| 672 | echo " ${prefix}Warning: could not stage .idsig; system-app updates may fail fs-verity." |
| 673 | else |
| 674 | echo " ${prefix}Warning: could not push .idsig; system-app updates may fail fs-verity." |
| 675 | fi |
| 676 | # The staged sidecar is copied into the session by install-write, so its |
| 677 | # tmp file only needs deleting once, together with the APK after commit |
| 678 | # (one round trip instead of two). |
| 679 | else |
| 680 | echo " ${prefix}Warning: no .idsig sidecar next to the APK; system-app updates may fail fs-verity." |
| 681 | fi |
| 682 | echo " ${prefix}[4/4] Committing install session $session..." |
| 683 | commit_out="$(adb_dev shell pm install-commit "$session")" |
| 684 | adb_dev shell rm -f "$device_apk" "$device_idsig" >/dev/null |
| 685 | t_done="$(date +%s)" |
| 686 | push_secs=$((t_pushed - t_start)); if [[ $push_secs -lt 1 ]]; then push_secs=1; fi |
| 687 | total_secs=$((t_done - t_start)); if [[ $total_secs -lt 1 ]]; then total_secs=1; fi |
| 688 | push_rate="$(awk "BEGIN {printf \"%.1f\", $apk_size/1048576/$push_secs}")" |
| 689 | # `pm install-commit` prints "Success" but exits 0 either way; fail on absence. |
| 690 | if ! echo "$commit_out" | grep -qi "^Success"; then |
| 691 | echo " ${prefix}Error: install failed for $package_id: $commit_out" |
| 692 | return 1 |
| 693 | fi |
| 694 | if [[ -n "$cur" && -n "$total" ]]; then |
| 695 | echo " Installed $package_id in ${total_secs}s (push ${push_secs}s @ ${push_rate} MB/s). [$cur/$total done]" |
| 696 | else |
| 697 | echo " Installed $package_id in ${total_secs}s (push ${push_secs}s @ ${push_rate} MB/s)." |
| 698 | fi |
| 699 | } |
| 700 | |
| 701 | # --- Extra Gradle args: --continue so one broken module doesn't block the rest --- |
| 702 | GRADLE_ARGS=(--continue) |
| 703 | gradle_args_str=" --continue" |
| 704 | if [[ "$VARIANT_LC" == "release" && $ALL_EXPANDED -eq 1 ]]; then |
| 705 | # Match the repo-wide R8 process cap in gradle.properties (48 single-thread |
| 706 | # deterministic processes); a lower cap here only lengthens release builds. |
| 707 | GRADLE_ARGS+=(-P"android.r8.maxWorkers=48") |
| 708 | gradle_args_str="$gradle_args_str -Pandroid.r8.maxWorkers=48" |
| 709 | fi |
| 710 | |
| 711 | # --- Big-build heap: gradle.properties defaults to a small daemon heap sized for |
| 712 | # scoped work. A full-repo build ("all", ~100 modules, concurrent D8 dex merge) |
| 713 | # OOMs on it, so escalate here — and only here (ALL_EXPANDED). -D spawns a |
| 714 | # separate big daemon; the small one stays alive for normal builds. |
| 715 | if [[ $ALL_EXPANDED -eq 1 ]]; then |
| 716 | GRADLE_ARGS+=(-D"org.gradle.jvmargs=-Xmx46144m -Xms4512m -XX:MaxMetaspaceSize=1024m -XX:+UseG1GC -Dfile.encoding=UTF-8") |
| 717 | GRADLE_ARGS+=(--max-workers=128) |
| 718 | # Rust crates build in parallel too (the pinned toolchain is preinstalled |
| 719 | # on the dev box, so no rustup install can race — see RustNative.kt). |
| 720 | GRADLE_ARGS+=(-P"modernapps.cargoParallelism=16") |
| 721 | gradle_args_str="$gradle_args_str -Dorg.gradle.jvmargs=<46g-big-build-heap> --max-workers=128 -Pmodernapps.cargoParallelism=16" |
| 722 | fi |
| 723 | |
| 724 | # --- versionCodeOverride: MAOS refuses to update a system package to the same |
| 725 | # versionCode, so an APK built from the same version.txt as the on-device OS image |
| 726 | # can never be installed over it. Query the device once; for system apps bump to |
| 727 | # one above the on-device version, for user apps just match it exactly |
| 728 | # (version.txt itself stays untouched). |
| 729 | REPO_VERSION="$(sed -n '1p' "$ROOT/version.txt" | tr -d '\r' | xargs)" |
| 730 | NEED_BUMP="$REPO_VERSION" |
| 731 | if [[ -n "$TARGET_SERIAL" ]]; then |
| 732 | # Batch the device queries: one `pm list packages` for every package's |
| 733 | # versionCode and one for the system-package set, instead of two adb |
| 734 | # round trips per module (146 calls for `all`, ~0.3s each over the |
| 735 | # forwarded transport — ~45s of pure latency before Gradle even starts). |
| 736 | # Falls back to the per-package queries if a batch call returns nothing. |
| 737 | ALL_PKG_VERSIONS="$(adb_dev shell pm list packages --show-versioncode 2>/dev/null || true)" |
| 738 | SYSTEM_PKGS="$(adb_dev shell pm list packages -s 2>/dev/null || true)" |
| 739 | batched_version_code() { |
| 740 | printf '%s\n' "$ALL_PKG_VERSIONS" | awk -v k="package:$1" '$1 == k { sub(/^versionCode:/, "", $2); print $2; exit }' |
| 741 | } |
| 742 | batched_is_system_app() { |
| 743 | printf '%s\n' "$SYSTEM_PKGS" | grep -qxF "package:$1" |
| 744 | } |
| 745 | idx=0 |
| 746 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 747 | pkg="$(module_application_id "${NORMALIZED_MODULES[$idx]}")" |
| 748 | if [[ -n "$pkg" ]]; then |
| 749 | if [[ -n "$ALL_PKG_VERSIONS" ]]; then |
| 750 | dev_v="$(batched_version_code "$pkg")" |
| 751 | else |
| 752 | dev_v="$(device_version_code "$pkg")" |
| 753 | fi |
| 754 | if [[ -n "$dev_v" && "$dev_v" -ge "$NEED_BUMP" ]]; then |
| 755 | if { [[ -n "$SYSTEM_PKGS" ]] && batched_is_system_app "$pkg"; } || { [[ -z "$SYSTEM_PKGS" ]] && is_system_app "$pkg"; }; then |
| 756 | target=$((dev_v + 1)) |
| 757 | kind="system app" |
| 758 | else |
| 759 | target=$dev_v |
| 760 | kind="user app" |
| 761 | fi |
| 762 | if [[ "$target" -gt "$NEED_BUMP" ]]; then |
| 763 | NEED_BUMP=$target |
| 764 | echo "Info: $pkg is a $kind at versionCode $dev_v on-device (>= repo $REPO_VERSION); will build with -PversionCodeOverride=$target." |
| 765 | fi |
| 766 | fi |
| 767 | fi |
| 768 | idx=$((idx + 1)) |
| 769 | done |
| 770 | fi |
| 771 | if [[ "$NEED_BUMP" -gt "$REPO_VERSION" ]]; then |
| 772 | GRADLE_ARGS+=(-P"versionCodeOverride=$NEED_BUMP") |
| 773 | gradle_args_str="$gradle_args_str -PversionCodeOverride=$NEED_BUMP" |
| 774 | fi |
| 775 | |
| 776 | if [[ $DRY_RUN -eq 1 ]]; then |
| 777 | echo "[DRY-RUN] Would execute: $SCRIPT_DIR/gradlew $tasks_str$gradle_args_str" |
| 778 | idx=0 |
| 779 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 780 | mod="${NORMALIZED_MODULES[$idx]}" |
| 781 | if apk="$(built_apk "$mod")"; then |
| 782 | echo "[DRY-RUN] Would session-install $(module_application_id "$mod") from $apk" |
| 783 | else |
| 784 | echo "[DRY-RUN] Would session-install $(module_application_id "$mod") (APK expected after assemble)" |
| 785 | fi |
| 786 | idx=$((idx + 1)) |
| 787 | done |
| 788 | exit 0 |
| 789 | fi |
| 790 | echo "Running: $SCRIPT_DIR/gradlew $tasks_str$gradle_args_str" |
| 791 | echo "" |
| 792 | |
| 793 | # Execute with all tasks expanded via index loop (preserving word splitting safety). |
| 794 | # --continue: one failing module must not starve the rest of the build, and the |
| 795 | # installer below already session-installs whichever APKs exist on failure. |
| 796 | set +u |
| 797 | "$SCRIPT_DIR/gradlew" --continue "${TASKS[@]}" "${GRADLE_ARGS[@]}" |
| 798 | BUILD_EXIT=$? |
| 799 | echo "" |
| 800 | |
| 801 | # Install whichever APKs were (re)built, even if sibling modules failed (--continue). |
| 802 | FAILURES=0 |
| 803 | SKIPPED=0 |
| 804 | if [[ $BUILD_EXIT -ne 0 ]]; then |
| 805 | echo "Warning: assemble reported failures; installing whatever APKs exist." |
| 806 | fi |
| 807 | TOTAL=${#NORMALIZED_MODULES[@]} |
| 808 | echo "" |
| 809 | # --- Skip-unchanged probe: one device query for every package ------------- |
| 810 | # Pushing dominates install time (a dev `all` run pushes ~8.4 GB over the |
| 811 | # forwarded adb transport; at the observed ~3.4 MB/s per stream that is most |
| 812 | # of the run). When a rebuild reproduces the exact APK already installed -- |
| 813 | # the common case for modules a change did not touch -- the push, the session |
| 814 | # round trips and the on-device commit are all wasted work. Compare size + |
| 815 | # SHA-256 of each built APK against the on-device base.apk: byte-identical |
| 816 | # means same versionCode, same signature and same fs-verity state, so |
| 817 | # skipping cannot bypass the versionCodeOverride or .idsig rules above. Any |
| 818 | # doubt (package absent, split APKs, size/hash unavailable) falls through to |
| 819 | # a normal install. The device hashes only when the size already matches, so |
| 820 | # a fleet-wide probe is one adb call plus on-device hashing of candidates. |
| 821 | SKIP_PROBE_FILE="" |
| 822 | if [[ "${INSTALL_SKIP_UNCHANGED:-1}" != "0" && -n "$TARGET_SERIAL" ]]; then |
| 823 | probe_specs=() |
| 824 | idx=0 |
| 825 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 826 | mod="${NORMALIZED_MODULES[$idx]}" |
| 827 | if apk="$(built_apk "$mod")"; then |
| 828 | probe_pkg="$(module_application_id "$mod")" |
| 829 | if [[ -n "$probe_pkg" ]]; then |
| 830 | probe_specs+=("$probe_pkg:$(wc -c < "$apk" | tr -d ' ')") |
| 831 | fi |
| 832 | fi |
| 833 | idx=$((idx + 1)) |
| 834 | done |
| 835 | if [[ ${#probe_specs[@]} -gt 0 ]]; then |
| 836 | echo "Probing on-device APKs for unchanged installs (one query for ${#probe_specs[@]} package(s))..." |
| 837 | probe_t0="$(date +%s)" |
| 838 | probe_cmd='for spec in '"${probe_specs[*]}"'; do p=${spec%:*}; want=${spec##*:}; paths=$(pm path "$p" 2>/dev/null); cnt=$(printf "%s" "$paths" | grep -c "^package:"); if [ "$cnt" = "1" ]; then f=${paths#package:}; f=$(printf "%s" "$f" | tr -d "\r"); sz=$(stat -c %s "$f" 2>/dev/null); sha=; if [ "$sz" = "$want" ]; then sha=$(sha256sum "$f" 2>/dev/null | cut -d" " -f1); fi; echo "SKIPCHK|$p|$sz|$sha"; else echo "SKIPCHK|$p||"; fi; done' |
| 839 | SKIP_PROBE_FILE="$(mktemp)" |
| 840 | if ! adb_dev shell "$probe_cmd" > "$SKIP_PROBE_FILE" 2>/dev/null; then |
| 841 | echo " Probe failed; installing everything (no skips)." |
| 842 | rm -f "$SKIP_PROBE_FILE" |
| 843 | SKIP_PROBE_FILE="" |
| 844 | else |
| 845 | echo " Probe done in $(( $(date +%s) - probe_t0 ))s. Byte-identical apps will be skipped (INSTALL_SKIP_UNCHANGED=0 disables)." |
| 846 | fi |
| 847 | fi |
| 848 | fi |
| 849 | # device_apk_state <package> -> "<size> <sha256>" (either may be empty). |
| 850 | device_apk_state() { |
| 851 | if [[ -z "$SKIP_PROBE_FILE" || ! -f "$SKIP_PROBE_FILE" ]]; then |
| 852 | return 0 |
| 853 | fi |
| 854 | awk -F'|' -v p="$1" '$1 == "SKIPCHK" && $2 == p { gsub(/\r/, "", $4); print $3, $4; exit }' "$SKIP_PROBE_FILE" |
| 855 | } |
| 856 | host_sha256() { |
| 857 | if command -v sha256sum >/dev/null 2>&1; then |
| 858 | sha256sum "$1" | awk '{print $1}' |
| 859 | else |
| 860 | shasum -a 256 "$1" | awk '{print $1}' |
| 861 | fi |
| 862 | } |
| 863 | # --- Install order: largest APK first -------------------------------------- |
| 864 | # Waves run INSTALL_JOBS at a time and wait for the slowest member, so |
| 865 | # starting the biggest pushes first (longest-processing-time scheduling) |
| 866 | # keeps the transport busy instead of stranding it behind one 195 MB APK in |
| 867 | # the final wave. |
| 868 | INSTALL_ORDER=() |
| 869 | order_lines=() |
| 870 | idx=0 |
| 871 | while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do |
| 872 | if apk="$(built_apk "${NORMALIZED_MODULES[$idx]}")"; then |
| 873 | order_lines+=("$(wc -c < "$apk" | tr -d ' ') $idx") |
| 874 | else |
| 875 | order_lines+=("0 $idx") |
| 876 | fi |
| 877 | idx=$((idx + 1)) |
| 878 | done |
| 879 | while read -r _ord_size ord_idx; do |
| 880 | INSTALL_ORDER+=("$ord_idx") |
| 881 | done < <(printf '%s\n' "${order_lines[@]}" | sort -rn) |
| 882 | # Session installs run INSTALL_JOBS at a time (default 4): every install is |
| 883 | # a push plus four `pm` round trips over the forwarded transport, so serial |
| 884 | # installs spent minutes on latency alone for `all`. PackageManager accepts |
| 885 | # concurrent sessions; pushes share the transport but overlap the round |
| 886 | # trips. The default stays at 4: the forwarded endpoint already drops the |
| 887 | # device for seconds at a time under install load at 4 (see adb_dev), and |
| 888 | # per-app push/commit timings printed below now make the right value |
| 889 | # measurable -- try INSTALL_JOBS=6 or 8 on an idle device and compare the |
| 890 | # aggregate MB/s before raising the default. Set INSTALL_JOBS=1 for the old |
| 891 | # serial behaviour. |
| 892 | INSTALL_JOBS="${INSTALL_JOBS:-4}" |
| 893 | case "$INSTALL_JOBS" in ""|*[!0-9]*|0) INSTALL_JOBS=4;; esac |
| 894 | echo "Installing $TOTAL app(s) to $TARGET_SERIAL (${INSTALL_JOBS} at a time, largest first)..." |
| 895 | INSTALL_T0="$(date +%s)" |
| 896 | install_one() { |
| 897 | local mod="$1" cur="$2" apk pkg local_size dev_state dev_size dev_sha local_sha |
| 898 | if apk="$(built_apk "$mod")"; then |
| 899 | pkg="$(module_application_id "$mod")" |
| 900 | # Skip only on a full size + SHA-256 match against the installed base.apk. |
| 901 | if [[ -n "$SKIP_PROBE_FILE" ]]; then |
| 902 | local_size="$(wc -c < "$apk" | tr -d ' ')" |
| 903 | dev_state="$(device_apk_state "$pkg")" |
| 904 | dev_size="$(printf '%s' "$dev_state" | awk '{print $1}')" |
| 905 | dev_sha="$(printf '%s' "$dev_state" | awk '{print $2}')" |
| 906 | if [[ -n "$dev_size" && -n "$dev_sha" && "$dev_size" == "$local_size" ]]; then |
| 907 | local_sha="$(host_sha256 "$apk" | tr '[:upper:]' '[:lower:]')" |
| 908 | if [[ -n "$local_sha" && "$local_sha" == "$(printf '%s' "$dev_sha" | tr '[:upper:]' '[:lower:]')" ]]; then |
| 909 | echo "[$cur/$TOTAL] Skipping $pkg (identical APK already installed, $(awk "BEGIN {printf \"%.1f\", $local_size/1048576}") MB)." |
| 910 | return 2 |
| 911 | fi |
| 912 | fi |
| 913 | fi |
| 914 | install_apk_via_session "$apk" "$pkg" "$cur" "$TOTAL" "$mod" |
| 915 | else |
| 916 | echo "[$cur/$TOTAL] Error: no APK found for :${mod} (assemble failed?)." |
| 917 | return 1 |
| 918 | fi |
| 919 | } |
| 920 | if [[ "$INSTALL_JOBS" -gt 1 && $TOTAL -gt 1 ]]; then |
| 921 | FAIL_DIR="$(mktemp -d)" |
| 922 | SKIP_DIR="$(mktemp -d)" |
| 923 | pos=0 |
| 924 | oi=0 |
| 925 | while [[ $oi -lt ${#INSTALL_ORDER[@]} ]]; do |
| 926 | wave_end=$((oi + INSTALL_JOBS)) |
| 927 | if [[ $wave_end -gt $TOTAL ]]; then wave_end=$TOTAL; fi |
| 928 | while [[ $oi -lt $wave_end ]]; do |
| 929 | idx="${INSTALL_ORDER[$oi]}" |
| 930 | mod="${NORMALIZED_MODULES[$idx]}" |
| 931 | pos=$((pos + 1)) |
| 932 | ( rc=0; install_one "$mod" "$pos" || rc=$? |
| 933 | if [[ $rc -eq 2 ]]; then touch "$SKIP_DIR/$idx"; |
| 934 | elif [[ $rc -ne 0 ]]; then touch "$FAIL_DIR/$idx"; fi ) & |
| 935 | oi=$((oi + 1)) |
| 936 | done |
| 937 | wait |
| 938 | done |
| 939 | FAILURES=$(find "$FAIL_DIR" -type f | wc -l | tr -d ' ') |
| 940 | SKIPPED=$(find "$SKIP_DIR" -type f | wc -l | tr -d ' ') |
| 941 | rm -rf "$FAIL_DIR" "$SKIP_DIR" |
| 942 | else |
| 943 | pos=0 |
| 944 | oi=0 |
| 945 | while [[ $oi -lt ${#INSTALL_ORDER[@]} ]]; do |
| 946 | idx="${INSTALL_ORDER[$oi]}" |
| 947 | pos=$((pos + 1)) |
| 948 | rc=0 |
| 949 | install_one "${NORMALIZED_MODULES[$idx]}" "$pos" || rc=$? |
| 950 | if [[ $rc -eq 2 ]]; then |
| 951 | SKIPPED=$((SKIPPED + 1)) |
| 952 | elif [[ $rc -ne 0 ]]; then |
| 953 | FAILURES=$((FAILURES + 1)) |
| 954 | fi |
| 955 | oi=$((oi + 1)) |
| 956 | done |
| 957 | fi |
| 958 | if [[ -n "$SKIP_PROBE_FILE" ]]; then |
| 959 | rm -f "$SKIP_PROBE_FILE" |
| 960 | fi |
| 961 | INSTALLED_COUNT=$((TOTAL - FAILURES - SKIPPED)) |
| 962 | INSTALL_SECS=$(( $(date +%s) - INSTALL_T0 )) |
| 963 | echo "" |
| 964 | echo "Install phase: ${INSTALL_SECS}s for $TOTAL app(s): $INSTALLED_COUNT installed, $SKIPPED skipped (unchanged), $FAILURES failed." |
| 965 | if [[ $FAILURES -gt 0 ]]; then |
| 966 | exit 1 |
| 967 | fi |
| 968 | if [[ $SKIPPED -gt 0 ]]; then |
| 969 | echo "Install complete: $INSTALLED_COUNT installed, $SKIPPED skipped (unchanged) of $TOTAL." |
| 970 | else |
| 971 | echo "Install complete: $TOTAL/$TOTAL installed successfully." |
| 972 | fi |
| 973 | exit $BUILD_EXIT |