# ADB shell command

An action block that executes a shell command through ADB, on the local or a remote Android device.

The fiber will pause until the command has completed.

When only executing commands on the local device then using the Shell command privileged block is preferable since it only has to connect to the ADB daemon once, making it faster and more reliable.

The command should be formatted as it would when executed at a shell prompt (without `adb shell` prefix), that includes proper escaping. Use the cliEncode function when including a filename or other text as an argument.

The standard output and error will contain the full text as generated by the command during its execution. The exit code is usually set to `0` (zero) for a successful execution of the command.

## Setup

A device has to be configured to accept incoming ADB connections. On Android 11+ devices, enable the Wireless debugging option in system Developer settings. On Android 10 or lower devices, it has to be enabled using a USB connected PC, please read ADB.

Every connection requires authentication in the form of a 2048-bit RSA key-pair. If the TLS option is enable, the public key should be encoded in a self-signed X.509 certificate which is used along with the private key for TLS encryption. If disabled, the private key is used to sign a “challenge-response” sent by the device, but the connection is unencrypted. Click the Install generated key button to create such a key-pair, or create it elsewhere, e.g. with a tool like `openssl`, then install it in system Security settings. Once an alias of an installed key has been chosen, the system will grant Automate perpetual usage, but the secret key material remains inaccessible, e.g. for export or backup.

A key-pair must be manually authorized by the user, click the Pair device button to perform the process. When using Wireless debugging the pairing process first has to be started by clicking the Pair device with pairing code button in system Developer settings, Wireless debugging.

The port for an Wireless debugging connection can change at device reboot, use the Network service discover block with the Android Debug Bridge (ADB) over TLS service type to perform a lookup prior to this block.

A device may refuse additional connections while already connected through USB cable, disconnect it. If an EACCES `SocketException` failure occurs, try disabling the system “Data Saver” feature, for just Automate or globally.

Privacy warning! If the TLS option is disabled the connection may be unencrypted, don’t connect to a remote device over a public network.

## Input arguments

- Command — command to execute, including any arguments.
- Keychain alias — alias of an 2048-bit RSA private key and certificate stored in the Android system keychain.
- Host or IP address — hostname or IP address of Android device, default is localhost.
- Port — remote port, default is 5555.
- Security — whether to establish a secure TLS connection to an Android 11+ device, default is no, i.e. unencrypted.

## Output variables

- Standard output text — variable to assign the STDOUT.
- Standard error text — variable to assign the STDERR.
- Exit code — variable to assign the exit code.