Automate Alipay Ant Forest Energy Harvesting with uiautomator2 and Python

This guide shows how to use Python's uiautomator2 library to automatically open Alipay, navigate to the Ant Forest feature, locate and collect energy from friends, and safely stop when no more energy is available, complete with installation steps and full source code.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Automate Alipay Ant Forest Energy Harvesting with uiautomator2 and Python

Although the author has many Alipay friends and can manually steal energy, they wanted to automate the process due to laziness.

After learning about Appium and uiautomator2, they found that uiautomator2 simplifies the workflow, especially after Ant Forest's redesign, enabling full automation.

Manual Energy Collection Steps

Open Alipay.

Enter Ant Forest.

Collect your own energy.

Jump to the next friend with energy.

Collect that friend's energy.

Repeat steps 4 and 5 until no more energy can be stolen.

Using uiautomator2, all these steps can be automated. UiAutomator is a Google-provided Java library for Android UI testing; uiautomator2 is its Python wrapper, allowing detection of UI elements, coordinates, and simulated clicks.

Installation and Setup

Install uiautomator2 with a single pip command: pip install --upgrade --pre uiautomator2 Before connecting your phone, enable Developer Mode and USB debugging. The first run will install the ATX app on the device, enabling wireless control.

Implementation Details

Launching Alipay and Opening Ant Forest

Start Alipay by its package name: d.app_start("com.eg.android.AlipayGphone") Then click the "Ant Forest" text element: d(text="蚂蚁森林").click() It is recommended to place the Ant Forest shortcut on Alipay's home page for easier access.

Collecting Energy

The script scans predefined screen regions to locate energy bubbles and performs long clicks, then clicks the "Find Energy" button to move to the next friend.

Stopping Condition

The script checks for the "Return to My Forest" button, which appears when no more energy can be stolen, and then exits the loop.

Full Source Code

import uiautomator2 as u2
import time
import random
# d = u2.connect()  # wired connection

d = u2.connect("192.168.0.108")  # wireless connection, same LAN, after initial wired setup

print("打开支付宝")
d.app_start("com.eg.android.AlipayGphone")
time.sleep(2)  # wait for Alipay to launch

print("打开蚂蚁森林,等待5s……")
d(text="蚂蚁森林").click()
time.sleep(5)  # wait for forest to load

def collectEnergy(cnt):
    print("开始第%d次偷能量!" % cnt)
    # scan and click possible energy locations
    for x in range(150, 1000, 150):
        for y in range(600, 900, 150):
            d.long_click(x + random.randint(10, 20), y + random.randint(10, 20), 0.1)
            time.sleep(0.01)
            if cnt != 1:
                d.click(536, 1816)

cnt = 1
while True:
    collectEnergy(cnt)
    a = d.xpath("//*[@resource-id='J_tree_dialog_wrap']").get().bounds
    d.click(1000, a[3] - 80)  # click find energy button
    # stop if "Return to My Forest" appears
    if d.xpath('//*[@text="返回我的森林"]').click_exists(timeout=2.0):
        break
    cnt += 1
print("###结束###")
# d.app_stop("com.eg.android.AlipayGphone")

Conclusion

The entire script is under 30 lines, but use it discreetly—don’t let friends know you’re cheating, or you might get blocked.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AlipayAndroid automationuiautomator2ant forestmobile scripting
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.