在测试代码中添加以下依赖项:
dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'com.android.support.test:runner:1.3.0'
androidTestImplementation 'com.android.support:support-annotations:28.0.0'
androidTestImplementation ('com.android.support.test.espresso:espresso-contrib:3.3.0') {
exclude group: 'com.android.support', module: 'support-annotations'
}
}
这个问题通常在升级到Android 12后发生,因为Espresso库中引入的AndroidX Test库版本不同,而这个库可能与Android 12不兼容。上面的依赖项可以解决这个问题,其中自定义规则在库espresso-contrib中排除了support-annotations的引用,因为它与AndroidX Test库中的androidx.annotation相冲突,会在Android 12上导致问题。