Modern Apps git

← Modern-Apps.git

install: skip byte-identical APKs, fix watch staging collision

Commit
161f6c03b8a4fa3714e0d74123d0ca2ce6ee380a
Author
Vayun Mathur <[email protected]>
When
2026-12-05 22:27 UTC (5 hours ago)
Parents
fdf3b24
The install phase of ./install, not Gradle, dominated repeat runs:
a dev 'all' run pushes 73 APKs totalling ~8.4 GB (avg 115 MB; maps
195 MB, camera 133 MB) over the forwarded adb transport at an
observed ~3.4 MB/s per stream, plus nine adb round trips per app.

Skip-unchanged: after assemble, one batched device query returns,
for every package, the installed base.apk size and (only when the
size matches the built APK) its SHA-256. An app whose built APK is
byte-identical to the installed one skips push, session staging and
commit entirely. Identical bytes imply identical versionCode and
signature, so the versionCodeOverride and fs-verity/.idsig rules
still hold; a package that is absent, split, or unprobeable always
installs normally. INSTALL_SKIP_UNCHANGED=0 disables the probe.
Skipped apps are counted and reported separately from installed
and failed ones.

Watch staging collision: :watch and :watch:wear share
applicationId com.vayunmathur.watch, so the old package-only
staging path /data/local/tmp/install_<package>.apk made the two
overwrite each other's APK (and cleanup-delete each other's file)
whenever they shared a parallel wave. Staging is now per-module:
install_<package>_<module-slug>.apk (+ matching .idsig path).

Ordering and observability: installs now run largest-APK-first so
wave barriers strand the transport less; the .idsig tmp cleanup is
merged into the post-commit rm (one fewer round trip per app);
each install prints total seconds and push seconds/MB-s, and the
run ends with an install-phase summary (installed / skipped /
failed + wall time). INSTALL_JOBS stays at 4: the forwarded
endpoint already drops the device under load at 4, and the new
per-app timings make the 6/8 experiment measurable on an idle
device before the default moves.

Verified: bash -n clean; ./install --dry-run 9 contacts calendar
prints the expected tasks; the SKIPCHK probe format was validated
with a single read-only adb shell query (size returned, hash only
on size match). No real install was run: a fleet install from
wt-install8 was live on the device throughout.

Diff

