Android Emulator Development, Anbox, & Waydroid

One-Click Deploy: Develop ADB Scripts to Automate App Installation and E2E Testing on Unlimited Emulators

Google AdSense Native Placement - Horizontal Top-Post banner

Introduction to Automated Multi-Emulator Testing

In the fast-paced world of Android development, ensuring your application performs flawlessly across a myriad of devices and Android versions is paramount. Manual testing on even a handful of emulators can be a time-consuming, error-prone, and incredibly tedious process. This is where the power of Android Debug Bridge (ADB) scripting comes into play. By leveraging ADB, developers and QA engineers can create robust, “one-click” deployment and end-to-end (E2E) testing solutions, automating the installation, launch, interaction, and uninstallation of applications across an unlimited number of emulators.

This article will guide you through the process of developing powerful ADB shell scripts to streamline your testing workflow. We’ll cover everything from setting up your environment to crafting a comprehensive script that identifies active emulators, installs your APK, simulates user interactions, captures screenshots for verification, and cleans up after itself. Get ready to transform your Android testing strategy.

The Challenge of Multi-Emulator Testing

Consider a scenario where you need to test a new build of your application on Android 9, 10, 11, 12, and 13, each running on both a phone and a tablet form factor. That’s ten separate test environments. Manually installing the APK, navigating through the app, performing a set of E2E steps, and collecting results for each can take hours, potentially delaying your release cycle. Any slight deviation in manual execution can lead to inconsistent test results, making it difficult to pinpoint genuine bugs. ADB automation provides a consistent, repeatable, and significantly faster alternative.

Prerequisites for ADB Automation

Before diving into script development, ensure you have the following tools and configurations in place:

  • Android SDK Platform-Tools: This package includes ADB. Ensure it’s installed and its path is added to your system’s PATH environment variable. You can typically find it within your Android SDK installation (e.g., /path/to/android-sdk/platform-tools).
  • Android Studio: While not strictly required for ADB itself, Android Studio’s AVD Manager is the easiest way to create and manage Android Virtual Devices (AVDs).
  • Multiple Android Emulators: Have several AVDs configured and ready to launch. For demonstration, we’ll assume you have at least two running concurrently.
  • An APK File: The application package you wish to test (e.g., my-app.apk).
  • Basic Shell Scripting Knowledge: Familiarity with Bash (Linux/macOS) or PowerShell/Batch (Windows) is beneficial.

Setting Up Your Android Emulator Environment

To launch multiple emulators, you can either do it through Android Studio’s AVD Manager or directly via the command line. For scripting, the command line is often preferred:

emulator -avd Pixel_2_API_30 -port 5554 &emulator -avd Pixel_3_API_33 -port 5556 &

The & symbol allows the command to run in the background, letting you launch multiple emulators simultaneously. The -port argument helps distinguish between emulators, although ADB usually handles this automatically if launched sequentially.

ADB Essentials for Scripting

ADB provides a rich set of commands crucial for automation. Here are some fundamental ones we’ll use:

  • adb devices

    Lists all connected devices and emulators. This is key for identifying your target environments.

    $ adb devicesList of devices attachedemulator-5554 deviceemulator-5556 device
  • adb -s <device_id> <command>

    Executes a command on a specific device. This is vital when multiple emulators are running.

    adb -s emulator-5554 install my-app.apk
  • adb install <path_to_apk>

    Installs an Android application package.

  • adb uninstall <package_name>

    Uninstalls an application.

  • adb shell am start -n <package_name>/<activity_name>

    Launches a specific activity of an application. You’ll need the package name (e.g., com.example.myapp) and the main activity name (e.g., com.example.myapp.MainActivity). You can often find these in your app’s AndroidManifest.xml or by using aapt dump badging my-app.apk.

  • adb shell input tap <x> <y>

    Simulates a tap event at screen coordinates (x, y). You might need to adjust these based on screen density and layout.

  • adb shell input text <string>

    Simulates typing text into an active input field.

  • adb shell input keyevent <key_code>

    Simulates a key press (e.g., 4 for the back button, 66 for Enter).

  • adb shell screencap -p <path_on_device>

    Captures a screenshot and saves it to the specified path on the device.

  • adb pull <path_on_device> <path_on_host>

    Copies a file from the device to your host machine.

Crafting Your Automation Script: A Step-by-Step Guide

Let’s build a Bash script that automates the installation, a simple login E2E test, and uninstallation across all active emulators. We’ll call this script automate_e2e.sh.

1. Listing Connected Devices

The first step is to get a list of all active emulators.

#!/bin/bashADB_PATH="/path/to/android-sdk/platform-tools/adb" # Adjust this path as neededAPK_FILE="my-app.apk"APP_PACKAGE="com.example.myapp"MAIN_ACTIVITY="com.example.myapp.MainActivity"DEVICES=$($ADB_PATH devices | grep emulator | awk '{print $1}')if [ -z "$DEVICES" ]; then  echo "No emulators found. Please ensure emulators are running."  exit 1fiecho "Found emulators:"for DEVICE in $DEVICES; do  echo "- $DEVICE"done

