I am adding Espresso to my project in Android Studio. I have installed the Support Repository and in fact have already been using pieces of it. Then I added these dependenci
There are 2 different test dependencies configurations:
testCompile
- used by unit test suite (located in src/test
folder and invoked by ./gradlew test
) androidTestCompile
- used by integration test suite (located at src/androidTest
folder and invoked by ./gradlew connectedAndroidTest
).My suspicion is that your test code is in the wrong test suite location
In your case your test code should go into src/androidTest
folder and test suite should be executed by running ./gradlew connectedAndroidTest
I had this issue as well and I have my android test cases under src/androidTests as recommended by Google, but this caused problems with build.gradle:
sourceSets {
main {
java.srcDirs = ['src']
}
}
With the above it's trying to compile all my test cases within the normal debug compilation target, which does not include the espresso and other dependencies because they're listed under androidTestCompile.
In the end I fixed this by excluding the androidTest subdirectory from compilation and set the root of androidTest to the appropriate directory.
sourceSets {
main {
java.srcDirs = ['src']
java.excludes = ['androidTest/**']
}
androidTest.setRoot('src/androidTest')
}
I encountered this issue while forward-porting one of my apps into a new visual paradigm, and found my app-level build.gradle was missing the following:
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
1.Click the drop down menu beside the Run button in the toolbar.
2.Click Edit configuration
3.Now remove all others except app(within Android App) and the Default one.
This worked for me. Hope it helps.
My solution was easier and simpler, I just went to Android Studio File>Invalidate Caches/Restart
and it worked properly, seems that android studio is keeping some cache that will not clean with Rebuild/Clean
.
I've had the same problem and it was solved by hitting Clean Project from the Build tab in Android Studio.
After hitting Clean Project, watch the Gradle Console for potential errors and if it completes the cleaning successfully, simply go into any one of your test classes and type in "Espresso" and the smart code completion should offer suggestions. Everything should automatically import as you use Espresso after that.
Hope this helps!