Add the required libraries into the dependencies section of your Android module’s build.gradle:

android {
...
  defaultConfig {
    ...
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  }
}

dependencies {
  ...
  androidTestCompile 'com.android.support.test:runner:0.5'
  androidTestCompile 'com.android.support.test:rules:0.5'
  androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
  androidTestCompile 'com.android.support:support-annotations:23.4.0'
}

⚠ Note that of course the versions may differ in the mean time.

After this sync with the changes.

Then add a new Java class inside the androidTest folder:

public class InterAppTest extends InstrumentationTestCase {
private UiDevice device;

@Override
public void setUp() throws Exception {
   device = UiDevice.getInstance(getInstrumentation());
}

public void testPressHome() throws Exception {
  device.pressHome();
}
}

By making a right click on the class tab and on “Run “InterAppTest” executes this test.