Search code examples
pythonandroidandroid-uiautomatorpydroid

Error on android using uiautomator2 on Pydroid3


I'm trying to automate tasks on Android, but when I run my code on Pydroid3 on Android, it return me a error...

My device is a Asus Zenfone Selfie 4

I'dont understand this error because the error is about a ADB EXE problem.

Android have ADB EXE?

The code:

#MODULES IMPORT
import os
import cv2
import numpy as np
from uiautomator2 import Device
from uiautomator2 import connect

#CLASS AND FUNCTIONS DEFINE
class AndroidAuto:
    def _init_ (self, Serial = "Serial"):
        self.Serial = Serial

    def Android_Screenshot(self, dir = ".screenshots"):
        dir = dir

        if not os.path.exists(dir):
            os.makedirs(dir)

        device = Device(self.Serial)
        device.screenshot(f"{dir}/.screenshot_opencv.png")
        return (f"{dir}/.screenshot_opencv.png")

    def Android_Touch(self, x = 0, y = 0):
        device = Device(self.Serial)
        device.click(x, y)

    def Android_Get_Position(self, target = None):
        Device.toast("Starting ")
        while True:
            template = AndroidAuto().Android_Screenshot()

            w, h = target.shape[:-1]

            res = cv2.matchTemplate(template, target, cv2.TM_CCOEFF_NORMED)
            threshold = 0.8
            loc = np.where(res >= threshold)

            if len(loc[0]) > 0:
                x, y = loc[::-1]
                return x, y
            else:
                x, y = None, None
                Device.toast("Trying again!")
                continue

    def Android_Find_Click(self, target = "", x = 0, y = 0):
        while True:
            match = AndroidAuto().Android_Get_Position(target)

            if match != None:
                AndroidAuto().Android_Touch(x, y = match)
                break
            else:
                continue

AA = AndroidAuto()

print(AA.Android_Screenshot())

Then when I play, it returns me this is the error:

RuntimeError: No adb exe could be found. install adb on your system

Solution

  • That's what site description says:

    python-uiautomator2 is a python-wrapper, which allows:

    • scripting with Python on computer
    • controlling the mobile with computer without usb connection.

    According to this (and my guess) uiautomator2 relies on ADB.exe to connect to the device, searches for it in $PATH system variable, fails to find it and shows you an error.

    So I guess it's not possible to use uiautomator2 in Pydroid3 like you would on PC.