无法解析符号'ActivityTestRule'

9
我学习使用Espresso测试我的应用程序,但是ActivityTestRule总是无法解析。这是我的app/build.gradle配置,并且我确定我将测试类放在src/androidTest中。
非常感谢您的帮助!! enter image description here
apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.android.teatime"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.android.support:design:27.1.1'
    compile 'com.android.support:support-annotations:27.1.1'
    compile 'com.google.android.gms:play-services-appindexing:9.8.0'
    // Testing-only dependencies
    androidTestCompile 'com.android.support:support-annotations:27.1.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestCompile 'com.android.support.test:runner:1.0.2'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:3.0.2'
    androidTestCompile 'com.android.support.test:rules:1.0.2'
}

请检查您的导入,确认是否已添加以下内容: import android.support.test.rule.ActivityTestRule; - Jojo Narte
1
GParekar的回答是有效的,但我们不应该降级依赖项。在此链接中有更好的解决方案和解释:https://dev59.com/MVUL5IYBdhLWcg3wopMM#50070152 - Alan
2个回答

11

我希望这对你有用。

使用

androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

反而

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

9
жҲ‘е»әи®®ж·»еҠ androidTestImplementation 'com.android.support.test:rules:1.0.2'иҖҢдёҚжҳҜйҷҚдҪҺеә“зҡ„зүҲжң¬пјӣеӣ дёәзҺ°еңЁActivityTestRuleзұ»е°ұеңЁиҝҷдёӘеә“йҮҢйқўгҖӮ - Guno Heitman
7
针对 AndroidX 的 androidTestImplementation 'androidx.test:rules:1.2.0' - Jérémy Reynaud

0
将以下代码添加到您的导入中:import android.support.test.rule.ActivityTestRule;,然后清除并重新构建您的应用程序。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接