ActivityTestRule 未导入

ActivityTestRule is not imported

我在 gradle 中添加了以下依赖项:

但是还是无法导入ActivityTestRule,报错

您似乎在使用 Espresso 2.2.2 中的旧依赖项。对于当前版本 (Espresso 3.0.2),ActivityTestRule 依赖项位于

androidTestImplementation 'com.android.support.test:rules:1.0.2'

检查 the official documentation 以确保其他依赖项也已正确设置可能是个好主意,但这是我建议开始使用 Espresso 的内容。

app/build.gradledependencies 部分,添加:

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'

app/build.gradledefaultConfig 部分,添加:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

如果您遇到其他类似的问题,the documentation 包含一长串其他 gradle 依赖项,可能有您需要的,但我上面列出的应该足以帮助您入门.

AndroidX

Note: Answer give by Rapunzel Van Winkle is correct and works but androidx we have to use following dependencies,

在应用级别添加gradle

androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'

检查与AndroidX Test相关的不同依赖项 official documentation