Search code examples
javaandroidandroid-studiokotlinandroid-uiautomator

Execute instrumented test from app in Android


First of all sorry if my question is ignorant, I'm not an Android developer. I have to build a bot application (automatically executes actions in another app) and found out that this is possible with UIAutomator. (I know I'm misusing the framework, but the way I see it what I'm trying to achieve is not possible in any other way.)

I started to build the app and it works, however, I would like to be able to run it without the device being connected to Android Studio. Is it possible to execute the tests from the app by pressing a button or something?


Solution

  • UIautomation relies on android.app.Instumentation which can only be started through adb or from within a System App. For security reasons the system doesn't grant permissions to "execute actions on other apps" unless accesibility permissions has been granted explicitly from the user.

    For this reasons I'm afraid it will NOT be possible to use UIAutomator without "being connected to android studio" (this connection is achieved precisely by means of adb, which Android studio uses to connect to the cellphone). You can also use adb directly if needed BUT connection to a computer and adb executable are essential.

    More information about UIAutomator and AccessibilityService diferencies here.

    Update: Even if it's not possible to use UIAutomator without an adb connection, you can still write your own accessibility service using this detailed guide. Please note it will not allow you to "execute instrumented tests" but to "perform actions in another app".