uiautomator2 Detailed explanation: Say goodbye to repetitive mobile phone operations!
As an Android user or test engineer, do you often get stuck in these repetitive tasks:
- When testing new functions, the same registration, login, and form-filling processes need to be manually completed a hundred times;
- Open a bunch of apps every day to check in, sign in, and do tasks, which is so repetitive that you doubt your life;
- I didn’t want to root my phone, but I also wanted to use code to control the Android interface, but I was dissuaded by Google’s native uiautomator.
ByteDance's Python lightweight automation library uiautomator2, which is based on Android's native UIAutomator secondary encapsulation, just solves these problems - No root required, non-intrusive, and extremely fast to get started. It can not only meet the needs of daily UI automation testing, but also serve as a basic tool for personal learning or lightweight automation (please be sure to use it within the legal scope).
This article will take you from Minimalist environment-setup → Core Tool Class Encapsulation → Douyin Information Flow Simulation Practice → Pitfall Avoidance and Anti-Detection Suggestions, and it will take you one hour to get this practical tool.
1. Basic configuration: two-step installation
1.1 Install Python core libraries and auxiliary tools
Domestic users give priority to Tsinghua Source or Douban Source, and the download speed can be more than 10 times faster:
1.2 The device is connected for the first time (only once)
This step requires you to do a little operation on your mobile phone, and then the computer will remember the configuration and connect automatically throughout the process.
-
Enable developer options and debugging permissions Open your phone Settings → About Phone, click "Version Number" 7 times in succession to enter developer mode. Then go to Settings → Developer Options and turn on "USB Debugging" and "USB Installation". If you need to test functions such as maps, you can also turn on "Allow simulated locations".
-
Connect the computer with a data cable Select File Transfer (MTP) mode when connecting. Open a command line to check if adb has recognized the device:
If you see the device serial number, the connection is successful.
- Run the automatic push service for the first time
The first time you connect a device in Python, uiautomator2 will automatically push and install two key components to the phone:
ATX-Agent:Service management program on the device sideuiautomator2-server: Core service responsible for UI interaction
⚠️ Important reminder: After the installation is complete, you must manually allow the floating window permissions of ATX-Agent on your mobile phone. Systems such as MIUI, ColorOS, Funtouch OS often need to be opened separately in "Application Permissions", otherwise elements may not be positioned properly.
2. Core tool class encapsulation: write once and use it everywhere
The API that comes with uiautomator2 is already very friendly, but if each script has to repeatedly write device connection, element search, and gesture sliding, the code will become more and more bloated. We can encapsulate these high-frequency operations into a lightweight tool classU2Controller, greatly improving development efficiency.
Usage Suggestions:
- Single device testing, directly
ctrl = U2Controller()That’s it;- Multiple devices in parallel, pass in the serial number
ctrl = U2Controller("设备序列号"), each instance independently controls a mobile phone.
3. Practical demonstration: Douyin lightweight information flow interaction
Next, an example close to a real scenario will be used to demonstrate the practical capabilities of the above tools - Simulating users to browse TikTok, like randomly, and occasionally comment. Be sure to use it only for automated testing or non-profit learning.
3.1 First use weeditor to figure out the interface layout
Start weeditor:
The browser will automatically open a page. After selecting the corresponding device, you can see the real-time screen of the mobile phone and the complete UI hierarchy tree. We can view the control structure of Douyin through weeditor, but in order to adapt to different screens and version updates, our script will give priority to using a combination of proportional coordinates and attribute positioning.
3.2 Write automated interaction scripts
4. Pitfall avoidance and anti-detection guide
4.1 Common pitfalls and solutions
-
Element positioning priority ✅
resourceId(unique, does not change with UI revision or multi-language) ✅description(Accessible text) 👉text(Multiple languages or UI revision may change) 👉class_name(easy to repeat) ⚠️xpath(Performance is slightly lower, positioning may not be accurate in long lists) Give priority to using the first two, which are stable and efficient. -
Floating window permission Be sure to make sure that the floating window permission of ATX-Agent is turned on, otherwise many positioning methods that rely on accessibility services will fail.
-
Multiple Device Management pass
adb devicesObtain the device serial number and create a separateU2Controllerinstances to avoid interfering with each other. -
Network Fluctuation In actual combat, try to use explicit wait (for example
element.wait()) instead of fixedtime.sleep(), to avoid operation failure due to network lag.
4.2 Basic anti-detection ideas (for learning reference only)
If you use these scripts in non-profit learning or automated testing, you can add the following "anthropomorphic" operations to reduce the probability of being recognized as a machine:
- Randomize everything: viewing duration, sliding distance, click coordinates, operation intervals, all added to random fluctuations.
- Diversified Behaviors: Don’t like continuously, don’t scroll down all the time, occasionally enter the comment area only to read, and occasionally switch to the background and come back.
- Reasonable frequency control: Do not run 24 hours a day, simulate normal work and rest, and control it within a reasonable time period every day.
5. Legal statement
All code and technical ideas provided in this article are only used for legitimate Android application automation testing and non-profit personal learning research. Please do not use it for any malicious brushing, false data, illegal collection or other illegal activities, otherwise all consequences arising therefrom will be borne by the user.

