Obviously I need the correct import statment to solve this problem. According to the docs for AndroidJUnit4, this should be

import android.support.test.runner.AndroidJUnit4;

When I do that, Android Studio highlights runner in red and complains "Cannot resolve symbol 'runner'".

Background

I got to this point by following the tutorials on the Android Developer site for setting up tests using UI Automator. The first problem I encountered was that com.android.support:support-v4:22.2.0 and com.android.support.test:runner:0.2 depend on different versions of com.android.support:support-annotations. I followed the suggestions from this Android bug report and added the following to allprojects in my project's build.gradle:

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}

This solved the immediate error, but I suspect it lead to my current problems. Does anyone have any suggestions about how to fix this?

Relevent sections from `./gradlew :app:dependencies

androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.jayway.android.robotium:robotium-solo:5.2.1
+--- com.squareup:fest-android:1.0.8
|    \--- org.easytesting:fest-assert-core:2.0M10
|         \--- org.easytesting:fest-util:1.2.5
+--- com.android.support.test:runner:0.2
|    +--- junit:junit-dep:4.10
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
|    \--- com.android.support:support-annotations:22.0.0 -> 22.2.0
+--- com.android.support.test:rules:0.2
|    \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.uiautomator:uiautomator-v18:2.1.0

compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.2.0
|    \--- com.android.support:support-v4:22.2.0
|         \--- com.android.support:support-annotations:22.2.0
+--- com.android.support:support-v4:22.2.0 (*)
+--- com.google.android.gms:play-services:6.1.71
|    \--- com.android.support:support-v4:20.0.0 -> 22.2.0 (*)
+--- com.crashlytics.android:crashlytics:1.+ -> 1.1.13
\--- com.jakewharton:butterknife:5.1.2

Make sure your app in debug build variant. Go to Build > Select Build Variant... and the following should show up:

enter image description here


I made the mistake to put the test classes at src/test. After moving them to src/androidTest/java/ the dependency was resolved.


Ok so here is your mistake and mine!

If we are going to write a pice of code for Local Unit Testing we shouldn't use @RunWith(AndroidJUnit4.class) cause we do not use AndroidJUnit4 but we need Junit4. so we should write @RunWith(JUnit4.class). And of course your java test file is under app/src/test/java/your.package.name directory.

Else if (!!) we want to write some Android Instrumented Unit Test we should put our test java files in app/src/androidTest/java/your.package.name directory and use annotation like @RunWith(AndroidJUnit4.class)


Update

The Android Test Library is now part of AndroidX. Be sure to use the correct Gradle dependencies found in the official documentation.

Original Answer

I found here that there are newer versions of the Testing Support Library than what I was using:

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'
}

Note: Be sure to use the most recent versions of these libraries. This question is from a time when the Android Test Support Library was new and the version numbers here are very out of date.