← All posts

Python UIAutomator2 Automation on a Real Cloud Phone (2026)

Nov 27, 2026 · By Xavier Fok

Appium is a lot of machinery when all you really want is a small python script that taps a real phone. A server to run, a driver to install, a capabilities block to get right. This guide shows the lighter path, python uiautomator2, where you pip install one package, adb connect to a real cloud Android phone, and drive it in plain python with no Appium server at all.

For context, I run a cloud Android farm in Singapore. Every phone is real hardware on a rack, each on its own persistent Singapore mobile ip, and I reach all of them over adb.

How uiautomator2 works

The openatx uiautomator2 project has two parts. There is the python client you install on your machine, and a small agent that runs on the phone, an http server backed by Android’s own uiautomator test framework. When you first connect, the client pushes that agent onto the phone and starts it. From then on, every command your python sends becomes an http call to the agent on the device, and the agent drives the real ui. So you are talking to a real phone over the network the whole time, with no separate Appium process to manage.

Appium uiautomator2
Server process Yes, separate None, agent runs on device
Install Appium + driver + client One pip package
Capabilities Full block A serial string
Best for Cross platform suites Quick python scripts

Install and connect

You need Python 3.8 or newer, then pip install uiautomator2. You also need the Android platform tools so the adb binary is on your path, because uiautomator2 uses adb to push and start its agent. Confirm with adb version.

Take the endpoint from your dashboard and run adb connect adb-sg.cloudf.one:5555, then adb devices to confirm it shows as device. If it says offline, wait two seconds and connect again.

In python, write import uiautomator2 as u2, then d = u2.connect("adb-sg.cloudf.one:5555") with your adb endpoint as the serial. The first time you connect, the client installs the atx agent if it is missing, so that first connect takes a few seconds. After that, calls are fast. Sanity check with d.info, which prints the screen size, the Android version, and the real product model, confirming real hardware.

Driving the phone

uiautomator2 has a clean selector syntax. To tap something by visible text: d(text="Login").click. To type into a field by resource id: d(resourceId="com.example:id/username").set_text("value"). set_text is better than a tap and a key sequence, because it sets the field directly and does not get eaten by an autocomplete popup. You can select by description, className, resourceId, or combine them, and you can chain to reach a specific item in a list.

A concrete login script: d.app_start(package) to launch the app, then d(resourceId="...username").wait which blocks until the field appears, so you never guess with a fixed sleep. Set the username, set the password, d(text="Login").click, then assert the home screen with d(resourceId="...navbar").wait(timeout=10). If it returns true the login worked. That wait matters more on a cloud phone than locally, because there is a network hop between your script and the device, so always wait on an element, never on the clock.

Because this is a real phone on a real mobile ip, the app’s anti fraud checks see a normal Singapore handset, not an emulator fingerprint. Many apps refuse to run on emulators, checking build props like ro.kernel.qemu or a generic model name. On a real cloud phone there is nothing to detect.

Watchers solve the random popup problem

On a real device you get system dialogs, permission prompts, rate us nags, and update banners at unpredictable times, and any one of them can break a script by stealing the tap. uiautomator2 lets you register a watcher: when an element with this text appears, click that button, and the client checks for it automatically before each action. Register a watcher for the allow permission button and one for the not now button, start the watchers, and your main script stops caring about interruptions. This is the single biggest reason scripts that pass locally survive on real hardware.

Other tools you will want: d.screenshot saves a png for debugging, so capture one on every exception. d.xpath gives full xpath when simple selectors are not enough. d.swipe handles scrolling, and d(scrollable=True).scroll.to(text="target") scrolls until an element is visible, which is deterministic and does not care about network timing. d.app_stop and d.app_clear reset an app between tests so each run starts clean.

Running across the fleet

List your fleet from the api, get back a set of adb endpoints, and spin one thread per endpoint, each calling u2.connect with its own serial. A ThreadPoolExecutor works well. Ten phones, ten parallel scripts, and because each device object is independent there is no shared server to collide on, which is one real advantage over the single Appium server model. Every phone has a distinct Singapore mobile ip, so region dependent flows are genuinely separate devices.

The failures you will hit

Device offline mid run: if a session drops, the http calls to the agent start failing, so wrap your connect in a retry that runs adb disconnect, then adb connect, then waits two seconds. Agent not responding after a phone reboot: just call u2.connect again and it reinstalls or restarts the agent. Leaked app session: always call d.app_stop in a finally block so the next run starts from a known state.

Wiring it into ci

Since the phone is just a network endpoint, your ci runner needs no usb hardware. pip install uiautomator2, export your token, lock a device from the pool through the api, adb connect, run your script with pytest, then unlock the device in a cleanup step that always runs. Plain cloud runner, real device, every pull request.

Try it on a real phone

If you want phones to script against, that is exactly what I run: real cloud Android phones in Singapore on dedicated hardware, each with a persistent Singapore mobile ip, an adb endpoint per phone, and an api to list and lock the fleet. Start at cloudf.one and use code YT30 for thirty percent off your first month.

Get new guides and videos first — join the Telegram channel.

40% off until 31 December 2026
Real phones in Singapore, each with its own mobile IP.
Introductory price until 31 December 2026. A dedicated phone straight away, no queue.