From a5854269249d3b3d70f064e9957abe910be3a6a2 Mon Sep 17 00:00:00 2001 From: Marc Bonnici Date: Tue, 21 Jun 2022 16:47:26 +0100 Subject: [PATCH] android: Don't error if ADB is already running as root With recent versions of adb, adb root can fail if the daemon is already running as root. Check the raised error message for this case and avoid raising an error in this scenario. --- devlib/utils/android.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/devlib/utils/android.py b/devlib/utils/android.py index 32afa96..b46a825 100755 --- a/devlib/utils/android.py +++ b/devlib/utils/android.py @@ -385,9 +385,18 @@ class AdbConnection(ConnectionBase): def adb_root(self, enable=True): cmd = 'root' if enable else 'unroot' - output = adb_command(self.device, cmd, timeout=30, adb_server=self.adb_server) - if 'cannot run as root in production builds' in output: - raise TargetStableError(output) + try: + output = adb_command(self.device, cmd, timeout=30, adb_server=self.adb_server) + except subprocess.CalledProcessError as e: + # Ignore if we're already root + if 'adbd is already running as root' in e.output: + pass + else: + raise + else: + # Check separately as this does not cause a error exit code. + if 'cannot run as root in production builds' in output: + raise TargetStableError(output) AdbConnection._connected_as_root[self.device] = enable def wait_for_device(self, timeout=30):