9

I'm learn to use espresso to test my app, but ActivityTestRule always can't be resolved. Here is my app/build.gradle config, and I'm sure that I put test classes at src/androidTest.

Many thanks for help!!

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'
}
coder
  • 8,346
  • 16
  • 39
  • 53
CarsonJc
  • 93
  • 1
  • 3
  • check your imports check if this is added. import android.support.test.rule.ActivityTestRule; – Jojo Narte May 30 '18 at 03:54
  • 1
    Answer by GParekar works but we should not have to downgrade the dependencies. There is a better solution and explanation at: https://stackoverflow.com/a/50070152/480269 – Alan Jun 05 '18 at 16:06

2 Answers2

11

I hope this will work for you.

Use

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

Instead

androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestCompile 'com.android.support.test:runner:1.0.2'
GParekar
  • 1,209
  • 1
  • 8
  • 15
  • 9
    I would suggest adding `androidTestImplementation 'com.android.support.test:rules:1.0.2'` instead of downgrading the library versions; that's where the ActivityTestRule class resides now... – Guno Heitman Oct 14 '18 at 12:07
  • 7
    `androidTestImplementation 'androidx.test:rules:1.2.0'` for AndroidX – Jérémy Reynaud Apr 03 '20 at 14:00
0

Add import android.support.test.rule.ActivityTestRule; in your imports and then clean and rebuild your app.

Sagar
  • 23,903
  • 4
  • 62
  • 62