Inês Inês - 1 year ago 506
Android Question

Build UiAutomator 2.0 from command line

I want to build, install and run the tests of a UiAutomator project through the command line.

In the previous version I was doing:

android create uitest-project -n <project_name> -p <project_path> -t <target_id>
ant build

to build
and then

adb push <jar_path> /data/local/tmp

to install and finally

adb shell uiautomator runtest <jar_name> -c <main_class>

However, right now I'm stuck in the building part.

The result is

[checkenv] Android SDK Tools Revision 24.1.2
[checkenv] Installed at C:\Android
[getbuildtools] Using latest Build Tools: 22.0.0
[echo] Resolving Build Target for AndroidExplorerTester...
[getuitarget] Project Target: Android 5.0.1
[getuitarget] API level: 21
[echo] ----------
[echo] Creating output directories if needed...
[dex] input: <test_path>\bin\classes
[dex] Converting compiled files and external libraries into <test_path>\bin\classes.dex...
[dx] no classfiles specified
C:\Android\tools\ant\uibuild.xml:198: null returned: 1
Total time: 1 second

I don't know if there is a better way to do it now since the new version of UiAutomator.

Note: I don't know if it matters but I was using Eclipse before and now I'm using IntelliJ (Android Studio if you prefer lol)

Answer Source

Well I finally figured it out.

From the command line, in the main folder of the project (where you can find gradlew.bat file) run the following commands


.\gradlew.bat assembleDebug

install on device:

.\gradlew.bat installDebug

(if you want Release version just replace Debug for Release, I didn't try it but the options exist and so I suppose they work)


.\gradlew.bat connectedCheck

If you want to know other options you may have run

.\gradlew.bat tasks

Extra information

To do it programmatically (Java) use Runtime.getRuntime().exec(String command, String[] envp, File dir). For instance,

Runtime.getRuntime().exec(shell_command + " <path_to_test_folder>\gradlew.bat assembleDebug", null, new File(<path_to_test_project>));

Where shell_command depends on the operating system (the command for the command line):

- Windows: cmd /C
- Unix-based: /bin/sh -c
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download