Skip to main content
Raw Android device I/O over ADB, with optional Mobilerun Portal enhancement.

AndroidDriver

AndroidDriver controls Android devices through ADB. Portal is optional and adds Unicode text input, app labels, richer UI state, and overlay-aware screenshots. Import the driver from mobilerun-core-local:

Constructor

Arguments:
  • serial — ADB serial, such as emulator-5554, a USB serial, or an ADB-over-network address.
  • use_tcp — Use Portal HTTP through an ADB port forward when available.
  • portal_mode"auto", "required", or "disabled".

Portal modes

An invalid mode raises ValueError. In Framework configuration, portal_mode="disabled" also skips automatic Portal setup. Examples:

Supported methods

drag() is not supported. Use swipe() instead.

Lifecycle

AndroidDriver.connect

Select and verify an online ADB device. Unless Portal is disabled, the driver connects to Portal and configures its keyboard. In auto mode, connection continues through ADB if Portal is unavailable. In required mode, it raises an error.

AndroidDriver.ensure_connected

Connect once if necessary. It is safe to call repeatedly.

Input actions

AndroidDriver.tap

Tap at absolute Android display-pixel coordinates through ADB.

AndroidDriver.swipe

Swipe between two absolute points over duration_ms milliseconds.

AndroidDriver.input_text

Type into the focused field and return True on success.
  • When the Portal keyboard is available, text input supports Unicode.
  • If Portal input fails in auto mode, the driver switches to ADB text input.
  • ADB-only input supports printable ASCII. It rejects control/non-ASCII characters and the literal substring %s, which Android reserves as a space escape.
  • clear=True clears the focused field first when possible.

AndroidDriver.press_button

Press back, home, or enter (case-insensitive). An unknown name raises ValueError.

AndroidDriver.press_key_code

Send an Android integer key code through ADB.

AndroidDriver.drag

drag() raises NotImplementedError. Use swipe() instead.

App management

AndroidDriver.start_app

Launch an Android package through ADB. If activity is omitted, Mobilerun opens the app’s launcher activity. It raises RuntimeError when the app cannot be launched.

AndroidDriver.install_app

Install a local APK through ADB. reinstall=False and grant_permissions=True are the defaults. A missing path raises FileNotFoundError.
grant_permissions=True grants eligible runtime permissions. Grant special access such as Accessibility, Display over other apps, Notification Access, Install unknown apps, and screen-capture consent on the device.

AndroidDriver.stop_app

Force-stop a package with am force-stop.

AndroidDriver.uninstall_app

Uninstall a package with pm uninstall.

AndroidDriver.list_packages

List package names through ADB. By default, only third-party packages are returned.

AndroidDriver.get_apps

Return {"package": ..., "label": ...} records. Portal supplies display labels when available. Without Portal, package names are used for both fields.

State and observation

AndroidDriver.screenshot

Return raw PNG bytes. Portal can hide its overlay when hide_overlay=True. In auto mode, the driver uses an ADB screenshot if Portal is unavailable.

AndroidDriver.get_ui_tree

Return the accessibility tree, phone state, and device context. ADB-only results may contain less detail than Portal results.

AndroidDriver.get_date

Return the output of the device’s ADB date command.

Portal transport

use_tcp=True uses Portal HTTP over an ADB port forward. In auto mode, Mobilerun uses the ContentProvider or ADB if HTTP is unavailable. Authentication is handled automatically. See the Portal Local API for direct API access.

Example

For element-based actions, see DeviceDriver and actions.