Android app tests
A real Android in a box for Appium tests, a fresh phone in a second from a snapshot, and the same run in GitHub Actions with one API key.
The android image is the base image plus Android: Android 14 runs as a container (Redroid) on the box's own kernel, so nothing is emulated and it is fast. Appium with the UiAutomator2 driver, Java 17, Maven and adb come with it. You keep building the app and writing the tests on your own machine; the box is the phone they run on.
A phone in a box
kvbox new phone --base android --size l
kvbox exec phone -- adb devicesList of devices attached
localhost:5600 deviceThe first boot downloads Android (about 1 GB) and starts it, a few minutes. Do it once, then snapshot the box: every phone after that starts from the snapshot, already booted, in about a second.
kvbox snapshots new phone android-ready
kvbox new --from android-ready run1Your app on it
Build a debug APK (Build → Build APK(s) in Android Studio, or ./gradlew :app:assembleDebug), copy it in and install it:
kvbox cp app/build/outputs/apk/debug/app-debug.apk run1:app.apk
kvbox exec run1 -- adb install -r app.apk-r replaces the app with the new build and keeps its data; adb uninstall com.example.app first for a clean start, or take a new box from the snapshot. The box is x86_64: an app with native libraries needs x86_64 in its abiFilters.
To find the ids a test can use, open the app and dump the screen:
kvbox exec run1 -- adb shell am start -n com.example.app/.MainActivity
kvbox exec run1 -- 'adb shell uiautomator dump /sdcard/ui.xml && adb shell cat /sdcard/ui.xml'Each field and button shows its resource-id. In a Jetpack Compose app, give elements Modifier.testTag("email") and set Modifier.semantics { testTagsAsResourceId = true } on the screen's root; the id is then the tag.
A first test
A Maven project with the Appium Java client:
login-test/
pom.xml
src/test/java/LoginTest.java<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>login-test</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>9.3.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>
</project>import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.net.URL;
import java.nio.file.*;
import java.time.Duration;
class LoginTest {
AndroidDriver phone;
static By id(String id) {
return AppiumBy.androidUIAutomator("new UiSelector().resourceId(\"" + id + "\")");
}
@BeforeEach
void open() throws Exception {
var options = new UiAutomator2Options()
.setUdid("localhost:5600") // the phone in the box
.setAppPackage("com.example.app")
.setAppActivity(".MainActivity");
phone = new AndroidDriver(new URL("http://localhost:4723"), options); // Appium in the box
}
@Test
void login() throws Exception {
phone.findElement(id("email")).sendKeys("test@example.com");
phone.findElement(id("password")).sendKeys("secret123");
phone.findElement(id("login")).click();
try {
new WebDriverWait(phone, Duration.ofSeconds(10)).until(ExpectedConditions.presenceOfElementLocated(
AppiumBy.androidUIAutomator("new UiSelector().textContains(\"Welcome\")")));
} finally {
File shot = phone.getScreenshotAs(OutputType.FILE);
Files.copy(shot.toPath(), Path.of("after-login.png"), StandardCopyOption.REPLACE_EXISTING);
}
}
@AfterEach
void close() { if (phone != null) phone.quit(); }
}Run it in the box and take the results back:
kvbox cp -r login-test run1:login-test
kvbox exec run1 -- 'cd login-test && mvn -q test'
kvbox cp run1:login-test/after-login.png .
kvbox cp -r run1:login-test/target/surefire-reports .after-login.png is the phone's screen at the end of the test: the page after login when it passed, the screen where it got stuck when it failed. The first run downloads the Maven dependencies; snapshot the box after it and later runs skip that. When something fails for no visible reason, kvbox exec run1 -- adb logcat -d has the phone's log.
Appium listens on port 4723 inside the box and has no login of its own. To drive it from your machine (Appium Inspector, tests run locally), publish the port only while you need it.
In CI
The same steps run in GitHub Actions. GitHub's runner builds the APK; a fresh box from your snapshot is the phone, removed when the job ends. The only kvbox secret is an API key: kvbox key create ci --expires never, saved as the repo secret KVBOX_TOKEN. Keep the test project in the app's repo (here uitests/), so tests change with the app.
name: UI tests
on: pull_request
jobs:
ui-tests:
runs-on: ubuntu-latest
env:
KVBOX_TOKEN: ${{ secrets.KVBOX_TOKEN }}
BOX: ci-${{ github.run_id }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with: { distribution: temurin, java-version: 17 }
- run: ./gradlew :app:assembleDebug
- run: npm install -g @kvbox/cli
- run: kvbox new $BOX --from android-ready
- run: |
kvbox cp app/build/outputs/apk/debug/app-debug.apk $BOX:app.apk
kvbox exec $BOX -- adb install -r app.apk
kvbox cp -r uitests $BOX:uitests
kvbox exec $BOX -- 'cd uitests && mvn -q test'
- if: always()
run: |
mkdir -p reports
kvbox cp -r $BOX:uitests/target/surefire-reports reports/ || true
kvbox exec $BOX -- 'cd uitests && tar -cf - *.png' | tar -xf - -C reports || true
- if: always()
uses: actions/upload-artifact@v4
with: { name: ui-test-results, path: reports }
- if: always()
run: kvbox rm $BOX -ykvbox exec exits with the command's exit code, so a failing test fails the job; the screenshots and reports are on the run's page as ui-test-results. The box exists only while the job runs. For a snapshot in an org, add --org <org> to kvbox new.
What it cannot test
- Google Play services: there are none, so Google sign-in, Firebase push (FCM), Google Maps and the SMS consent API do not work. Test those flows on a device farm (Firebase Test Lab, BrowserStack) and everything else here.
- ARM-only apps: the box is x86_64, so an APK whose native libraries are ARM-only does not install.
- GPU: there is none, the screen is drawn in software. Fine for UI tests, not for games or graphics benchmarks.
Changing the phone
Two box variables change what Android runs, at the box's next start:
kvbox env set REDROID_ARGS "androidboot.redroid_width=1080 androidboot.redroid_height=2400" --box phone
kvbox env set REDROID_IMAGE redroid/redroid:13.0.0-latest --box phone
kvbox stop phone && kvbox start phoneThe app's data is kept across restarts. If Android does not come up, kvbox exec phone -- journalctl -u kvbox-android says why.