Yonto Player is one codebase with an Android TV app and a phone and tablet app. Testing it means running both on emulators, repeatedly, and keeping the setup simple enough that it stays out of the way.
This post is the setup I would hand to anyone testing a TV app and a phone app side by side, especially with more than one checkout of the repo on the same machine. Most of it is plain adb, avdmanager and a short Python script, so it carries over to any Android project.
One emulator per kind per checkout
Each checkout gets its own devices, named after the kind of device and the folder they belong to: a TV, a phone, and a tablet when a change needs one. The name makes a device easy to find again the next time you work in that checkout.
Making one is cheap if you keep a base image around. On a Mac with APFS, a copy-on-write copy of the AVD folder is instant and takes no extra disk, so a clone is a copy plus a few edits: drop the lock files and snapshots, point the config at the new name, switch the hardware keyboard on, and write the small .ini that registers it. A simplified sketch of the idea, with generic names:
# Simplified sketch of cloning an AVD and booting it on a chosen port. KIND=phone # or tv, tablet SRC=base_$KIND NEW=${KIND}_$(basename "$PWD") PORT=5554 # an explicit port for this emulator cp -c -R ~/.android/avd/$SRC.avd ~/.android/avd/$NEW.avd # copy-on-write cd ~/.android/avd/$NEW.avd rm -rf *.lock snapshots # state that belongs to the source sed -i '' -e "s/^avd.id=.*/avd.id=$NEW/" \ -e "s/^avd.name=.*/avd.name=$NEW/" \ -e "s/^hw.keyboard=.*/hw.keyboard=yes/" config.ini # ...write ~/.android/avd/$NEW.ini pointing at this folder... emulator -avd $NEW -port $PORT -gpu host \ -no-snapshot-load -no-snapshot-save When there is no base to clone, avdmanager create avd with the right system image and device profile does the same job. Boot a clone cold the first time.
Give each emulator an explicit port
An emulator takes a console port (and its neighbour for adb), and its serial is emulator-<port>. Without -port, a restarted emulator takes the lowest free pair and its serial can change. So every boot passes -port, and the serial is something you know in advance and can put in every command.
Name the serial in test commands
The Android Gradle plugin's install* and connected* tasks run against all attached devices unless told otherwise. With several emulators booted, an unqualified installDebug installs to all of them, and a connected test run exercises all of them. Set ANDROID_SERIAL in the same command as the task, and pass -s to adb:
# Simplified sketch: the serial rides on the command itself. ANDROID_SERIAL=emulator-$PORT ./gradlew :app:tv:installDebug ANDROID_SERIAL=emulator-$PORT ./gradlew :app:tv:connectedDebugAndroidTest adb -s emulator-$PORT shell am start -n "$PACKAGE/$ACTIVITY" Putting it on the command line, rather than exporting it earlier in a shell session, makes each command complete by itself. An instrumented test run also uninstalls the app and its test APK when it finishes, which clears the app's data, so it is worth pointing at a device you have chosen on purpose.
Run with the host GPU
Pass -gpu host for smooth frames. An image whose config says auto can fall back to software rendering, which is noticeably slower on a tablet image. Even with host rendering, emulator timing is not device timing, so I treat it as good for comparing one run with another and not as a number to quote.
Measure time with a script, not a screenshot
A screenshot tells you what the screen looked like when the capture finished, which is some time after you asked. Controls that hide themselves after a few seconds, such as a player's, can be gone by the time a tap arrives, and a still image cannot tell you that. When the question is about timing, I write the sequence as commands with their own clock: tap, wait for a label, read the state. The answer is then a number from a script that can be repeated, not an impression from a picture.
A scripted smoke test
The same idea produces the smoke test. It is a small Python script that takes the form (phone or tv), a serial and optionally an APK, so it runs after a build and takes a minute or two. It does this:
- Installs the APK over what is there, if one is given.
- Clears the app's data with
pm clear, so every run is a first launch on the demo library of open films. - Clears the log and starts the app.
- Walks the flow, taking a numbered screenshot at each step.
On the phone the flow covers Home, Browse, Search for a title, the title page, playback, backing out of the player, and Settings. On the TV it presses OK on the hero's Play, checks playback, and goes Back to Home.
Most steps wait for a label the screen should show, read from uiautomator dump, rather than sleeping for a fixed delay. The one exception is the TV's Play press, whose focus is not a labelled node, so the script cannot wait for it.
Playback is checked by evidence, not by a picture. On the phone, the app's media session is read with dumpsys media_session, and the test passes when it is in the playing state and the position has moved by more than a second and a half. The TV does not publish a media session, so there the test checks that the app's audio track started and kept running for five seconds. That proves audio is running, not that frames advance, so the screenshot of the step remains the check for the picture.
When a step fails, the script keeps one more screenshot named failure, checks the crash log for the app's own crash, prints a single FAIL line with the reason and the screenshot path, and exits with code 1. A pass exits 0.
The smoke test does not judge whether a screen looks good. It says the basics hold, so that attention goes to what a script cannot judge.
Holding a key like a remote
A TV is driven by a D-pad, and the interesting behaviour is often in what happens when a key is held. adb shell input keyevent sends one press, which is not the same thing: a real remote sends a key down, then repeats every few tens of milliseconds, then a key up.
For this I keep a small Java tool. A script compiles it to a dex file, pushes it to the device and runs it under app_process as the shell user, which is allowed to inject input. The tool sends the down, waits 400 ms for the first repeat, flags that first repeat as a long press, repeats every 50 ms by default, and ends with the up. The key is a normal KeyEvent name, so holding Up for three seconds looks like this:
ANDROID_SERIAL=emulator-$PORT hold-key-helper DPAD_UP 3000 An optional third argument sets the interval between repeats. A shorter one, 16 ms for example, stands in for a slow box that cannot finish a frame between repeats, so you can check how a screen behaves when the input arrives faster than it can draw.
What you can take from this
A device per checkout, an explicit port, a named serial, host rendering and a label-driven smoke script are each small, and together they give you a repeatable first check after a build: a command with a clear pass or fail and a screenshot of each step.


