Skip to content

feat(mobile): add an Android emulator harness for device-only repros - #13366

Open
makoto-developer wants to merge 3 commits into
stablyai:mainfrom
makoto-developer:feat/mobile-android-emulator-harness
Open

makoto-developer wants to merge 3 commits into
stablyai:mainfrom
makoto-developer:feat/mobile-android-emulator-harness

Conversation

@makoto-developer

Copy link
Copy Markdown
Contributor

Summary

Adds pnpm start:emulator:android, so an Android repro is one command instead of an afternoon.

Mobile bugs that only appear under an Android IME keep stalling on "no Android device here":

start:emulator drives the iOS simulator only (--device 'iPhone 17 Pro'), and the sole other Android script packages a release, so every contributor rebuilds the same setup from scratch. I just did, while chasing #7427 on Android, and this is that setup with the traps encoded.

The script boots or reuses an AVD, waits for sys.boot_completed, reverses the Metro and mock-host ports, and prints the exact next commands. Flags: --avd <name>, --port <n> (repeatable), --no-reverse.

▸ SDK root: /Users/me/Library/Android/sdk
▸ AVD home: /Users/me/.config/.android/avd
▸ Booting AVD orca-android
▸ Reversed ports: 8081, 6768

✔ Emulator ready: emulator-5554

What is unit-tested rather than discovered on a cold machine. Path and output parsing live in android-emulator-environment.ts so the parts that go wrong silently are pinned:

  • AVDs are resolved the way avdmanager writes them ($XDG_CONFIG_HOME/.android/avd), and that path is passed to every spawned tool. Reporting it is not enough — the emulator reads its own env, so -list-avds otherwise returns nothing for an AVD that was just created. I shipped this bug into my own first draft and only caught it by running the script.
  • adb output is trusted only when a device reports device, so a still-booting emulator (offline) and a physical handset are both skipped.
  • JAVA_HOME is checked against the range AGP accepts. JDK 24+ aborts configureCMakeDebug with a restricted-method error that reads like an unrelated native failure.
  • An explicit --avd is no longer ignored when a different AVD is already running; the script names the one to stop.

README gains the prerequisites and the four traps that cost the most time: the build JDK, the Play Store image Gboard needs for CJK input, the AVD path, and the mock host colliding with a running Orca desktop on 6768.

Screenshots

No visual change — this is developer tooling.

Testing

  • pnpm lint (mobile oxlint, clean)
  • pnpm typecheck (tsc --noEmit in mobile/)
  • pnpm test — mobile 3401 passed, 11 of them new. One pre-existing unrelated failure, src/mock-server-key-pair.test.ts > makes concurrent creators converge on the persisted winner, which fails identically on an unmodified tree.
  • pnpm build — not run; adds a dev script and its tests, no app code.
  • Added tests that would catch regressions

Exercised end to end on macOS 15 / Apple silicon against a Pixel 7 API 36 AVD: cold boot, reuse of a running emulator, --avd match and mismatch, missing AVD, and the JDK 17 (silent) and JDK 25 (warned) branches.

AI Review Report

Reviewed with Claude.

  • Cross-platform. SDK root has a per-platform default (%LOCALAPPDATA%\Android\Sdk, ~/Library/Android/sdk, ~/Android/Sdk), tool paths get .exe for the native binaries and .bat for the avdmanager wrapper, and every path is built with path.join. The review caught a ${JAVA_HOME}/bin/java template literal that would have broken on Windows; it now uses path.join and java.exe. All three platform branches are asserted in tests. Only macOS was exercised for real — Linux and Windows rest on the unit tests, which is the honest limit here.
  • Correctness. Two defects came out of running the script rather than reading it: the AVD-home env not reaching the spawned emulator, and --avd being silently ignored when any emulator was already up. Both fixed and covered.
  • Scope. No app code, no runtime dependency, one package.json script. Nothing here executes during a build or in the shipped app.
  • Performance. Polls adb every 2s with a 5-minute ceiling; no busy loop.
  • Resource cleanup. The emulator is spawned detached and unref'd on purpose, so the script exits while the emulator keeps running — that is the intended workflow, and the serial is printed so it can be killed with adb emu kill.
  • SSH / remote / providers / agents. Untouched.

Security Audit

