How To Execute Shell Commands On The Remote Device

It is possible to execute any command on the remote Android device or an emulator under test and get the output from it. This action might be potentially insecure and is disabled on the server side by default. One must provide the --relaxed-security command line argument while starting the server in order to enable remote shell commands execution (and other insecure features, which are disabled by default). An exception will be thrown if the relaxed security has not been enabled on the server side and one tries to invoke mobile: shell endpoint on the client side.

mobile: shell

Executes the given shell command on the device under test and returns its stdout or both stdout and stderr if includeStderr is set to true. An exception will be thrown if the command's return code is not zero. This command acts in the same manner as it would be executed via adb shell on the host computer.

Supported arguments

Usage examples

// Java
Map<String, Object> args = new HashMap<>();
args.put("command", "echo");
args.put("args", Lists.newArrayList("arg1", "arg2"));
String output = driver.executeScript("mobile: shell", args);
assert output.equals("arg1 arg2");
# Python
result = driver.execute_script('mobile: shell', {
    'command': 'echo',
    'args': ['arg1', 'arg2'],
    'includeStderr': True,
    'timeout': 5000
})
assert result['stdout'] == 'arg1 arg2'