AdbTools
AdbTools.__init__
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)app_opener_llm
LLM | None - LLM instance for app opening workflow (optional). Used by helper tools to open apps by natural language description.text_manipulator_llm
LLM | None - LLM instance for text manipulation (optional). Used by helper tools for text editing operations.credential_manager
CredentialManager | None - CredentialManager instance for secret handling (optional). Enables secure credential access in automation workflows.
- Automatically sets up the Droidrun Portal keyboard on initialization 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
UI Interaction Methods
AdbTools.tap_by_index
index
int - Index of the element to tap (from accessibility tree)
str
- Result message describing the tapped element
- Call
get_state()
first to populate the clickable elements cache - Returns descriptive error message (not raises exception) if element index is invalid
- Error message includes up to 20 available indices to help with debugging
- Automatically searches nested children for the target index
- Returns detailed information about tapped element including text, class, type, and child content
AdbTools.tap_by_coordinates
x
int - X coordinatey
int - Y coordinate
bool
- True if tap succeeded, False otherwise
AdbTools.tap
tap_by_index()
. This function uses the cached clickable elements from the last get_state()
call to find the element with the given index and tap on its center coordinates.
Arguments:
index
int - Index of the element to tap
str
- Result message
AdbTools.swipe
start_x
int - Starting X coordinatestart_y
int - Starting Y coordinateend_x
int - Ending X coordinateend_y
int - Ending Y coordinateduration_ms
float - Duration of swipe in milliseconds (default: 300)
bool
- True if swipe succeeded, False otherwise
- Emits SwipeActionEvent when context is set for trajectory tracking
- Uses
@Tools.ui_action
decorator for automatic screenshot capture - Duration is converted to seconds internally (dividing by 1000)
AdbTools.drag
start_x
int - Starting X coordinatestart_y
int - Starting Y coordinateend_x
int - Ending X coordinateend_y
int - Ending Y coordinateduration
float - Duration of drag in seconds (default: 3)
bool
- True if drag succeeded, False otherwise
- Emits DragActionEvent when context is set for trajectory tracking
- Uses
@Tools.ui_action
decorator for automatic screenshot capture - Includes sleep after drag operation to allow UI to settle
AdbTools.input_text
text
str - Text to input. Can contain spaces, newlines, and special characters including non-ASCII.index
int - Index of the element to input text into. If -1, uses the currently focused element (default: -1).clear
bool - Whether to clear existing text before inputting (default: False)
str
- Result message
- Always ensure a text field is focused before inputting text (use
tap_by_index()
or setindex
parameter) - Uses the Droidrun Portal app keyboard for reliable text input via PortalClient
- Supports Unicode characters and special characters including non-ASCII
- If
index != -1
, automatically taps the element first before inputting text - Call
get_state()
first to populate element cache if usingindex
parameter - Emits InputTextActionEvent when context is set for trajectory tracking
- Uses
@Tools.ui_action
decorator for automatic screenshot capture - Text longer than 50 characters is truncated in result message (but fully input to device)
AdbTools.back
str
- Result message
- Uses Android keycode 4 (KEYCODE_BACK)
- Emits KeyPressActionEvent when context is set for trajectory tracking
- Uses
@Tools.ui_action
decorator for automatic screenshot capture
AdbTools.press_key
3
: HOME4
: BACK66
: ENTER67
: DELETE
keycode
int - Android keycode to press
str
- Result message with key name
- Common keycodes (3, 4, 66, 67) are mapped to readable names (HOME, BACK, ENTER, DELETE)
- Emits KeyPressActionEvent when context is set for trajectory tracking
- Uses
@Tools.ui_action
decorator for automatic screenshot capture
App Management Methods
AdbTools.start_app
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.
str
- Result message indicating success or error
- Uses
cmd package resolve-activity --brief
to auto-detect main activity when not specified - Emits StartAppEvent when context is set for trajectory tracking
AdbTools.install_app
apk_path
str - Path to the APK file on the local machinereinstall
bool - Whether to reinstall if app already exists (default: False)grant_permissions
bool - Whether to grant all permissions automatically (default: True)
str
- Result message indicating success or error
- APK file must exist on the local machine (not on device)
- Returns error message if APK file is not found
- With
grant_permissions=True
, automatically grants runtime permissions via-g
flag
AdbTools.list_packages
include_system_apps
bool - Whether to include system apps (default: False)
List[str]
- List of package names
AdbTools.get_apps
include_system
bool - Whether to include system apps (default: True)
List[Dict[str, str]]
- List of dictionaries containing ‘package’ and ‘label’ keys
State and Screenshot Methods
AdbTools.get_state
clickable_elements_cache
used by tap_by_index()
.
Returns:
Dict[str, Any]
- Dictionary containing both ‘a11y_tree’ and ‘phone_state’ data:a11y_tree
: List of UI elements with indices, text, class names, bounds, etc.phone_state
: Current activity name, keyboard visibility, etc.
- Always call this method before using
tap_by_index()
to populate the element cache - The
type
attribute is filtered out from elements in the returned tree - Uses PortalClient which automatically selects TCP or content provider mode
AdbTools.take_screenshot
screenshots
list with timestamp information.
Arguments:
hide_overlay
bool - Whether to hide Portal app overlay elements during screenshot (default: True)
Tuple[str, bytes]
- Tuple of (format, image_bytes) where format is “PNG” and image_bytes is the PNG image data
- Screenshots are automatically stored in
tools.screenshots
list with timestamp - Each screenshot entry contains:
{"timestamp": float, "image_data": bytes, "format": "PNG"}
- Uses PortalClient for screenshot capture
AdbTools.get_date
str
- Date and time string from device
Device Communication Methods
AdbTools.ping
Dict[str, Any]
- Dictionary with ping result
Memory and Completion Methods
AdbTools.remember
information
str - The information to remember
str
- Confirmation message
- Memory is limited to 10 most recent items
- Memory persists for the duration of the agent’s execution
- Memory is accessible via
get_memory()
or automatically included in agent context
AdbTools.get_memory
List[str]
- List of stored memory items
AdbTools.complete
success
bool - Indicates if the task was successfulreason
str - Reason for failure/success (optional for success, required if success=False)
- This sets internal flags (
finished
,success
,reason
) used by agents to determine completion - If
success=False
,reason
is required (raises ValueError if not provided) - If
success=True
and no reason provided, defaults to “Task completed successfully.” - Uses
@Tools.ui_action
decorator for automatic screenshot capture - This does not terminate execution, it only sets completion flags
Properties
Instance variables:device
- ADB device instance (from adbutils)portal
- PortalClient instance for device communication (TCP or content provider mode)clickable_elements_cache
- List of cached UI elements from lastget_state()
callmemory
- List of remembered information items (max 10)screenshots
- List of captured screenshots with timestamps (format:[{"timestamp": float, "image_data": bytes, "format": "PNG"}]
)save_trajectories
- Trajectory saving level: “none”, “step”, or “action”finished
- Boolean indicating if task is complete (set bycomplete()
)success
- Boolean indicating if task succeeded (set bycomplete()
)reason
- String describing success/failure reason (set bycomplete()
)app_opener_llm
- LLM instance for app opening workflow (optional)text_manipulator_llm
- LLM instance for text manipulation (optional)credential_manager
- CredentialManager instance for secret handling (optional)
Notes
- Portal app required: The Droidrun 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. - Element caching: Always call
get_state()
before usingtap_by_index()
ortap()
to populate the element cache - Trajectory recording: When
save_trajectories="action"
, screenshots and UI states are automatically captured for each UI action via the@Tools.ui_action
decorator - Unicode support:
input_text()
supports Unicode characters and special characters via the Portal app’s custom keyboard - Event streaming: When a context is set via
_set_context()
, action events (TapActionEvent, SwipeActionEvent, etc.) are emitted for trajectory tracking - Decorator behavior: Methods decorated with
@Tools.ui_action
automatically capture screenshots and emit events when trajectory recording is enabled