#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT="$SCRIPT_DIR" print_help() { cat <<'EOF' Usage: ./install [dev|release] [module...] ./install [module...] # defaults to dev ./install [dev|release] all # installs all app modules ./install all # same, defaults to dev ./install [dev|release] [8|9] # Pixel 8 or Pixel 9 (by model) Description: Ergonomic wrapper around ./gradlew :module:assembleDev + adb session install. - Variant is swappable and optional (defaults to dev) - Supports both slash and colon notations: games/voxels == games:voxels - Supports games shorthand: ./install voxels -> games:voxels (auto-prefixed if games/ is a game) - Supports personal shorthand: ./install dooraccess -> personal:dooraccess (auto-prefixed if personal/ is a personal app) - Supports "all" keyword: ./install all -> installs every app module - Supports multiple modules in one call (single gradlew invocation) - 8|9 selects the Pixel 8 or Pixel 9 by model (optional; defaults to the first non-emulator device). Also accepts --device 8|9 / --device=8|9. - Installs via adb package sessions with the .idsig sidecar staged, so updates to preinstalled MAOS system apps get fs-verity (plain installDev/installRelease and `adb install` never stage it and always fail those updates). Auto-passes -PversionCodeOverride when the on-device package is at the same versionCode as the build: +1 for system apps, exactly the on-device version for user apps. Examples: ./install dev contacts -> :contacts:assembleDev + session install ./install release games:voxels -> :games:voxels:assembleRelease + session install ./install dev games/voxels -> :games:voxels:assembleDev (slash normalized) ./install dev :games:voxels: -> :games:voxels:assembleDev (colon trimming) ./install voxels -> :games:voxels:assembleDev (shorthand auto games:) ./install chess -> :games:chess:assembleDev (shorthand auto games:) ./install dooraccess -> :personal:dooraccess:assembleDev (shorthand auto personal:) ./install all -> :contacts:assembleDev :games:chess:assembleDev ... (all modules) ./install dev all -> same as above with dev variant ./install contacts calendar -> :contacts:assembleDev :calendar:assembleDev ./install release contacts -> :contacts:assembleRelease + session install ./install contacts dev -> :contacts:assembleDev (variant anywhere) ./install 9 maps -> :maps:assembleDev + session install to the Pixel 9 ./install dev 8 maps -> :maps:assembleDev + session install to the Pixel 8 ./install --help ./install --dry-run dev contacts # prints what would run without executing gradle ./install --dry-run all # preview all modules Variant rules: - dev (default): assembleDev + session install unless explicitly asked for release - release: assembleRelease + session install when explicitly requested - debug: BLOCKED - never use assembleDebug Notes: - NEVER uninstalls an app (no uninstall tasks) - Validates modules against app modules (those applying common-conventions-app) - Installs to the first connected device whose serial does not start with 'emulator' (unless 8|9 selects the Pixel 8 or Pixel 9) - Set INSTALL_DRY_RUN=1 or pass --dry-run for dry-run mode EOF } normalize_module() { local m="$1" m="$(echo "$m" | xargs)" m="$(echo "$m" | sed -e 's|^[/:]*||' -e 's|[/:]*$||')" m="$(echo "$m" | tr '/' ':')" while echo "$m" | grep -q "::"; do m="$(echo "$m" | sed 's/::/:/g')" done m="$(echo "$m" | tr '[:upper:]' '[:lower:]')" echo "$m" } get_app_modules_slash() { local settings_file="$ROOT/settings.gradle.kts" if [[ ! -f "$settings_file" ]]; then return fi grep -E 'include\(":[^"]+"\)' "$settings_file" 2>/dev/null \ | sed -E 's/.*include\(":([^"]+)"\).*/\1/' \ | tr ':' '/' \ | while IFS= read -r p; do case "$p" in "" ) continue;; esac if [[ -f "$ROOT/$p/build.gradle.kts" ]] && grep -qF 'id("common-conventions-app")' "$ROOT/$p/build.gradle.kts" 2>/dev/null; then echo "$p" fi done | sort -u } print_valid_modules() { local slash_modules slash_modules="$(get_app_modules_slash)" if [[ -z "$slash_modules" ]]; then echo " (could not discover app modules)" return fi echo "" echo "Valid app modules (slash notation for filesystem, colon for Gradle):" echo "" printf " %-30s %s\n" "Filesystem (slash)" "Gradle (colon)" printf " %-30s %s\n" "--------------------" "-------------------" while IFS= read -r sm; do case "$sm" in "" ) continue;; esac colon_mod="$(echo "$sm" | tr '/' ':')" printf " %-30s :%s\n" "$sm" "$colon_mod" done <<< "$slash_modules" echo "" echo "Tip: Use either notation: ./install dev games/voxels OR ./install dev games:voxels" echo "Shorthand: ./install dev voxels (auto-expands to games:voxels if games/ exists)" echo " ./install dev dooraccess (auto-expands to personal:dooraccess if personal/ exists)" echo "All: ./install all (installs every app module)" } array_contains() { local needle="$1" shift local item for item in "$@"; do if [[ "$item" == "$needle" ]]; then return 0 fi done return 1 } # --- Handle zero args early (no array expansion needed) --- if [[ $# -eq 0 ]]; then print_help echo "" echo "Error: No modules specified." print_valid_modules exit 1 fi # --- Detect --help/-h/help anywhere (direct $@ loop, no array) --- for arg in "$@"; do case "$arg" in --help|-h|help) print_help exit 0 ;; esac done # --- Early DRY_RUN detection --- DRY_RUN=0 if [[ "${INSTALL_DRY_RUN:-0}" == "1" ]]; then DRY_RUN=1 fi for arg in "$@"; do if [[ "$arg" == "--dry-run" ]]; then DRY_RUN=1 fi done # --- Build FILTERED_ARGS (exclude --dry-run) --- FILTERED_ARGS=() for arg in "$@"; do if [[ "$arg" == "--dry-run" ]]; then continue fi FILTERED_ARGS+=("$arg") done # After filtering, if empty (e.g., only --dry-run was passed) if [[ ${#FILTERED_ARGS[@]} -eq 0 ]]; then print_help echo "" echo "Error: No modules specified (dry-run mode)." print_valid_modules exit 1 fi # --- Device hint: bare 8|9 anywhere, or --device 8|9 / --device=8|9 (optional) --- DEVICE_HINT="" PRESCAN_ARGS=() idx=0 while [[ $idx -lt ${#FILTERED_ARGS[@]} ]]; do arg="${FILTERED_ARGS[$idx]}" lc="$(echo "$arg" | tr '[:upper:]' '[:lower:]')" if [[ "$lc" == "--device" ]]; then if [[ $((idx + 1)) -ge ${#FILTERED_ARGS[@]} ]]; then echo "Error: --device requires a value: 8 (Pixel 8) or 9 (Pixel 9)." exit 1 fi DEVICE_HINT="$(echo "${FILTERED_ARGS[$((idx + 1))]}" | tr '[:upper:]' '[:lower:]')" idx=$((idx + 2)) continue elif [[ "$lc" == --device=* ]]; then DEVICE_HINT="${lc#--device=}" idx=$((idx + 1)) continue elif [[ "$lc" == "8" || "$lc" == "9" ]]; then DEVICE_HINT="$lc" idx=$((idx + 1)) continue elif [[ "$lc" =~ ^[0-9]+$ ]]; then echo "Error: Invalid device '$arg'. Use 8 (Pixel 8) or 9 (Pixel 9)." exit 1 fi PRESCAN_ARGS+=("$arg") idx=$((idx + 1)) done FILTERED_ARGS=("${PRESCAN_ARGS[@]}") if [[ -n "$DEVICE_HINT" && "$DEVICE_HINT" != "8" && "$DEVICE_HINT" != "9" ]]; then echo "Error: Invalid device '$DEVICE_HINT'. Use 8 (Pixel 8) or 9 (Pixel 9)." exit 1 fi # --- Arg parsing: variant detection anywhere --- VARIANT_LC="" VARIANT_COUNT=0 MODULES_RAW=() idx=0 while [[ $idx -lt ${#FILTERED_ARGS[@]} ]]; do arg="${FILTERED_ARGS[$idx]}" lc="$(echo "$arg" | tr '[:upper:]' '[:lower:]')" if [[ "$lc" == "dev" || "$lc" == "release" || "$lc" == "debug" ]]; then if [[ -z "$VARIANT_LC" ]]; then VARIANT_LC="$lc" VARIANT_COUNT=1 else if [[ "$VARIANT_LC" != "$lc" ]]; then echo "Error: Multiple distinct variants specified: '$VARIANT_LC' and '$lc'." echo "Please specify only one variant: dev or release." echo "" print_help exit 1 fi VARIANT_COUNT=$((VARIANT_COUNT + 1)) fi else MODULES_RAW+=("$arg") fi idx=$((idx + 1)) done if [[ -z "$VARIANT_LC" ]]; then VARIANT_LC="dev" fi if [[ "$VARIANT_LC" == "debug" ]]; then echo "Error: 'debug' variant is blocked." echo "" echo " - Never assemble or install a debug variant" echo " - Always install the dev build, unless specifically asked for release" echo "" echo "Use 'dev' (default) or 'release' instead:" echo " ./install dev " echo " ./install release " exit 1 fi if [[ ${#MODULES_RAW[@]} -eq 0 ]]; then echo "Error: No modules specified (only variant '$VARIANT_LC' found)." echo "" print_help print_valid_modules exit 1 fi # --- Normalization + Dedup + Shorthand + "all" + auto games: prefix --- NORMALIZED_MODULES=() ALL_EXPANDED=0 idx=0 while [[ $idx -lt ${#MODULES_RAW[@]} ]]; do raw="${MODULES_RAW[$idx]}" norm="$(normalize_module "$raw")" if [[ -z "$norm" ]]; then echo "Warning: Skipping empty/invalid module token '$raw' after normalization." idx=$((idx + 1)) continue fi # Handle "all" keyword - expand to every app module if [[ "$norm" == "all" ]]; then if [[ $ALL_EXPANDED -eq 0 ]]; then echo "Info: Expanding 'all' to all app modules..." all_slash="$(get_app_modules_slash)" if [[ -z "$all_slash" ]]; then echo "Error: Could not discover app modules for 'all' expansion." print_valid_modules exit 1 fi while IFS= read -r sm; do case "$sm" in "" ) continue;; esac colon_mod="$(echo "$sm" | tr '/' ':')" # dedup for expanded modules dup=0 j=0 while [[ $j -lt ${#NORMALIZED_MODULES[@]} ]]; do if [[ "${NORMALIZED_MODULES[$j]}" == "$colon_mod" ]]; then dup=1 break fi j=$((j + 1)) done if [[ $dup -eq 0 ]]; then NORMALIZED_MODULES+=("$colon_mod") fi done <<< "$all_slash" ALL_EXPANDED=1 fi idx=$((idx + 1)) continue fi # Auto games:/personal: prefix - if token has no colon and games/ or personal/ is a valid app module, prefix it # This allows ./install voxels -> games:voxels, ./install chess -> games:chess, ./install dooraccess -> personal:dooraccess etc. if [[ "$norm" != *":"* ]]; then for auto_prefix in games personal; do pref_path="$ROOT/$auto_prefix/$norm" if [[ -f "$pref_path/build.gradle.kts" ]] && grep -qF 'id("common-conventions-app")' "$pref_path/build.gradle.kts" 2>/dev/null; then expanded="$auto_prefix:$norm" echo "Info: Expanding shorthand '$raw' ($norm) -> $expanded (found $auto_prefix/$norm)" norm="$expanded" break elif [[ ! -e "$ROOT/$norm" && -e "$ROOT/$auto_prefix/$norm" ]]; then # Fallback: if root/ doesn't exist but / does (backward compat) expanded="$auto_prefix:$norm" echo "Info: Expanding shorthand '$raw' ($norm) -> $expanded (found $auto_prefix/$norm)" norm="$expanded" break fi done fi # Dedup via index loop (bash 3.2 safe) dup=0 j=0 while [[ $j -lt ${#NORMALIZED_MODULES[@]} ]]; do if [[ "${NORMALIZED_MODULES[$j]}" == "$norm" ]]; then dup=1 break fi j=$((j + 1)) done if [[ $dup -eq 0 ]]; then NORMALIZED_MODULES+=("$norm") fi idx=$((idx + 1)) done if [[ ${#NORMALIZED_MODULES[@]} -eq 0 ]]; then echo "Error: No valid modules after normalization." print_valid_modules exit 1 fi # --- Validation --- INVALID_MODULES=() idx=0 while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do mod="${NORMALIZED_MODULES[$idx]}" fs_path="$(echo "$mod" | tr ':' '/')" full_path="$ROOT/$fs_path" if [[ ! -f "$full_path/build.gradle.kts" ]]; then INVALID_MODULES+=("$mod (not found: $fs_path/build.gradle.kts)") else if ! grep -qF 'id("common-conventions-app")' "$full_path/build.gradle.kts" 2>/dev/null; then INVALID_MODULES+=("$mod (exists but is not an app module - missing common-conventions-app)") fi fi idx=$((idx + 1)) done if [[ ${#INVALID_MODULES[@]} -gt 0 ]]; then echo "Error: Some modules are invalid:" idx=0 while [[ $idx -lt ${#INVALID_MODULES[@]} ]]; do echo " - ${INVALID_MODULES[$idx]}" idx=$((idx + 1)) done print_valid_modules exit 1 fi # --- Build Gradle tasks (assemble only; install happens below via adb sessions) --- VARIANT_CAP="$(echo "$VARIANT_LC" | awk '{print toupper(substr($0,1,1)) substr($0,2)}')" TASKS=() idx=0 while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do mod="${NORMALIZED_MODULES[$idx]}" TASKS+=(":${mod}:assemble${VARIANT_CAP}") idx=$((idx + 1)) done # Safe joining without ${ARRAY[*]} under set -u when empty (should not be empty here, but guard) modules_str="" tasks_str="" idx=0 while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do if [[ $idx -gt 0 ]]; then modules_str="$modules_str ${NORMALIZED_MODULES[$idx]}" else modules_str="${NORMALIZED_MODULES[$idx]}" fi idx=$((idx + 1)) done idx=0 while [[ $idx -lt ${#TASKS[@]} ]]; do if [[ $idx -gt 0 ]]; then tasks_str="$tasks_str ${TASKS[$idx]}" else tasks_str="${TASKS[$idx]}" fi idx=$((idx + 1)) done echo "Variant: $VARIANT_LC (assemble + adb session install)" echo "Modules (${#NORMALIZED_MODULES[@]}): $modules_str" echo "Gradle tasks: $tasks_str" echo "" # --- Device selection: first connected device whose serial doesn't start with 'emulator' --- ADB_BIN="" if command -v adb >/dev/null 2>&1; then ADB_BIN="adb" else for adb_base in "${ANDROID_HOME:-}" "${ANDROID_SDK_ROOT:-}" "$HOME/Android/Sdk" "$HOME/Library/Android/sdk"; do if [[ -n "$adb_base" && -x "$adb_base/platform-tools/adb" ]]; then ADB_BIN="$adb_base/platform-tools/adb" break fi done fi TARGET_SERIAL="" if [[ -n "$ADB_BIN" ]]; then # hint '' = first non-emulator device; '8' = model Pixel_8; # '9' = model Pixel_9* (covers Pixel_9 and Pixel_9_Pro_XL). while read -r dev_line; do dev_serial="$(echo "$dev_line" | awk '{print $1}')" dev_state="$(echo "$dev_line" | awk '{print $2}')" [[ "$dev_state" == "device" ]] || continue case "$dev_serial" in emulator*) continue;; esac case "$dev_serial" in "List"*) continue;; esac if [[ -z "$DEVICE_HINT" ]]; then TARGET_SERIAL="$dev_serial" break fi dev_model="$(echo "$dev_line" | sed -nE 's/.*model:([^[:space:]]+).*/\1/p')" if [[ "$DEVICE_HINT" == "8" && "$dev_model" == "Pixel_8" ]]; then TARGET_SERIAL="$dev_serial" break fi if [[ "$DEVICE_HINT" == "9" && "$dev_model" == Pixel_9* ]]; then TARGET_SERIAL="$dev_serial" break fi done < <("$ADB_BIN" devices -l 2>/dev/null) fi if [[ -z "$ADB_BIN" ]]; then if [[ $DRY_RUN -eq 1 ]]; then echo "[DRY-RUN] Warning: adb not found; a device could not be selected." else echo "Error: adb not found; cannot select a target device." echo "Install Android platform-tools and put adb on PATH (or set ANDROID_HOME)." exit 1 fi elif [[ -n "$TARGET_SERIAL" ]]; then export ANDROID_SERIAL="$TARGET_SERIAL" if [[ -n "$DEVICE_HINT" ]]; then echo "Target device: $ANDROID_SERIAL (Pixel $DEVICE_HINT)" else echo "Target device: $ANDROID_SERIAL (first connected device not starting with 'emulator')" fi elif [[ -n "$DEVICE_HINT" ]]; then echo "Error: No connected Pixel $DEVICE_HINT found (check 'adb devices')." exit 1 elif [[ $DRY_RUN -eq 1 ]]; then echo "[DRY-RUN] Warning: no non-emulator device connected; ANDROID_SERIAL not set." else echo "Error: No non-emulator device connected." echo "Connect a physical device (check 'adb devices'); refusing to install to an emulator." exit 1 fi # applicationId for a module (all are string literals in build.gradle.kts). module_application_id() { sed -nE 's/^[[:space:]]*applicationId[[:space:]]*=[[:space:]]*"([^"]+)".*/\1/p' \ "$ROOT/$(echo "$1" | tr ':' '/')/build.gradle.kts" | head -1 } # APK built for a module; output-metadata.json names the file authoritatively # (nested modules like cast:tv build tv-dev.apk, not cast-tv-dev.apk). built_apk() { local mod="$1" fs_path out_dir meta base_name file_name fs_path="$(echo "$mod" | tr ':' '/')" out_dir="$ROOT/$fs_path/build/outputs/apk/$VARIANT_LC" meta="$out_dir/output-metadata.json" base_name="${mod##*:}" file_name="$base_name-$VARIANT_LC.apk" if [[ -f "$meta" ]]; then local found found="$(grep -oE '"outputFile"[[:space:]]*:[[:space:]]*"[^"]+"' "$meta" | head -1 | sed -E 's/.*\"([^\"]+)\"$/\1/')" [[ -n "$found" ]] && file_name="$found" fi if [[ -f "$out_dir/$file_name" ]]; then echo "$out_dir/$file_name" return 0 fi return 1 } # on-device versionCode for a package, empty when not installed. device_version_code() { "$ADB_BIN" -s "$TARGET_SERIAL" shell pm list packages --show-versioncode "$1" 2>/dev/null \ | sed -nE "s/^package:${1//./\\.}[[:space:]]+versionCode:([0-9]+).*/\\1/p" | head -1 } # exit 0 when the package is installed as a system app. is_system_app() { "$ADB_BIN" -s "$TARGET_SERIAL" shell pm list packages -s "$1" 2>/dev/null \ | tr -d '\r' | grep -qFx "package:$1" } install_apk_via_session() { local apk="$1" package_id="$2" cur="${3:-}" total="${4:-}" local apk_name apk_size apk_mb session device_apk idsig device_idsig commit_out prefix apk_name="$(basename "$apk")" apk_size="$(wc -c < "$apk" | tr -d ' ')" apk_mb="$(awk "BEGIN {printf \"%.1f\", $apk_size/1048576}")" device_apk="/data/local/tmp/install_${package_id}.apk" if [[ -n "$cur" && -n "$total" ]]; then prefix="[$cur/$total] " else prefix="" fi echo "${prefix}Installing $package_id ($apk_name, ${apk_mb} MB)..." echo " ${prefix}[1/4] Pushing APK to device..." "$ADB_BIN" -s "$TARGET_SERIAL" push "$apk" "$device_apk" >/dev/null 2>&1 || { echo " ${prefix}Error: adb push failed for $apk_name."; return 1 } echo " ${prefix}[2/4] Creating install session..." session="$("$ADB_BIN" -s "$TARGET_SERIAL" shell pm install-create -r 2>&1 \ | sed -nE 's/.*created install session \[([0-9]+)\].*/\1/p')" if [[ -z "$session" ]]; then echo " ${prefix}Error: could not create install session." "$ADB_BIN" -s "$TARGET_SERIAL" shell rm -f "$device_apk" >/dev/null 2>&1 return 1 fi # Stage the APK first, then its .idsig sidecar (V4 signature) under the # matching name. PackageManager enables fs-verity from a staged idsig, which # MAOS/GrapheneOS requires for updates to preinstalled system apps. Plain # `adb install` never stages the sidecar, so those updates always fail. echo " ${prefix}[3/4] Staging APK into session $session..." "$ADB_BIN" -s "$TARGET_SERIAL" shell pm install-write -S "$apk_size" "$session" base.apk "$device_apk" >/dev/null 2>&1 || { echo " ${prefix}Error: failed to stage $apk_name into session $session." "$ADB_BIN" -s "$TARGET_SERIAL" shell pm install-abandon "$session" >/dev/null 2>&1 "$ADB_BIN" -s "$TARGET_SERIAL" shell rm -f "$device_apk" >/dev/null 2>&1 return 1 } idsig="$apk.idsig" if [[ -f "$idsig" ]]; then device_idsig="$device_apk.idsig" echo " ${prefix}Staging .idsig sidecar..." if "$ADB_BIN" -s "$TARGET_SERIAL" push "$idsig" "$device_idsig" >/dev/null 2>&1; then "$ADB_BIN" -s "$TARGET_SERIAL" shell pm install-write -S "$(wc -c < "$idsig" | tr -d ' ')" \ "$session" base.apk.idsig "$device_idsig" >/dev/null 2>&1 || \ echo " ${prefix}Warning: could not stage .idsig; system-app updates may fail fs-verity." else echo " ${prefix}Warning: could not push .idsig; system-app updates may fail fs-verity." fi "$ADB_BIN" -s "$TARGET_SERIAL" shell rm -f "$device_idsig" >/dev/null 2>&1 else echo " ${prefix}Warning: no .idsig sidecar next to the APK; system-app updates may fail fs-verity." fi echo " ${prefix}[4/4] Committing install session $session..." commit_out="$("$ADB_BIN" -s "$TARGET_SERIAL" shell pm install-commit "$session" 2>&1)" "$ADB_BIN" -s "$TARGET_SERIAL" shell rm -f "$device_apk" >/dev/null 2>&1 # `pm install-commit` prints "Success" but exits 0 either way; fail on absence. if ! echo "$commit_out" | grep -qi "^Success"; then echo " ${prefix}Error: install failed for $package_id: $commit_out" return 1 fi if [[ -n "$cur" && -n "$total" ]]; then echo " Installed $package_id. [$cur/$total done]" else echo " Installed $package_id." fi } # --- Extra Gradle args: --continue so one broken module doesn't block the rest --- GRADLE_ARGS=(--continue) gradle_args_str=" --continue" if [[ "$VARIANT_LC" == "release" && $ALL_EXPANDED -eq 1 ]]; then GRADLE_ARGS+=(-P"android.r8.maxWorkers=8") gradle_args_str="$gradle_args_str -Pandroid.r8.maxWorkers=8" fi # --- Big-build heap: gradle.properties defaults to a small daemon heap sized for # scoped work. A full-repo build ("all", ~100 modules, concurrent D8 dex merge) # OOMs on it, so escalate here — and only here (ALL_EXPANDED). -D spawns a # separate big daemon; the small one stays alive for normal builds. if [[ $ALL_EXPANDED -eq 1 ]]; then GRADLE_ARGS+=(-D"org.gradle.jvmargs=-Xmx46144m -Xms4512m -XX:MaxMetaspaceSize=1024m -XX:+UseG1GC -Dfile.encoding=UTF-8") GRADLE_ARGS+=(--max-workers=32) gradle_args_str="$gradle_args_str -Dorg.gradle.jvmargs=<46g-big-build-heap> --max-workers=32" fi # --- versionCodeOverride: MAOS refuses to update a system package to the same # versionCode, so an APK built from the same version.txt as the on-device OS image # can never be installed over it. Query the device once; for system apps bump to # one above the on-device version, for user apps just match it exactly # (version.txt itself stays untouched). REPO_VERSION="$(sed -n '1p' "$ROOT/version.txt" | tr -d '\r' | xargs)" NEED_BUMP="$REPO_VERSION" if [[ -n "$TARGET_SERIAL" ]]; then idx=0 while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do pkg="$(module_application_id "${NORMALIZED_MODULES[$idx]}")" if [[ -n "$pkg" ]]; then dev_v="$(device_version_code "$pkg")" if [[ -n "$dev_v" && "$dev_v" -ge "$NEED_BUMP" ]]; then if is_system_app "$pkg"; then target=$((dev_v + 1)) kind="system app" else target=$dev_v kind="user app" fi if [[ "$target" -gt "$NEED_BUMP" ]]; then NEED_BUMP=$target echo "Info: $pkg is a $kind at versionCode $dev_v on-device (>= repo $REPO_VERSION); will build with -PversionCodeOverride=$target." fi fi fi idx=$((idx + 1)) done fi if [[ "$NEED_BUMP" -gt "$REPO_VERSION" ]]; then GRADLE_ARGS+=(-P"versionCodeOverride=$NEED_BUMP") gradle_args_str="$gradle_args_str -PversionCodeOverride=$NEED_BUMP" fi if [[ $DRY_RUN -eq 1 ]]; then echo "[DRY-RUN] Would execute: $SCRIPT_DIR/gradlew $tasks_str$gradle_args_str" idx=0 while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do mod="${NORMALIZED_MODULES[$idx]}" if apk="$(built_apk "$mod")"; then echo "[DRY-RUN] Would session-install $(module_application_id "$mod") from $apk" else echo "[DRY-RUN] Would session-install $(module_application_id "$mod") (APK expected after assemble)" fi idx=$((idx + 1)) done exit 0 fi echo "Running: $SCRIPT_DIR/gradlew $tasks_str$gradle_args_str" echo "" # Execute with all tasks expanded via index loop (preserving word splitting safety) set +u "$SCRIPT_DIR/gradlew" "${TASKS[@]}" "${GRADLE_ARGS[@]}" BUILD_EXIT=$? echo "" # Install whichever APKs were (re)built, even if sibling modules failed (--continue). FAILURES=0 if [[ $BUILD_EXIT -ne 0 ]]; then echo "Warning: assemble reported failures; installing whatever APKs exist." fi TOTAL=${#NORMALIZED_MODULES[@]} echo "" echo "Installing $TOTAL app(s) to $TARGET_SERIAL..." idx=0 while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do mod="${NORMALIZED_MODULES[$idx]}" cur=$((idx + 1)) if apk="$(built_apk "$mod")"; then install_apk_via_session "$apk" "$(module_application_id "$mod")" "$cur" "$TOTAL" || FAILURES=$((FAILURES + 1)) else echo "[$cur/$TOTAL] Error: no APK found for :${mod} (assemble failed?)." FAILURES=$((FAILURES + 1)) fi idx=$((idx + 1)) done if [[ $FAILURES -gt 0 ]]; then echo "" echo "Install complete: $((TOTAL - FAILURES))/$TOTAL succeeded, $FAILURES failed." exit 1 fi echo "" echo "Install complete: $TOTAL/$TOTAL installed successfully." exit $BUILD_EXIT