Build Uiautomator 2.0 From Command Line
Solution 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 --target0--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:
- package is renamed from com.android.uiautomator to android.support.test.uiautomator
- 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
Solution 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
Post a Comment for "Build Uiautomator 2.0 From Command Line"