2. Installing the Application

We’ll create a function to handle the installation and launch.

# ... (previous code)install_and_launch_app() {  local device=$1  echo "[${device}] Installing ${APK_FILE}..."  $ADB_PATH -s "$device" install -r "${APK_FILE}" >/dev/null 2>&1  if [ $? -ne 0 ]; then    echo "[${device}] Installation failed. Attempting to uninstall previous version..."    $ADB_PATH -s "$device" uninstall "${APP_PACKAGE}" >/dev/null 2>&1    $ADB_PATH -s "$device" install "${APK_FILE}" >/dev/null 2>&1    if [ $? -ne 0 ]; then      echo "[${device}] Critical: Installation failed after uninstall. Skipping device."      return 1    fi  fi  echo "[${device}] Launching ${APP_PACKAGE}/${MAIN_ACTIVITY}..."  $ADB_PATH -s "$device" shell am start -n "${APP_PACKAGE}/${MAIN_ACTIVITY}" >/dev/null 2>&1  sleep 3 # Give app time to launch  return 0}

3. Simulating User Interaction for E2E Tests

For our hypothetical app, let’s simulate a simple login sequence: tap username field, enter text, tap password field, enter text, tap login button. *Note: You’ll need to find the correct coordinates (x, y) for your specific app’s UI elements.* These can be determined using tools like Layout Inspector in Android Studio or by taking a screenshot and manually identifying pixel coordinates.

# ... (previous code)run_e2e_test() {  local device=$1  echo "[${device}] Running E2E test scenario (Login)..."  # Tap username field (example coordinates)  $ADB_PATH -s "$device" shell input tap 300 500  sleep 1  $ADB_PATH -s "$device" shell input text "testuser"  sleep 1  # Tap password field (example coordinates)  $ADB_PATH -s "$device" shell input tap 300 650  sleep 1  $ADB_PATH -s "$device" shell input text "password123"  sleep 1  # Tap login button (example coordinates)  $ADB_PATH -s "$device" shell input tap 500 800  sleep 5 # Wait for login process  echo "[${device}] E2E test steps completed."  return 0}

4. Capturing and Verifying Results

A simple verification step is to take a screenshot and pull it to the host machine. You can then manually inspect it or integrate image comparison tools.

# ... (previous code)capture_screenshot() {  local device=$1  local screenshot_name="${device}_result.png"  echo "[${device}] Capturing screenshot..."  $ADB_PATH -s "$device" shell screencap -p /sdcard/${screenshot_name} >/dev/null  $ADB_PATH -s "$device" pull /sdcard/${screenshot_name} ./${screenshot_name} >/dev/null  if [ $? -eq 0 ]; then    echo "[${device}] Screenshot saved to ./${screenshot_name}"  else    echo "[${device}] Failed to capture screenshot."  fi  # Clean up screenshot on device  $ADB_PATH -s "$device" shell rm /sdcard/${screenshot_name} >/dev/null}

5. The Full Automation Script

Now, let’s assemble all the pieces into our automate_e2e.sh script.