Reviewed with Claude.

  • Command execution. execFile/spawn only, never a shell, so no interpolation reaches a command line. Executable paths are derived from ANDROID_HOME or a platform default and are existsSync-checked before use.
  • Input handling. --avd and --port are compared and range-checked, not interpolated into commands; ports must be positive integers.
  • Env handling. The child env is process.env plus two Android path variables. No secrets are read, written, or logged; the script prints only paths, an AVD name and a device serial.
  • Network. adb reverse maps loopback ports into the emulator the same way expo run:android already does. It opens nothing to the network.
  • Paths / auth / IPC / dependencies. No new dependencies. No credential or IPC surface.

Notes

  • This is unsolicited tooling, so say the word if it is not wanted and I will close it — the README section alone would still be worth keeping.
  • The JDK ceiling is a version-sensitive constant (MAX_SUPPORTED_BUILD_JDK). It is a warning, never a hard stop, so a future AGP that supports newer JDKs makes it stale rather than wrong.
  • Verified on macOS only. If you want Linux or Windows confirmation before merging, I would rather you know that up front than assume it.
  • Found while building this: mobile/scripts/mock-server-terminal-stream.ts answers terminal.send with { ok: true } while the client gates on accepted, so mock-based runs silently drop the carriage return after a flushed IME composition. That fix is in fix(mobile): hold the Japanese IME reading off the terminal until it commits #13070 rather than here, to keep this PR to tooling.

Mobile bugs that only appear under an Android IME keep stalling on "no Android
device here": stablyai#7427 sat unreproduced with the reporter's trace unverified, and
stablyai#7495 needed a hand-built emulator rig before it could be closed. `start:emulator`
only drives the iOS simulator, and the only other Android script packages a
release, so every contributor rebuilds the same setup from scratch.

Add `pnpm start:emulator:android`, which boots or reuses an AVD, waits for
sys.boot_completed, reverses the Metro and mock-host ports, and prints the exact
next commands. Path and output parsing live in a separate module so the parts
that silently go wrong are unit-tested rather than discovered on a cold machine:

- AVDs are resolved the way avdmanager writes them ($XDG_CONFIG_HOME/.android),
  and that path is passed to every tool the script spawns. Reporting it is not
  enough — the emulator reads its own env, so `-list-avds` otherwise comes back
  empty for an AVD that was just created.
- adb output is only trusted when a device reports `device`, so a still-booting
  emulator and a physical handset are both skipped.
- JAVA_HOME is checked against the range the Android Gradle plugin accepts.
  JDK 24+ aborts configureCMakeDebug with a restricted-method error that reads
  like an unrelated native build failure.

An explicitly requested --avd is no longer ignored when a different AVD is
already running; the script says which one to stop.

README gains the prerequisites and the four traps that cost the most time: the
build JDK, the Play Store image Gboard needs for CJK input, the AVD path, and
the mock host colliding with a running Orca desktop on 6768.
@makoto-developer
makoto-developer marked this pull request as ready for review August 12, 2026 17:03
@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1e4bd67e-5bea-41c6-8af4-5b2ed4436c3b

📥 Commits

Reviewing files that changed from the base of the PR and between 282d661 and 47441b1.

📒 Files selected for processing (2)
  • mobile/scripts/android-emulator-environment.ts
  • mobile/src/android-emulator-environment.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • mobile/src/android-emulator-environment.test.ts
  • mobile/scripts/android-emulator-environment.ts

📝 Walkthrough

Walkthrough

Adds cross-platform Android SDK, AVD, tool, emulator, and Java environment helpers with Vitest coverage. Adds a TypeScript command that validates tools, selects or starts an AVD, waits for boot completion, reverses development ports, and prints follow-up commands. Adds the start:emulator:android package script and documents emulator setup, command options, prerequisites, port handling, and keyboard configuration.

Mergeability Score: ⚪ Minimal · up to 47441

This PR adds a localized Android emulator helper for developer workflows without changing shipped application behavior; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: adding an Android emulator harness for device-only reproductions.
Description check ✅ Passed The description explains the change, rationale, testing, platform scope, security, and limitations, but it does not use the required template headings or provide a dedicated Fixes # entry.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Nitpick comments (3)
mobile/scripts/android-emulator-environment.ts (1)

1-3: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Shorten this file-level comment.

The file name and exports already state this responsibility. Keep only non-obvious rationale in one short line.

As per coding guidelines, “Comments must be concise, non-obvious, and brief—prefer one line; do not explain obvious behavior or walk through code.”

Source: Coding guidelines

