I also use uiautomator 2.0 from AndroidStudio. Here are some answers to your questions.
How to install it on the device? How to run it in the device?
Make sure your device is connected using
adb devices
If not, you should connect it using
adb kill-server adb connect xxx.xxx.xxx.xxx
Then from AndroidStudio, right-click on your test class and click "Run Program."
Does anything need to be done in AndroidManifest?
I have nothing special in my manifest, but be sure to add
android { defaultConfig { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } }
in build.gradle file
Am I editing in the right place? Do I have to do something under src / main?
Yes, you are editing in the right place. But you can move your code to src/main . To do this, you will need to change androidTestCompile to compile in the build.gradle file.
I have not tried to run the test from the command line yet, but you can see the AndroidStudio commands, maybe this can help.
Hope this helps you.
EDIT 1
I am using this code
build.gradle (projectRoot)
apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "22.0.0" lintOptions { abortOnError false } packagingOptions { exclude 'NOTICE' exclude 'LICENSE.txt' } defaultConfig { minSdkVersion 19 targetSdkVersion 22 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.0.0' compile 'com.android.support.test:testing-support-lib:0.1' compile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0' compile project(':aiccore') }
LoginTestCase (projectRoot / src / main / LoginTestCase.java)
public class LoginTestCase extends InstrumentationTestCase { protected UiDevice device = null; protected String appName; public LoginTestCase() { this("YourAppName") } public LoginTestCase(String appName) { this.appName = appName; } public void runApp(String appName) throws UiObjectNotFoundException, RemoteException { device = UiDevice.getInstance(getInstrumentation()); device.pressHome(); device.waitForWindowUpdate("", 2000); UiObject2 allAppsButton = device.findObject(By.desc("Apps")); allAppsButton.click(); device.waitForWindowUpdate("", 2000); UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true)); appViews.setAsHorizontalList(); UiObject settingsApp = appViews.getChildByText(new UiSelector().className(TextView.class.getName()), appName); settingsApp.clickAndWaitForNewWindow(); assertTrue("Unable to detect app", settingsApp != null); } @Override public void setUp() throws RemoteException, UiObjectNotFoundException { this.runApp(appName); } @Override public void tearDown() throws RemoteException, UiObjectNotFoundException {