Skip to main content
Raw Android device I/O via ADB + Portal.

AndroidDriver

Raw Android device I/O via ADB and the Mobilerun Portal app. AndroidDriver provides low-level device communication for Android devices through ADB (Android Debug Bridge). It supports both TCP communication and content provider modes via the Mobilerun Portal app. AndroidDriver declares its capabilities in the supported set, and unsupported methods raise NotImplementedError.

AndroidDriver.__init__

Initialize the AndroidDriver instance. Arguments:
  • serial str | None - Device serial number (e.g., “emulator-5554”, “192.168.1.100:5555”). If None, auto-detects the first available device.
  • use_tcp bool - Whether to prefer TCP communication (default: False). TCP is faster but requires port forwarding. Falls back to content provider mode if TCP fails.
  • remote_tcp_port int - TCP port for Portal app communication on device (default: 8080)
Usage:
Supported methods:
Notes:
  • Automatically sets up the Mobilerun Portal keyboard on connect() via setup_keyboard()
  • Creates a PortalClient instance that handles TCP/content provider communication
  • Device serial can be emulator name, USB serial, or TCP/IP address:port
  • Must call connect() or ensure_connected() before using any methods

Lifecycle Methods

AndroidDriver.connect

Establish connection to the device. Discovers the ADB device, creates a PortalClient, and sets up the Portal keyboard. Usage:

AndroidDriver.ensure_connected

Connect if not already connected. Safe to call multiple times.

Input Action Methods

AndroidDriver.tap

Tap at absolute pixel coordinates on the device screen. Arguments:
  • x int - X coordinate
  • y int - Y coordinate
Usage:

AndroidDriver.swipe

Swipe from (x1, y1) to (x2, y2). Arguments:
  • x1 int - Starting X coordinate
  • y1 int - Starting Y coordinate
  • x2 int - Ending X coordinate
  • y2 int - Ending Y coordinate
  • duration_ms float - Duration of swipe in milliseconds (default: 1000)
Usage:
Notes:
  • Duration is converted to seconds internally (dividing by 1000)
  • Includes an async sleep matching the swipe duration for UI settling

AndroidDriver.input_text

Type text into the currently focused field. Arguments:
  • text str - Text to input. Supports Unicode and special characters.
  • clear bool - Whether to clear existing text before inputting (default: False)
Returns:
  • bool - True if input succeeded, False otherwise
Usage:
Notes:
  • Uses the Mobilerun Portal app keyboard for reliable text input via PortalClient
  • Supports Unicode characters and special characters

AndroidDriver.press_button

Press a named system button. Supported buttons: back, home, enter Raises ValueError if the button name is not in supported_buttons. Arguments:
  • button str - Button name (case-insensitive)
Usage:

AndroidDriver.drag

Drag from (x1, y1) to (x2, y2). Arguments:
  • x1 int - Starting X coordinate
  • y1 int - Starting Y coordinate
  • x2 int - Ending X coordinate
  • y2 int - Ending Y coordinate
  • duration float - Duration of drag in seconds (default: 3.0)
Notes:
  • Currently raises NotImplementedError (declared in supported set but not yet implemented)

App Management Methods

AndroidDriver.start_app

Launch an application on the device. If activity is not provided, automatically resolves the main/launcher activity using cmd package resolve-activity. Arguments:
  • package str - Package name (e.g., “com.android.settings”, “com.google.android.apps.messaging”)
  • activity str | None - Optional activity name (e.g., ”.Settings”). If None, auto-detects the main launcher activity.
Returns:
  • str - Result message indicating success or error
Usage:

AndroidDriver.install_app

Install an APK on the device. Arguments:
  • path str - Path to the APK file on the local machine
  • reinstall bool - Whether to reinstall if app already exists (default: False)
  • grant_permissions bool - Whether to grant all permissions automatically (default: True)
Returns:
  • str - Result message indicating success or error
Usage:

AndroidDriver.list_packages

Return installed package names. Arguments:
  • include_system bool - Whether to include system apps (default: False)
Returns:
  • List[str] - List of package names

AndroidDriver.get_apps

Return installed apps as list of dicts with ‘package’ and ‘label’ keys. Arguments:
  • include_system bool - Whether to include system apps (default: True)
Returns:
  • List[Dict[str, str]] - List of dictionaries containing ‘package’ and ‘label’ keys

State and Observation Methods

AndroidDriver.screenshot

Capture the current screen as raw PNG bytes. Arguments:
  • hide_overlay bool - Whether to hide Portal app overlay elements during screenshot (default: True)
Returns:
  • bytes - Raw PNG image data
Usage:

AndroidDriver.get_ui_tree

Return the raw UI / accessibility tree from the device. Returns a dictionary containing both the accessibility tree and phone state data from the Portal app. Returns:
  • Dict[str, Any] - Raw UI tree data from the device

AndroidDriver.get_date

Get the current date and time on the device. Returns:
  • str - Date and time string from device
Usage:

Properties

Instance variables:
  • device - ADB device instance (from async_adbutils)
  • portal - PortalClient instance for device communication (TCP or content provider mode)
  • supported - Set of supported method names for capability checking

Notes

  • Portal app required: The Mobilerun Portal app must be installed and accessibility service enabled on the device
  • TCP vs Content Provider: TCP is faster but requires port forwarding (adb forward tcp:8080 tcp:8080). Content provider is the fallback mode using ADB shell commands.
  • Capability checking: Check "method_name" in driver.supported to determine if a method is available before calling it
  • Async-only: All methods are async and must be awaited
  • No element resolution: AndroidDriver provides raw device I/O only. Element resolution (by index) is handled by UIState via the StateProvider layer.

Example Workflow

Note: For higher-level interactions with element indexing and structured results, use action functions with ActionContext (see MobileAgent) rather than calling the driver directly.