mobile/src/android-emulator-environment.test.ts (1)

26-49: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove redundant Given/When/Then comments.

The test names and assertions already identify setup, action, and expected result. Keep comments only where they add non-obvious platform or emulator behavior.

As per coding guidelines, “Comments must be concise, non-obvious, and brief—prefer one line; do not explain obvious behavior or walk through code.”

Also applies to: 53-60, 63-83, 86-100, 102-165

Source: Coding guidelines

mobile/scripts/start-android-emulator.ts (1)

2-4: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Shorten this file-level comment.

The command name states the workflow. Keep only the non-obvious emulator limitation in one short line.

As per coding guidelines, “Comments must be concise, non-obvious, and brief—prefer one line; do not explain obvious behavior or walk through code.”

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3bd48262-d45b-40ad-84d3-0240f5f22b51

📥 Commits

Reviewing files that changed from the base of the PR and between bd9addb and bfb20b5.

📒 Files selected for processing (5)
  • mobile/README.md
  • mobile/package.json
  • mobile/scripts/android-emulator-environment.ts
  • mobile/scripts/start-android-emulator.ts
  • mobile/src/android-emulator-environment.test.ts

Comment thread mobile/README.md Resolved
Comment thread mobile/README.md Resolved
Review findings from the first revision:

- ANDROID_EMULATOR_HOME was ignored. `emulator -help-environment` documents it as
  the replacement for ~/.android, so it belongs between ANDROID_AVD_HOME and the
  XDG fallback.
- Reuse compared the running AVD with a strict equality while startup matches
  case-insensitively on a substring, so `--avd pixel` rejected the very
  Pixel_7_API_36 it would otherwise have booted. Both paths now share the rule.
- A failed `adb reverse` was still listed as reversed. Successful and failed
  ports are now reported separately.
- The README pinned arm64-v8a, which on an Intel or AMD host runs under
  translation with no hardware acceleration. Select the ABI for the host CPU.
- The 6768 workaround told the reader to move the mock host without mentioning
  that the new port has to be reversed too.
@makoto-developer

Copy link
Copy Markdown
Contributor Author

All five findings were valid; fixed in 282d66179.

  • ANDROID_EMULATOR_HOME ignored (🟠 Major). Checked it against emulator -help-environment, which documents it as the replacement for ~/.android. Precedence is now ANDROID_AVD_HOME → ANDROID_EMULATOR_HOME/avd → ANDROID_USER_HOME/avd → $XDG_CONFIG_HOME/.android/avd → ~/.android/avd, with the ordering pinned in tests.
  • README pinned arm64-v8a (🟠 Major). Right — on Intel or AMD that image runs under translation with no hardware acceleration, which is unusable for typing repros. The prerequisites now select the ABI for the host CPU.
  • Reuse used strict equality (🟡 Minor). --avd pixel rejected the Pixel_7_API_36 that startup would have selected. Both paths now go through selectAvdName, so the matching rule is the same on either branch.
  • Failed reversals reported as successful (🟡 Minor). Successful and failed ports are tracked separately now. Verified with a deliberately bad port: ▸ Reversed ports: 8081 / ! could not reverse: 99999.
  • 6768 workaround was incomplete (🟡 Minor). Correct — moving the mock host is useless without reversing the new port. The README now shows PORT=6769 pnpm mock-server alongside --port 8081 --port 6769.

Re-exercised on the emulator: reuse with a partial --avd, the mismatch failure, and the mixed-port reversal all behave as described. Unit tests still 11/11, tsc and oxlint clean.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d7ced9ae-3fa5-4d40-8c0b-32b74fc740fa

📥 Commits

Reviewing files that changed from the base of the PR and between bfb20b5 and 282d661.

📒 Files selected for processing (4)
  • mobile/README.md
  • mobile/scripts/android-emulator-environment.ts
  • mobile/scripts/start-android-emulator.ts
  • mobile/src/android-emulator-environment.test.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • mobile/src/android-emulator-environment.test.ts
  • mobile/scripts/start-android-emulator.ts
  • mobile/scripts/android-emulator-environment.ts

Comment thread mobile/README.md Resolved
…ness

An exported-but-empty ANDROID_EMULATOR_HOME or ANDROID_USER_HOME passed the
nullish check, so path.join resolved the AVD store to the relative './avd'
and every tool in the harness was pointed at a directory that does not
exist. Match the truthiness checks the rest of the module already uses.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants