Tutorial: Set up Android UIAutomator in Eclipse

8 02 2014

Remember my post about MonkeyTalk? UIAutomator seems to be even better automated andrroid app test suite – it does not require any dedicated libs in Android project and can work in any application (even if you do not have access to its source code. All this because it is nested in Android OS, so it can control anything and emulate clicks in any application.

This post will help you to set up basic UIAutomator test.

Note: remember that UIAutomator will work only in Android 4.1+

Instruction is based on this reference.

1. Create new Java Project
Create new Java project that will generate jar with UIAutomator tests. This jar will be later deployed on the Android device (Not: jar, not apk). The jar is separate from application. Thanks to that, it can automate clicks not only in the app view, but whole Android.

2. Add JUnit library
UIAutomator is built upon the JUnit, so you need to add it to build path by

Right click > Properties > Java Build Path > Add Library > JUnit > select JUnit3

3. Add UIAutomator library
On the same window, click ‘Add External JAR’ and navigate to SDK directory > platforms. There choose the android.jar and uiautomator.jar from latest platform available

4. Create sample uiautomator test case
Add new file to the src directory in your project:
New > JUnit Test Case

Make it extend the UiAutomatorTestCase, give it name you like. Mine test will try to automatically click the device back button:

public void testBackPress() {

5. Create test project build configuration
Android tools can create ant build.xml file containing the build configuration for test project. It is issued by this command:

/tools/android create uitest-project -n  -t 1 -p 
-n is the project name param
-p is the path to your project param
-t is the target installed on your system

To set the -t param, according to help in this post, execute:

android list targets

and choose the highest id installed, in my case it is:

id: 34 or "Google Inc.:Google APIs:19"
     Name: Google APIs
     Type: Add-On
     Vendor: Google Inc.
     Revision: 2
     Description: Android + Google APIs
     Based on Android 4.4.2 (API level 19)

As a result you should be informed, that the build.xml file was created

6. Build project with ant

Navigate to project directory and build project with ant from command line:

ant build

To avoid the

android-sdk\tools\ant\uibuild.xml:183: Error running javac.exe compiler

Ensure that your default JAVA_HOME points to JDK installation, not the JRE, and check if your compiler is in the PATH. Try to type “javac” in the command prompt. If javac is not found, then you should put your bin directory in the PATH.
For example, in Windows:

SET PATH=c:/jdk1.5.0_07/bin;%PATH%

Then restart eclipse if needed. What is more – I needed to reboot my computer to make it work and find javac!

7. Copy test project to your device

You can do it with file explorer or use adb push command like this:

adb push  /sdcard/

replacing the with path to .jar file generated by and build

8. Run test
execute adb shell command to run the particular class in test:

adb shell uiautomator runtest /sdcard/ -c

which for example may look like:

adb shell uiautomator runtest /sdcard/MyUIAutomatorTest.jar -c com.looksok.uiautomator.TestSampleBackButton

You can even run particular method in chosen class by calling the method name like that:

-c com.looksok.uiautomator.TestSampleBackButton#testMethod

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!



15 responses

13 03 2014

Cool tutorila, but u forgot to say that need to download ant ))

16 01 2015

If you have ADT with Eclipse then you can find ant in the plugins directory of Eclipse. You can also run ant from within Eclipse (http://blog.blundell-apps.com/running-an-ant-script-with-eclipse/)

17 01 2015
Jacek Milewski

It is obvious. Thinking that way I could have also written that you need to download eclipse

31 03 2014

Can u just tell me how can i run the different Methods of the class from .xml file…..and how to integrate JUNIT / TestNG with UIAutomator so that i can get the better Result as the output.?

31 03 2014
Jacek Milewski

I am afraid I don’t know what you mean saying ‘methods defined in xml’

And regarding jUnit – you can use it with uiautomator, since there is support of it. You can for instance find UIObject add you usually do, get its text content and call jUnit assert method on it. Just simple as that

17 05 2014
Tutorial: Set up Android UIAutomator in Eclipse | Looks OK! - appgong

[…] Tutorial: Set up Android UIAutomator in Eclipse | Looks OK! […]

23 07 2014

Hi Jacek,

Is any library file available to create UIAutomation test cases for ‘Accessibility’ testing?
Could you tell any tips for creating ‘Accessiblity (TalkBack)’ test cases.

Thank in advance.


23 07 2014
Jacek Milewski

I am not aware of any tools to du this kind of testing

8 08 2014

Hi Jacek,

Is there a way to execute the tests written without following the lengthy procedure of -Creating a jar, building the xml file, pushing the jar file in the device.

Also, can you tell how to test the application on the emulator rather than a device

9 08 2014
Jacek Milewski

no, I don’t know any other way than generating jar. UiAutomator is quite new and there is no IDE support yet. But I belive it will be soon

My solution is to create batch/shell script that will execute those commands for you:
adb push /sdcard/
adb shell uiautomator runtest /sdcard/MyUIAutomatorTest.jar -c com.looksok.uiautomator.TestSampleBackButton

You can also push this jar file to an emulator – just like on the device.

24 08 2014
Syed Mehtab

Yes, I have recently published an open source tool UIautomator-bot. This exactly does what u want and more. You can download it from http://sourceforge.net/projects/uiautomator/ . UIautomator-bot is currently available for Linux.

24 08 2014
Jacek Milewski

Thanks for your work and for sharing it!

16 08 2014
Mobile test automation, an overview | Bottle of Code : Luke Hines

[…] Automator links: Google documentation A good setup article “UI Automator in eclipse” My Thoughts… Right now there are some limitations to just how far automated testing can go […]

24 08 2014
Syed Mehtab

Hi Jacek ,

Thanks for sharing this article. I stopped by to brag : ) about my new open source project which makes the task of creating and running test cases simple and easy. You can download it from here http://sourceforge.net/projects/uiautomator/
and for further info refer http://uiautomator-bot.blogspot.in/

Please try this new tool. Waiting for your valuable inputs.
Thanks in advance.

24 08 2015

Why JUnit3 and not JUnit4?

Give Your feedback:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: