27

I'm trying to import

 import android.support.test.InstrumentationRegistry;

my build.gradle file

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'

in default config:

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

Is there a library I'm missing here? I'm trying to import InstrumentationRegistry but it doesn't recognise it!

Adz
  • 2,809
  • 10
  • 43
  • 61

4 Answers4

37

Check what kind of test do you use.

InstrumentationRegistry used for Instrumented tests that use emulator or device and they are placed in src/androidTest and use config androidTestCompile.
If you use Local unit tests for JVM from folder src/test you should use config testCompile

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

After that you can import InstrumentationRegistry, but you will get other errors at run-time.

ashishdhiman2007
  • 807
  • 2
  • 13
  • 28
Tim
  • 526
  • 6
  • 6
  • 1
    Agree, great answer! God bless you!) I've changed androidTestCompile to testCompile and InstrumentationRegistry resolved, – Kiryl Ivanou Nov 10 '17 at 14:33
5

try

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

instead of

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

thor
  • 21,418
  • 31
  • 87
  • 173
1

it seems, com.android.support.test had been recently excluded from some other package (no clue which one), which also resulted in android.support.test.InstrumentationRegistryto be unknown; not excluding it from com.android.support.test:runner fixed the issue for me.

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

basically, androidTestImplementation needs to contain com.android.support.test once.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
0

Another alternative - you can also add androidx.test:monitor dependency where the class InstrumentedRegistry is.

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

Patryk Kubiak
  • 1,679
  • 2
  • 11
  • 17