dtmilano / AndroidViewClient

Android ViewServer and ADB client
Apache License 2.0
1.63k stars 347 forks source link

ViewClient crash due to __parseTreeFromUiAutomatorDump ? #320

Closed sooraj-sizon-pj closed 8 months ago

sooraj-sizon-pj commented 1 year ago

Hi @dtmilano , I this error running AndroidViewClient on AWS ec2 instance image

Device is custom android emulator x86_64 running on AWS EC2 , I have same Emulator setup on my local env where AndroidViewClient works perfectly Where as on EC2 with same Automation script it goes through a few screens of android setupwizard and able to click on buttons and views and fails with this crash on like 4th or 5th step .

The emulator is running custom system-images build from AOSP A11_r1 tag has custom setup wizard another system app and a system service running very early at boot.

I ran uiautomatorviewer dump which returned

Xlib extension RANDR missing on display :1

Do you know what might cause this crash ?

dtmilano commented 1 year ago

Are you using nested virtualization or bare metal EC2 instance? Is the setup wizard using animations? There's a known limitation of uiautomator not being able to obtain screen dump when the device is not idle (i.e. animation). To verifty that, if androidviewclient fails, you can try uiautomator dump immediately and verify the same error is obtained.

To overcome that limitation and others and adding extra functionality is that CulebraTester2-public backend exists. Perhaps you can give it a try.

sooraj-sizon-pj commented 1 year ago

Are you using nested virtualization or bare metal EC2 instance? Is the setup wizard using animations?

I'm using EC2 bare metal instance (g4dn.metal)

There's a known limitation of uiautomator not being able to obtain screen dump when the device is not idle (i.e. animation). To verifty that, if androidviewclient fails, you can try uiautomator dump immediately and verify the same error is obtained.

To my surprise the same emulator build with setupwizard and everything works perfectly fine in my local machine running same ubuntu 22.04

To overcome that limitation and others and adding extra functionality is that CulebraTester2-public backend exists. Perhaps you can give it a try.

Thanks I will take a look ..

dtmilano commented 1 year ago

To test after the error you should run

adb shell uiautomator dump

You missed shell in your screenshot.

dtmilano commented 1 year ago

https://source.android.com/docs/setup/create/cuttlefish perhaps this helps.

sooraj-sizon-pj commented 4 months ago

This was a bug in uiautomator for now simple workaround is add retries when this happens


MAX_RETRIES = 3
RETRY_DELAY = 1  # seconds

def retry_on_value_error(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        retries = 0
        while retries < MAX_RETRIES:
            try:
                return func(*args, **kwargs)
            except ValueError as e:
                print(f"Error: {e}, retrying... ({retries + 1}/{MAX_RETRIES})")
                time.sleep(RETRY_DELAY)
                retries += 1
        raise ValueError(f"Failed after {MAX_RETRIES} retries")
    return wrapper

viewClient.dump = retry_on_value_error(viewClient.dump)

Run this at the start of the script and every time uiautomnator sends empty xml it would retry

dtmilano commented 4 months ago

Interesting, I thought it wasn't a recoverable error. Great that retrying works.