Android Espresso测试'无法解析符号' InstrumentationRegistry''

时间:2016-03-28 12:27:19

标签: android unit-testing junit android-espresso

我试图导入

 import android.support.test.InstrumentationRegistry;

我的build.gradle文件

androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

在默认配置中:

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

我在这里找不到图书馆吗?我试图导入InstrumentationRegistry,但它没有识别它!

4 个答案:

答案 0 :(得分:28)

检查您使用的是哪种测试。

InstrumentationRegistry用于检测测试 使用模拟器或设备,它们放在src / androidTest中并使用config androidTestCompile
如果您从文件夹src / test使用本地单元测试用于JVM,则应使用 config testCompile

testImplementation 'com.android.support.test:runner:1.0.2'

之后您可以导入InstrumentationRegistry,但在运行时会遇到其他错误。

答案 1 :(得分:2)

尝试

编译' com.android.support.test:runner:0.2'

而不是

testCompile' com.android.support.test:runner:0.2'

答案 2 :(得分:1)

看来,com.android.support.test最近被排除在其他一些软件包之外(不知道哪一个),这也导致android.support.test.InstrumentationRegistry未知;不从com.android.support.test:runner中排除它为我解决问题。

androidTestImplementation ("com.android.support.test:runner:1.0.2") {
    // exclude group: "com.android.support.test"
    exclude group: "com.android.support"
}

基本上,androidTestImplementation需要包含com.android.support.test一次。

答案 3 :(得分:0)

另一种选择 - 您还可以在类 androidx.test:monitor 所在的位置添加 InstrumentedRegistry 依赖项。

androidTestImplementation 'androidx.test:monitor:x.y.z'