# Android Run Android Studio's Run 'app' for Herdr. Press Shift+F10 in an Android project and it builds your variant, installs it on the connected phone or emulator and launches it, with the build log one click away. With nothing connected it offers Studio's Wi-Fi pairing (a QR code, or a pairing code with the phone's address filled in) or your emulator. Outside Android projects it stays out of the way. ![Android Run](preview.png) This file is the brief: enough to build Android Run from an empty folder. The files next to it are one build of it. If they don't work on your Herdr, or you want it different, build it again from here. ## What you see - **The bar**, a block about 330px wide and 28px high, meant for the right end of a status bar. Background `chrome`, 8px side padding, right-aligned, 4px gaps. Each control is 26px high with 8px side padding, 5px gaps and 6px corners, 12px text in `muted`, `hover` background on hover. It holds one of: - **Nothing**, when the focused space has no Android project. - **Idle**: a 14px play icon and "Run", with the tooltip "Shift+F10". - **Running**: a 12px ring spinner in `working` (`mods/workspace-chrome/spinner.svg`, `.spin(900)`) and the step, truncated at 240px: "Reading build variants…", "Choose a build variant", "Looking for a device…", "Reconnecting Wi-Fi devices…", "Pairing a device…", "Paired. Connecting…", "Booting Small_Phone…", "Building devDebug", "Installing on Pixel_9". - **Finished**: a `✓` in `done` beside Run for 10 seconds, with the tooltip "Launched devDebug on Pixel_9". - **Failed**: `✗` and the message in `ansi1`, truncated at 240px with the full text as a tooltip, beside Run, until the next run. For example "Build failed: 2 errors" or "Install failed on Pixel_9". - **The menu**, a 170x72 popup with two rows drawn like Herdr's context menu (31px rows, 7px corners, 12.8px text, 14px `muted` icons): "Show log" (`mods/workspace-chrome/terminal.svg`) and "Stop" (`square.svg`). - **Build variant**, a dialog 520px wide, 70px plus 30px per row, at most 460px: the title and a list such as `:app devDebug`, with `:app` before other modules and debug variants before the rest. - **No device connected**, the same dialog with "Pair device using Wi-Fi" and, when an emulator exists, "Start emulator Small_Phone". - **Pair device using Wi-Fi**, a dialog of 520x480 on `popover`, 16px padding, 10px gaps, 13px text: - The 15px semibold title, then two clickable tabs, "QR code" and "Pairing code", the shown one on `selected`. - One 12px `muted` line saying where the matching screen is on the phone, such as "On the phone: Developer options > Wireless debugging > Pair device with QR code". - QR tab: a 280px QR code, black on white with its quiet zone. - Code tab: "IP address and port" and "Pairing code" fields (34px high, 8px corners, `input` border on `background`) and a "Pair" button in `solid`. With more than one phone found, their addresses and names are listed above the fields. - At the bottom, one 12px line: "Waiting for the phone to scan the code…", "Looking for a phone showing a pairing code…" or "Phone found. Enter the code it shows." in `muted`, and errors in `ansi1`. ## How it behaves - **Which project.** The focused pane's folder first, then the other panes of its tab and space, then the space's folder, each walking up its parents. A folder counts when it has `settings.gradle` or `settings.gradle.kts`, and either `app/src/main/AndroidManifest.xml` or `com.android.` in `gradle/libs.versions.toml`, the root build file or `app`'s. With no project, or in a space on another machine, the key goes on to the focused terminal as if it weren't bound. - **The run.** Pressing the key or clicking Run stops any run in progress and starts over: 1. The variant saved for this project. The first time, it reads them with `android describe` and asks. 2. A device: the first physical one in `adb devices`, else an emulator. With none, it tries `adb connect` on every advertised `_adb-tls-connect._tcp` address and looks again, and only then asks. 3. `./gradlew ::assemble --console=plain` (or `gradle` without a wrapper). 4. `android run --apks= --device= --install-options=-d`, plus `--activity=` when the manifest names a launcher. - **While it runs.** Clicking the chip opens the menu. Show log peeks at the step's terminal and Stop kills it. Before a step has a terminal (looking for a device, connecting after pairing) the click does nothing. A click on `✓` or `✗` opens the log directly, and the terminal stays until the next run. A failure also posts a toast that opens the log. - **Pairing.** The dialog checks every second for phones advertising `_adb-tls-pairing._tcp` and for connected devices. - It opens on the QR tab. When the QR's name is advertised, meaning the phone scanned it, it pairs by itself. - When a phone opens its pairing code screen, the dialog switches to the code tab, fills in the address and puts the cursor in the code field. Reopening the phone's screen, which gets a new port, updates the address. An address you typed stays. - Enter or Pair runs `adb pair`. A failure shows in the dialog and clears the code field so you can try again. A failed QR pairing also makes a new QR code. - A phone paired earlier that connects by itself closes the dialog, and the run continues. Esc cancels the run. - **Reloads.** A step is kept in shared state, so a build started before a mod reload or a layout switch still goes on to the install. - Agent statuses play no part, and the spinner is the only animation. ## Built from - **One mod**, `mods/android-run/`: `main.js` (the view and the run), `android.js` (project detection, adb, APK lookup), `run-button.js` (the bar), `menu.js`, `dialog.js`, `pair.js`, `play.svg` (lucide's play icon) and `mod.json` (`place: "none"`, so it only shows where a layout puts it). One view with four roles: with `props().overlay` set it draws the overlay named by `props().view` (`choose`, `pair` or `menu`), otherwise the bar. - **`keys.json`**: `{ "global": { "shift-f10": "android-run.run" } }`. - **A block in a layout you own** (clone a shipped one first with `layout.clone`). `.` reaches shown copies only, so without this block the key does nothing. For example, a status row at the bottom of the work column: ```json { "id": "status-bar", "type": "row", "size": { "px": 28 }, "resizable": false, "children": [ { "id": "status", "type": "block", "content": { "mod": "workspace-status" } }, { "id": "android-run", "type": "block", "size": { "px": 330 }, "content": { "mod": "android-run", "props": { "border": "top" } } } ] } ``` - **Props of that block**, all optional: - `border`: `"top"` or `"bottom"` draws a 1px `border` line on that edge, to match a bar beside it. - `bar: false` draws nothing. To put the button among a tab bar's own, keep a copy of `run-button.js` in a tab bar mod you own (a mod can't import from another mod's folder), add `.children(runButtons())` to its row, and give this block `"size": { "px": 0 }` and `bar: false`. A zero-width block counts as shown. - `targets`: a file under home for the saved variants, instead of `/data/android-run/targets.json`. The format is `{ "": { "module", "variant", "listing" } }`. - `passKeyTo`: program keys, such as `["nvim"]`, that keep Shift+F10 for their own Run when focused. - **Terminals.** Gradle, `android describe`, `android run` and the emulator run in a core terminal named `android-run`: `hc run --bg --json --no-parent --no-notify --rm --name android-run --cwd -- sh -c sh `. `` runs the command, puts the exit code in the terminal's title (`android-run|`) and waits for Enter. The mod reads the title from `terminals()`. - **State.** Shared `android-run.run` holds the step (`prepare`, `build`, `install`, `done`, `error`), its message and its terminal. Shared `android-run.project` holds the focused project's root, for the bar. Shared `android-run.answer` carries a dialog's answer back. A 300ms timer watches the terminal and starts the next step. - **The APK.** `android describe` gives each variant's output listing file: it names `output-metadata.json`, whose element without ABI filters (else the first) is the APK. The launcher is the first enabled MAIN/LAUNCHER activity in `build/intermediates/packaged_manifests//*/AndroidManifest.xml`. - **The QR code.** `WIFI:T:ADB;S:studio-<10 random>;P:<12 random>;;`, as Studio makes it, through `qrencode -t PNG -s 20 -m 4` into `/data/android-run/qr-.png`, deleted when the dialog closes. - **Programs**: `android` ([Android CLI](https://developer.android.com/tools/agents)) and `adb` are required, and `adb` may also come from `ANDROID_HOME`, `ANDROID_SDK_ROOT`, `local.properties` or `~/Android/Sdk`. `avahi-browse` and `qrencode` are optional. `hc`, `sh`, `timeout`, `setsid`, `sleep`, `ls`, `test`, `mkdir` and `rm` are used too. - **Herdr functions**: `exec`, `hc`, `readTextFile`, `writeTextFile`, `home`, `appHome`, `terminals`, `panes`, `spaces`, `focused`, `shared`, `setShared`, `nextCommand`, `notify`, `openOverlay`, `closeOverlay`, `props`, `run`, `theme`; `image`, `svg`, `InputState`, `Input`, `.spin`, `.tooltip`. ## Watch out - **It runs programs and reads outside its folder.** It runs your project's Gradle wrapper, which executes the project's build scripts, and `android`, `adb`, `avahi-browse`, `qrencode` and `hc`. It reads the project's Gradle files, manifests and build output, and writes under `/data/android-run/`. It reaches the network only through mDNS discovery and `adb` on your local network. Pairing codes and the QR password go to `adb pair` as arguments. - **`exec` stops at 30 seconds**, so anything longer runs in a terminal. The terminal stays open after the command because a peek needs a live one. - **`terminals()` lags just after `hc run`** and can still list a killed terminal. Follow a step by the address `hc run --json` returned, and give a new terminal 5 seconds to appear before treating it as gone. Reading by name can pick up the previous terminal's exit code. - **adb's mDNS list can stay empty for over a minute** while the phone is reachable, so `avahi-browse -rpt` is asked too, and first, since a list that far behind can hold an old port. - **A phone advertises its pairing service only while that screen is open**, usually after the dialog opened. Looking once finds nothing, so keep polling. - **Some phones can't scan the code.** A vivo V2514 on Android 16 read neither this QR code nor Android Studio's, while ZXing decodes both. Pairing code works there, and the dialog switches to it by itself. - **`--install-options=-d`** lets a branch switch install an older `versionCode` over a newer one, which otherwise fails with `INSTALL_FAILED_VERSION_DOWNGRADE`. The system honours it for debuggable packages only, so a release variant still refuses. - **`android run` refuses to guess** when an APK has two launcher entries ("Specify which component to launch"), and LeakCanary adds one. That is why `--activity=` is passed. - **The key is passed on by name.** Rebinding the key in `keys.json` also means changing `KEY` in `main.js`, so that other projects still get the key you pressed. - **There's no command to change a variant yet.** Remove the project's entry from `targets.json` and the next run asks again. - **A log open during the build closes when the install starts**, because the install gets its own terminal. - **"Paired. Connecting…" can't be cancelled.** It retries 30 times, 90 seconds or more, before it fails. - **Overlays.** - Focus given while an overlay is still opening can be lost, so the pairing dialog focuses again after 600ms. - Tab in the fields moves between them and doesn't reach `on_key_down`, so the tabs are click-only. - Images are cached by path, so each QR code needs a new file name. - A popup without `at` opens at the pointer and is pushed back into the window, here over the chip. - The running chip has no tooltip, because one stays on top of the menu's first row. - **Linux.** Built and used on Linux. Stock macOS has neither `timeout` nor `setsid`, so reconnecting, avahi and the emulator path would need changes there. - **What has run for real.** Build, delta install and launch have run on a real phone. Wi-Fi pairing and starting an emulator have only run against stand-in tools so far, though real adb and avahi found a real phone's connect service. ## Done when - `herdr-app check mods/android-run` passes, and `herdr-app status` shows no errors from the mod. - The bar shows Run in a space with an Android project and nothing in other spaces. - Shift+F10 in an Android project asks for a variant once, builds it, installs and launches it on the connected device, shows `✓` for 10 seconds, and remembers the variant. - In a project of another kind, Shift+F10 starts nothing and the terminal gets the key. - During a build, clicking the chip offers Show log and Stop. Show log opens the Gradle output. Stop ends the build and brings Run back. - A failing build shows `✗ Build failed: N errors` and a toast that opens the log. - With no device, the dialog offers pairing. Opening "Pair device with pairing code" on the phone switches it to the code tab with the address filled in, and the right code pairs and continues the run. A wrong code shows an error and keeps the dialog open. - With `qrencode` missing, the QR tab says so and the code tab still works. - `{ "bar": false }` on the block in a `{ "px": 0 }` slot draws nothing, and the key still runs.