Android Espresso test '无法解析符号'InstrumentationRegistry



我正在尝试导入

 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,但它无法识别!

检查您使用的测试类型

InstrumentationRegistry用于仪器测试使用模拟器或设备,它们被放置在src/androidTest中,并使用configandroidTestCompile
如果从src/test文件夹为JVM使用本地单元测试,则应该使用configtestCompile

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

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

尝试

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

而不是

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

似乎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一次。

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

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

最新更新