Android Emulator Development, Anbox, & Waydroid

Automating ADB USB Passthrough for CI/CD in Virtual Android Labs

Google AdSense Native Placement - Horizontal Top-Post banner

Introduction: Bridging the Gap in Virtual Android CI/CD

In the realm of modern Android development, continuous integration and continuous deployment (CI/CD) pipelines are indispensable for rapid iteration and quality assurance. However, integrating physical Android devices into virtualized CI/CD environments – such as those leveraging Anbox, Waydroid, or traditional virtual machines – often presents a significant challenge: reliable Android Debug Bridge (ADB) connectivity. While network ADB exists, it frequently falls short for tasks requiring low-level interaction, high reliability, or specific USB functionalities like device flashing. This article delves into a robust solution: automating ADB USB passthrough using the Linux usbip framework, enabling seamless physical device interaction within your virtualized Android CI/CD labs.

The Challenge of ADB in Virtualized Android Environments

Why Traditional ADB Fails or is Inconsistent

Traditional ADB over TCP/IP, while convenient for quick debugging, introduces layers of network abstraction that can lead to increased latency, dropped connections, and general unreliability. For critical CI/CD tasks like flashing custom firmware, executing extensive test suites that involve multiple device reboots, or performing deep hardware-level debugging, a direct and stable USB connection is paramount. Network instability or misconfigurations can derail entire test runs, making robust USB connectivity a necessity.

Anbox, Waydroid, and VMs: A Different Kind of Host

Anbox and Waydroid, which provide containerized Android environments on Linux, abstract the underlying hardware considerably. While they offer performance advantages, direct USB device access to the Android instance can be complex. Similarly, traditional virtual machines (VMs) often require specific hypervisor configurations for USB passthrough, which may not always be scriptable or compatible with a wide range of devices in an automated CI/CD context. The usbip solution bypasses many of these issues by virtualizing the USB connection itself over the network, allowing the virtualized Android environment to perceive a directly connected USB device.

Understanding USB/IP: The Passthrough Mechanism

USB/IP is a Linux kernel module and set of user-space utilities that allow a USB device connected to one machine (the ‘server’ or ‘host’) to be used on another machine (the ‘client’ or ‘guest’) over an IP network, as if it were locally attached. It effectively virtualizes the USB bus. The server exports the physical USB device, making it available over the network, while the client imports and attaches this remote device. This mechanism is ideal for our purpose, as it provides a stable, low-level USB connection that ADB can leverage reliably.

Prerequisites for using USB/IP include:

  • Both host and client machines must be running a Linux kernel with usbip modules compiled in or available (most modern distributions include them).
  • The usbip user-space utilities must be installed on both machines.
  • Network connectivity between the host (where the physical device is connected) and the client (the virtualized Android environment) is essential.

Step-by-Step: Setting Up the USB/IP Server (Host Machine)

The host machine is where your physical Android device is connected via USB.

1. Install USB/IP Utilities and Load Modules

First, ensure you have the necessary tools and kernel modules on your host Linux system:

sudo apt update && sudo apt install linux-tools-generic usbipcd /lib/modules/$(uname -r)/kernel/drivers/usb/usbip/sudo modprobe usbip_core usbip_hostsudo modprobe vhci-hcd # While primarily for client, good to have

2. Identify Your Android Device

Connect your Android device to the host machine. Then, list the available USB devices to find its BusID. Look for your device’s vendor/product ID (e.g., Google devices often start with 18d1:).

usbip list -l

Example output:

BusID 1-1: Qualcomm, Inc. Snapdragon (05c6:901d)BusID 1-2: Google Inc. Nexus/Pixel (MTP) (18d1:4ee7)

Note the BusID (e.g., 1-2) for your Android device.

3. Bind the USB Device

Before exporting, you must bind the device to the usbip_host driver, detaching it from its current driver. Replace <BUSID> with your device’s ID (e.g., 1-2).

sudo usbip bind --busid=<BUSID>

Verify the binding by running usbip list -l again. The device should now show (vudc_driver) or similar indicating it’s bound to the USB/IP driver.

4. Export the Device for Client Access

Now, start the usbipd daemon to export the device over the network. It will listen for incoming connections on TCP port 3240.

sudo usbipd -D

If you have a firewall, ensure port 3240 is open for incoming TCP connections from your client machine’s IP address or subnet:

sudo ufw allow from <CLIENT_IP_ADDRESS> to any port 3240/tcp

Step-by-Step: Connecting from the USB/IP Client (Virtual Android Environment)

The client machine is your virtualized Android environment (VM, Anbox container, Waydroid container, etc.). It needs network access to the host.

1. Install USB/IP Utilities and Load Modules

Similar to the host, install the necessary tools and load the client-side kernel module:

sudo apt update && sudo apt install linux-tools-generic usbipcd /lib/modules/$(uname -r)/kernel/drivers/usb/usbip/sudo modprobe vhci-hcdsudo modprobe usbip_core

2. List Available Remote USB Devices

From the client, you can now list the USB devices exported by your host machine. Replace <HOST_IP_ADDRESS> with the actual IP address of your host.

usbip list -r <HOST_IP_ADDRESS>

You should see your Android device listed, along with its bus ID on the host.

3. Attach the Remote USB Device

Attach the remote device to your client machine. Use the BusID *as reported by the usbip list -r command* from the previous step. This will likely be the same BusID as on the host, e.g., 1-2.

sudo usbip attach -r <HOST_IP_ADDRESS> --busid=<BUSID>

After attaching, verify that the device appears as a locally connected USB device on your client:

lsusb

4. Verify ADB Connection

Finally, confirm that ADB can now detect your Android device from within the virtualized environment:

adb kill-serveradb start-serveradb devices

You should see your device listed as

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