Caused by: java.lang.IllegalStateException:UiAutomationService 。。。。registered!

Caused by: java.lang.IllegalStateException:UiAutomationService 。。。。registered!

问题:

运行自动化代码的时候在appium中突然爆出这个错误:

Causedby:java.lang.IllegalStateException:UiAutomationService android.xxxxxxxxxxxxxxxxxxxxxalready registered!

解决办法:

这个错误 UiAutomationService already registered 表明 UiAutomation 服务已经被注册,可能是之前的测试会话没有正确清理导致的

运行下面命令:

# 在运行Python代码前执行这些命令
adb shell am force-stop com.android.settings
adb shell settings put secure enabled_accessibility_services null
adb shell pm clear com.android.settings
adb kill-server && adb start-server

再次运行代码发现成功了。

上面的方法只是临时的,最好创建一个python脚本解决这种问题:

python名字为:clear.py

代码如下:

import subprocess
import time


def fix_uiautomation_conflict():
    """
    修复 UiAutomationService 冲突问题
    """
    print("开始修复 UiAutomationService 冲突...")

    # 1. 停止所有可能冲突的服务
    print("1. 停止相关服务...")
    services_to_stop = [
        "io.appium.uiautomator2.server",
        "io.appium.uiautomator2.server.test",
        "com.github.uiautomator",
        "com.github.uiautomator.test",
        "com.android.documentsui",  # 停止xx应用,这里要写对应启动模拟器时打开应用的包名
    ]

    for service in services_to_stop:
        subprocess.run(f"adb shell am force-stop {service}",
                       shell=True, capture_output=True)

    # 2. 重置无障碍服务设置
    print("2. 重置无障碍服务...")
    reset_commands = [
        "adb shell settings delete secure enabled_accessibility_services",
        "adb shell settings delete secure accessibility_enabled",
        "adb shell settings put secure enabled_accessibility_services null",
        "adb shell settings put secure accessibility_enabled 0",
    ]

    for cmd in reset_commands:
        subprocess.run(cmd, shell=True, capture_output=True)

    # 3. 杀死相关进程
    print("3. 终止相关进程...")
    processes_to_kill = [
        "uiautomator",
        "accessibility",
        "settings",
        "instrumentation",
    ]

    for process in processes_to_kill:
        subprocess.run(f"adb shell pkill -f {process}",
                       shell=True, capture_output=True)

    # 4. 清理缓存和数据
    print("4. 清理应用数据...")
    apps_to_clear = [
        "com.android.documentsui",  # 这里要写对应启动模拟器时打开应用的包名
        "io.appium.uiautomator2.server",
        "io.appium.uiautomator2.server.test",
    ]

    for app in apps_to_clear:
        subprocess.run(f"adb shell pm clear {app}",
                       shell=True, capture_output=True)

    # 5. 重启ADB和模拟器
    print("5. 重启ADB服务...")
    subprocess.run("adb kill-server", shell=True, capture_output=True)
    time.sleep(2)
    subprocess.run("adb start-server", shell=True, capture_output=True)
    time.sleep(3)

    # 6. 验证修复
    print("6. 验证修复结果...")
    result = subprocess.run(
        "adb shell 'ps | grep uiautomator'",
        shell=True, capture_output=True, text=True
    )

    if "uiautomator" in result.stdout:
        print("⚠️  仍有uiautomator进程在运行,可能需要重启模拟器")
    else:
        print("✓  uiautomator进程已清理")

    print("修复完成!等待模拟器稳定...")
    time.sleep(5)


在需要运行代码的地方前先调用这个类中的fix_uiautomation_conflict()方法先清理一下就可以了。

posted @ 2026-01-04 22:40  蜀道,难  阅读(30)  评论(0)    收藏  举报