AGENTS.md
diff --git a/AGENTS.md b/AGENTS.mdindex d4ed17e2bd..15550e4d82 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -97,7 +97,7 @@ Variants: apps and libraries both define only `dev` and `release` build types (t ## 4. Tools โ€” use these, not raw gradle/adb Work in repo dir. Experiments/logs go in `analysis/` (gitignored). -`./install` โ€” build + session-install (handles `.idsig`/fs-verity + `versionCodeOverride` automatically):+`./install` โ€” build + session-install (handles `.idsig`/fs-verity + `versionCodeOverride` automatically; skips apps whose built APK is byte-identical to the installed one, `INSTALL_SKIP_UNCHANGED=0` disables): ``` ./install <device> <module> [...]     # release by default; device (8|9|--device=watch) is required ./install [dev|release] <device> <module> [...]
install
diff --git a/install b/installindex 042f3d07bc..f2aa801cd8 100755--- a/install+++ b/install@@ -33,6 +33,13 @@ Description:     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.+  - Skips the push+install for an app whose built APK is byte-identical to+    the one already installed (size + SHA-256 compared against the on-device+    base.apk, probed for every package in one device query). Identical bytes+    mean identical versionCode and signature, so skipping cannot bypass the+    versionCodeOverride or fs-verity rules; any doubt (not installed, split+    APKs, probe/hash failure) installs normally. Skipped apps are reported+    separately from installed ones. INSTALL_SKIP_UNCHANGED=0 disables this.  Examples:   ./install release contacts                -> :contacts:assembleRelease + session install@@ -64,6 +71,8 @@ Notes:   - 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+  - INSTALL_JOBS=N: parallel session installs (default 4). INSTALL_SKIP_UNCHANGED=0:+    always push+install, never skip byte-identical APKs.  EOF }@@ -609,12 +618,21 @@ is_system_app() { }  install_apk_via_session() {-  local apk="$1" package_id="$2" cur="${3:-}" total="${4:-}"+  local apk="$1" package_id="$2" cur="${3:-}" total="${4:-}" mod="${5:-}"   local apk_name apk_size apk_mb session device_apk idsig device_idsig commit_out prefix+  local t_start t_pushed t_done push_secs total_secs push_rate mod_slug   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"+  # Stage under a per-module name: :watch and :watch:wear deliberately share+  # applicationId com.vayunmathur.watch, so a package-only staging name made+  # the two overwrite each other's pushed APK whenever they landed in the+  # same parallel wave (and the loser's cleanup rm deleted the winner's file+  # mid-install). The module slug keeps every staging file unique.+  mod_slug="$(printf '%s' "${mod:-$package_id}" | tr ':/' '__')"+  device_apk="/data/local/tmp/install_${package_id}_${mod_slug}.apk"+  device_idsig="$device_apk.idsig"+  t_start="$(date +%s)"   if [[ -n "$cur" && -n "$total" ]]; then     prefix="[$cur/$total] "   else@@ -625,6 +643,7 @@ install_apk_via_session() {   adb_dev push "$apk" "$device_apk" >/dev/null || {     echo "  ${prefix}Error: adb push failed for $apk_name."; return 1   }+  t_pushed="$(date +%s)"   echo "  ${prefix}[2/4] Creating install session..."   session="$(adb_dev shell pm install-create -r \     | sed -nE 's/.*created install session \[([0-9]+)\].*/\1/p')"@@ -646,7 +665,6 @@ install_apk_via_session() {   }   idsig="$apk.idsig"   if [[ -f "$idsig" ]]; then-    device_idsig="$device_apk.idsig"     echo "  ${prefix}Staging .idsig sidecar..."     if adb_dev push "$idsig" "$device_idsig" >/dev/null; then       adb_dev shell pm install-write -S "$(wc -c < "$idsig" | tr -d ' ')" \@@ -655,22 +673,28 @@ install_apk_via_session() {     else       echo "  ${prefix}Warning: could not push .idsig; system-app updates may fail fs-verity."     fi-    adb_dev shell rm -f "$device_idsig" >/dev/null+    # The staged sidecar is copied into the session by install-write, so its+    # tmp file only needs deleting once, together with the APK after commit+    # (one round trip instead of two).   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_dev shell pm install-commit "$session")"-  adb_dev shell rm -f "$device_apk" >/dev/null+  adb_dev shell rm -f "$device_apk" "$device_idsig" >/dev/null+  t_done="$(date +%s)"+  push_secs=$((t_pushed - t_start)); if [[ $push_secs -lt 1 ]]; then push_secs=1; fi+  total_secs=$((t_done - t_start)); if [[ $total_secs -lt 1 ]]; then total_secs=1; fi+  push_rate="$(awk "BEGIN {printf \"%.1f\", $apk_size/1048576/$push_secs}")"   # `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]"+    echo "  Installed $package_id in ${total_secs}s (push ${push_secs}s @ ${push_rate} MB/s). [$cur/$total done]"   else-    echo "  Installed $package_id."+    echo "  Installed $package_id in ${total_secs}s (push ${push_secs}s @ ${push_rate} MB/s)."   fi } @@ -776,23 +800,118 @@ echo ""  # Install whichever APKs were (re)built, even if sibling modules failed (--continue). FAILURES=0+SKIPPED=0 if [[ $BUILD_EXIT -ne 0 ]]; then   echo "Warning: assemble reported failures; installing whatever APKs exist." fi TOTAL=${#NORMALIZED_MODULES[@]} echo ""+# --- Skip-unchanged probe: one device query for every package -------------+# Pushing dominates install time (a dev `all` run pushes ~8.4 GB over the+# forwarded adb transport; at the observed ~3.4 MB/s per stream that is most+# of the run). When a rebuild reproduces the exact APK already installed --+# the common case for modules a change did not touch -- the push, the session+# round trips and the on-device commit are all wasted work. Compare size ++# SHA-256 of each built APK against the on-device base.apk: byte-identical+# means same versionCode, same signature and same fs-verity state, so+# skipping cannot bypass the versionCodeOverride or .idsig rules above. Any+# doubt (package absent, split APKs, size/hash unavailable) falls through to+# a normal install. The device hashes only when the size already matches, so+# a fleet-wide probe is one adb call plus on-device hashing of candidates.+SKIP_PROBE_FILE=""+if [[ "${INSTALL_SKIP_UNCHANGED:-1}" != "0" && -n "$TARGET_SERIAL" ]]; then+  probe_specs=()+  idx=0+  while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do+    mod="${NORMALIZED_MODULES[$idx]}"+    if apk="$(built_apk "$mod")"; then+      probe_pkg="$(module_application_id "$mod")"+      if [[ -n "$probe_pkg" ]]; then+        probe_specs+=("$probe_pkg:$(wc -c < "$apk" | tr -d ' ')")+      fi+    fi+    idx=$((idx + 1))+  done+  if [[ ${#probe_specs[@]} -gt 0 ]]; then+    echo "Probing on-device APKs for unchanged installs (one query for ${#probe_specs[@]} package(s))..."+    probe_t0="$(date +%s)"+    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'+    SKIP_PROBE_FILE="$(mktemp)"+    if ! adb_dev shell "$probe_cmd" > "$SKIP_PROBE_FILE" 2>/dev/null; then+      echo "  Probe failed; installing everything (no skips)."+      rm -f "$SKIP_PROBE_FILE"+      SKIP_PROBE_FILE=""+    else+      echo "  Probe done in $(( $(date +%s) - probe_t0 ))s. Byte-identical apps will be skipped (INSTALL_SKIP_UNCHANGED=0 disables)."+    fi+  fi+fi+# device_apk_state <package> -> "<size> <sha256>" (either may be empty).+device_apk_state() {+  if [[ -z "$SKIP_PROBE_FILE" || ! -f "$SKIP_PROBE_FILE" ]]; then+    return 0+  fi+  awk -F'|' -v p="$1" '$1 == "SKIPCHK" && $2 == p { gsub(/\r/, "", $4); print $3, $4; exit }' "$SKIP_PROBE_FILE"+}+host_sha256() {+  if command -v sha256sum >/dev/null 2>&1; then+    sha256sum "$1" | awk '{print $1}'+  else+    shasum -a 256 "$1" | awk '{print $1}'+  fi+}+# --- Install order: largest APK first --------------------------------------+# Waves run INSTALL_JOBS at a time and wait for the slowest member, so+# starting the biggest pushes first (longest-processing-time scheduling)+# keeps the transport busy instead of stranding it behind one 195 MB APK in+# the final wave.+INSTALL_ORDER=()+order_lines=()+idx=0+while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do+  if apk="$(built_apk "${NORMALIZED_MODULES[$idx]}")"; then+    order_lines+=("$(wc -c < "$apk" | tr -d ' ') $idx")+  else+    order_lines+=("0 $idx")+  fi+  idx=$((idx + 1))+done+while read -r _ord_size ord_idx; do+  INSTALL_ORDER+=("$ord_idx")+done < <(printf '%s\n' "${order_lines[@]}" | sort -rn) # Session installs run INSTALL_JOBS at a time (default 4): every install is # a push plus four `pm` round trips over the forwarded transport, so serial # installs spent minutes on latency alone for `all`. PackageManager accepts # concurrent sessions; pushes share the transport but overlap the round-# trips. Set INSTALL_JOBS=1 for the old serial behaviour.+# trips. The default stays at 4: the forwarded endpoint already drops the+# device for seconds at a time under install load at 4 (see adb_dev), and+# per-app push/commit timings printed below now make the right value+# measurable -- try INSTALL_JOBS=6 or 8 on an idle device and compare the+# aggregate MB/s before raising the default. Set INSTALL_JOBS=1 for the old+# serial behaviour. INSTALL_JOBS="${INSTALL_JOBS:-4}" case "$INSTALL_JOBS" in ""|*[!0-9]*|0) INSTALL_JOBS=4;; esac-echo "Installing $TOTAL app(s) to $TARGET_SERIAL (${INSTALL_JOBS} at a time)..."+echo "Installing $TOTAL app(s) to $TARGET_SERIAL (${INSTALL_JOBS} at a time, largest first)..."+INSTALL_T0="$(date +%s)" install_one() {-  local mod="$1" cur="$2" apk+  local mod="$1" cur="$2" apk pkg local_size dev_state dev_size dev_sha local_sha   if apk="$(built_apk "$mod")"; then-    install_apk_via_session "$apk" "$(module_application_id "$mod")" "$cur" "$TOTAL"+    pkg="$(module_application_id "$mod")"+    # Skip only on a full size + SHA-256 match against the installed base.apk.+    if [[ -n "$SKIP_PROBE_FILE" ]]; then+      local_size="$(wc -c < "$apk" | tr -d ' ')"+      dev_state="$(device_apk_state "$pkg")"+      dev_size="$(printf '%s' "$dev_state" | awk '{print $1}')"+      dev_sha="$(printf '%s' "$dev_state" | awk '{print $2}')"+      if [[ -n "$dev_size" && -n "$dev_sha" && "$dev_size" == "$local_size" ]]; then+        local_sha="$(host_sha256 "$apk" | tr '[:upper:]' '[:lower:]')"+        if [[ -n "$local_sha" && "$local_sha" == "$(printf '%s' "$dev_sha" | tr '[:upper:]' '[:lower:]')" ]]; then+          echo "[$cur/$TOTAL] Skipping $pkg (identical APK already installed, $(awk "BEGIN {printf \"%.1f\", $local_size/1048576}") MB)."+          return 2+        fi+      fi+    fi+    install_apk_via_session "$apk" "$pkg" "$cur" "$TOTAL" "$mod"   else     echo "[$cur/$TOTAL] Error: no APK found for :${mod} (assemble failed?)."     return 1@@ -800,31 +919,55 @@ install_one() { } if [[ "$INSTALL_JOBS" -gt 1 && $TOTAL -gt 1 ]]; then   FAIL_DIR="$(mktemp -d)"-  idx=0-  while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do-    wave_end=$((idx + INSTALL_JOBS))+  SKIP_DIR="$(mktemp -d)"+  pos=0+  oi=0+  while [[ $oi -lt ${#INSTALL_ORDER[@]} ]]; do+    wave_end=$((oi + INSTALL_JOBS))     if [[ $wave_end -gt $TOTAL ]]; then wave_end=$TOTAL; fi-    while [[ $idx -lt $wave_end ]]; do+    while [[ $oi -lt $wave_end ]]; do+      idx="${INSTALL_ORDER[$oi]}"       mod="${NORMALIZED_MODULES[$idx]}"-      ( install_one "$mod" "$((idx + 1))" || touch "$FAIL_DIR/$idx" ) &-      idx=$((idx + 1))+      pos=$((pos + 1))+      ( rc=0; install_one "$mod" "$pos" || rc=$?+        if [[ $rc -eq 2 ]]; then touch "$SKIP_DIR/$idx";+        elif [[ $rc -ne 0 ]]; then touch "$FAIL_DIR/$idx"; fi ) &+      oi=$((oi + 1))     done     wait   done   FAILURES=$(find "$FAIL_DIR" -type f | wc -l | tr -d ' ')-  rm -rf "$FAIL_DIR"+  SKIPPED=$(find "$SKIP_DIR" -type f | wc -l | tr -d ' ')+  rm -rf "$FAIL_DIR" "$SKIP_DIR" else-  idx=0-  while [[ $idx -lt ${#NORMALIZED_MODULES[@]} ]]; do-    install_one "${NORMALIZED_MODULES[$idx]}" "$((idx + 1))" || FAILURES=$((FAILURES + 1))-    idx=$((idx + 1))+  pos=0+  oi=0+  while [[ $oi -lt ${#INSTALL_ORDER[@]} ]]; do+    idx="${INSTALL_ORDER[$oi]}"+    pos=$((pos + 1))+    rc=0+    install_one "${NORMALIZED_MODULES[$idx]}" "$pos" || rc=$?+    if [[ $rc -eq 2 ]]; then+      SKIPPED=$((SKIPPED + 1))+    elif [[ $rc -ne 0 ]]; then+      FAILURES=$((FAILURES + 1))+    fi+    oi=$((oi + 1))   done fi+if [[ -n "$SKIP_PROBE_FILE" ]]; then+  rm -f "$SKIP_PROBE_FILE"+fi+INSTALLED_COUNT=$((TOTAL - FAILURES - SKIPPED))+INSTALL_SECS=$(( $(date +%s) - INSTALL_T0 ))+echo ""+echo "Install phase: ${INSTALL_SECS}s for $TOTAL app(s): $INSTALLED_COUNT installed, $SKIPPED skipped (unchanged), $FAILURES failed." 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."+if [[ $SKIPPED -gt 0 ]]; then+  echo "Install complete: $INSTALLED_COUNT installed, $SKIPPED skipped (unchanged) of $TOTAL."+else+  echo "Install complete: $TOTAL/$TOTAL installed successfully."+fi exit $BUILD_EXIT