Build UiAutomator 2.0 from command line

2019-03-22 03:25发布

问题:

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

-check-env:
  [checkenv] Android SDK Tools Revision 24.1.2                                    
  [checkenv] Installed at C:\Android                                                                                                                             
-build-setup:                                                                   
[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...                                                                                                            
-pre-compile:                                                                                                                                                   
compile:                                                                                                                                                        
-post-compile:                                                                                                                                                  
-dex:                                                                                 
    [dex] input: <test_path>\bin\classes                                                                          
    [dex] Converting compiled files and external libraries into <test_path>\bin\classes.dex...
     [dx] no classfiles specified                                                                                                                             
BUILD FAILED                                                                    
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)

回答1:

Here is one more way for those, who don't want to move to gradle and wish to stay with ant. Btw, main reason, why old way doesn't work, is moving of uiautomator starting from its 2.0 version from standalone test runner for jars to standard android 'am instrument' test runner for apps. This move has only one 'contra'. Now test projects should be bound to a definite target app (see workaround in the first step). So here is a plan.

First of all, you should have a target project, for which your test is designed. In fact it can be an empty app, which will not be shown at all, neither in apps menu, nor during testing. I've managed to create one in Eclipse, without creating any activity in wizard. To make ant's build.xml run:

android update project --target 0 --path %path_to_empty_app%

For more information about android tool see http: //developer. android. com/ tools/ projects/ projects-cmdline .html

Note: you may want to give necessary permissions to your target app, which will be spread to test app. Now test is not run with shell user permissions.

Second step is creating a test project. As I've mentioned, uiautomator is now integrated in standard android testing scheme. Thus, it uses a standard command for creating test apps:

android create test-project -m %path_to_target_app% -n %test_app_name% -p %path_to_test_app%

A usual app structure will be created in %path_to_test_app%, including ant's build.xml For more information see http://developer.android.com/tools/testing/testing_otheride.html

Third: copy uiautomator classes jar to test app libs. The jar can be extracted from *.aar archive situated in SDK in \extras\android\m2repository\com\android\support\test\uiautomator\uiautomator-v18\2.1.0 or similar.

Fourth: put your test class *.java to test app src folder. Note the following changes in uiautomator here:

  1. package is renamed from com.android.uiautomator to android.support.test.uiautomator
  2. UiAutomatorTestCase class is left for compatibility, but is deprecated; extend your test class from InstrumentationTestCase, to get UiDevice instance, use UiDevice.getInstance(getInstrumentation())

Fifth: install and run your test. Simple way is the following:

cd %path_to_test_app%
:: Here 'ant instrument install' builds and installs both target and test apps.
ant instrument install
ant test

or the last line can be modified to

adb shell am instrument -w %target_app_full_name%.tests/android.test.InstrumentationTestRunner

For more information see http://developer.android.com/reference/android/test/InstrumentationTestRunner.html



回答2:

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

build:

.\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)

run:

.\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