#!/bin/bash# --- Configuration ---ADB_PATH="/Users/username/Library/Android/sdk/platform-tools/adb" # IMPORTANT: Adjust this pathAPK_FILE="./app-debug.apk" # Path to your APK fileAPP_PACKAGE="com.example.myapp" # Your app's package nameMAIN_ACTIVITY="com.example.myapp.MainActivity" # Your app's main activity# --- Functions ---install_and_launch_app() {  local device=$1  echo "
[${device}] Starting installation and launch..."  echo "[${device}] Installing ${APK_FILE}..."  # -r for reinstall, in case it's already there  $ADB_PATH -s "$device" install -r "${APK_FILE}" >/dev/null 2>&1  if [ $? -ne 0 ]; then    echo "[${device}] Installation failed. Attempting to uninstall previous version first."    $ADB_PATH -s "$device" uninstall "${APP_PACKAGE}" >/dev/null 2>&1    $ADB_PATH -s "$device" install "${APK_FILE}" >/dev/null 2>&1    if [ $? -ne 0 ]; then      echo "[${device}] CRITICAL: Installation failed after uninstall. Skipping device."      return 1    fi  fi  echo "[${device}] Launching ${APP_PACKAGE}/${MAIN_ACTIVITY}..."  $ADB_PATH -s "$device" shell am start -n "${APP_PACKAGE}/${MAIN_ACTIVITY}" >/dev/null 2>&1  sleep 3 # Give app time to launch  return 0}run_e2e_test() {  local device=$1  echo "[${device}] Running E2E test scenario (Login simulation)..."  # These coordinates are illustrative. You MUST find the correct ones for your app.  # Use Android Studio's Layout Inspector or 'adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp''  # to help identify active elements and their bounds.  # Tap username field (example coordinates)  $ADB_PATH -s "$device" shell input tap 300 500  sleep 1  $ADB_PATH -s "$device" shell input text "testuser"  sleep 1  # Tap password field (example coordinates)  $ADB_PATH -s "$device" shell input tap 300 650  sleep 1  $ADB_PATH -s "$device" shell input text "password123"  sleep 1  # Tap login button (example coordinates)  $ADB_PATH -s "$device" shell input tap 500 800  sleep 5 # Wait for login process/UI update  echo "[${device}] E2E test steps completed."  return 0}capture_screenshot() {  local device=$1  local timestamp=$(date +%Y%m%d_%H%M%S)  local screenshot_name="${device}_${timestamp}_result.png"  echo "[${device}] Capturing screenshot..."  # Save to /sdcard first, then pull  $ADB_PATH -s "$device" shell screencap -p /sdcard/${screenshot_name} >/dev/null  $ADB_PATH -s "$device" pull /sdcard/${screenshot_name} ./screenshots/${screenshot_name} >/dev/null  if [ $? -eq 0 ]; then    echo "[${device}] Screenshot saved to ./screenshots/${screenshot_name}"  else    echo "[${device}] Failed to capture screenshot."  fi  # Clean up screenshot on device  $ADB_PATH -s "$device" shell rm /sdcard/${screenshot_name} >/dev/null}uninstall_app() {  local device=$1  echo "[${device}] Uninstalling ${APP_PACKAGE}..."  $ADB_PATH -s "$device" uninstall "${APP_PACKAGE}" >/dev/null 2>&1  if [ $? -eq 0 ]; then    echo "[${device}] Uninstallation successful."  else    echo "[${device}] Uninstallation failed or app not found."  fi}# --- Main Execution ---mkdir -p screenshots # Ensure screenshots directory existsecho "
--- Discovering Emulators ---"DEVICES=$($ADB_PATH devices | grep emulator | awk '{print $1}')if [ -z "$DEVICES" ]; then  echo "No emulators found. Please ensure emulators are running."  exit 1fiecho "Found emulators:"for DEVICE in $DEVICES; do  echo "- $DEVICE"doneecho "
--- Starting Automated Test Sequence ---"for DEVICE in $DEVICES; do  install_and_launch_app "$DEVICE"  if [ $? -eq 0 ]; then # Only proceed if install/launch was successful    run_e2e_test "$DEVICE"    capture_screenshot "$DEVICE"    uninstall_app "$DEVICE"  fi  echo "[${DEVICE}] Test sequence completed.
"  sleep 2 # Small pause between devices to avoid race conditions/overloaddoneecho "--- All Automated Tests Completed ---"

6. Running Your Automated Tests

1. Save the script as automate_e2e.sh.2. Make it executable: chmod +x automate_e2e.sh3. Place your app-debug.apk in the same directory as the script, or update the APK_FILE path.4. Ensure your emulators are running.5. Execute the script: ./automate_e2e.sh

Watch as your script iterates through each emulator, installs the app, performs the test steps, captures a screenshot in the newly created screenshots directory, and cleans up. This provides a clear, repeatable process for verifying basic application functionality across multiple environments.

Advanced Considerations

  • Parallel Execution

    For a large number of emulators, running tests sequentially can still be slow. You can modify the script to execute tests in parallel using tools like xargs -P or managing background processes with wait commands. Be mindful of host machine resources (RAM, CPU).

  • More Robust Verification

    Instead of just screenshots, consider:

    • Logcat Monitoring: Use adb -s <device_id> logcat -s <YOUR_TAG> to check for specific log messages indicating success or failure.
    • UI Automator (adb shell uiautomator dump): Dump the UI hierarchy to an XML file and parse it to verify element existence or text content.
  • Integration with CI/CD

    This script can be easily integrated into a CI/CD pipeline (e.g., Jenkins, GitLab CI, GitHub Actions) to automate testing on every code commit. Your CI environment would be responsible for launching emulators and running the script.

  • Using Appium/Espresso/UI Automator

    For truly complex E2E scenarios, dedicated testing frameworks like Appium (cross-platform), Espresso (Android native), or UI Automator (Android native) offer more powerful APIs and robust ways to interact with your application’s UI, inspect elements, and handle asynchronous operations. While ADB scripts are excellent for quick, targeted automation, these frameworks provide a more comprehensive solution for intricate test suites.

Conclusion

Automating app installation and E2E testing on multiple Android emulators with ADB scripts is a powerful technique that significantly enhances your development and QA efficiency. By eliminating repetitive manual tasks, you gain faster feedback cycles, more consistent test results, and free up valuable time for more complex testing or feature development. This “one-click deploy” methodology is a crucial step towards building a more robust and agile mobile development workflow.

Android Mobile Specs & Compare Directory

Are you researching mobile hardware properties, processor SoCs, GPU chipsets, or RAM configurations? Access our complete specs catalog to compare up to 5 devices side-by-side!

Compare Devices Specs →
Google AdSense Inline Placement - Content Footer banner