androidx.test.InstrumentationRegistry is deprecated

Morozov picture Morozov · Oct 22, 2018 · Viewed 19.9k times · Source

Switched to AndroidX and received deprecated: import androidx.test.InstrumentationRegistry.

If I made next import: import androidx.test.platform.app.InstrumentationRegistry I can't use getContext().

Ex: val context = InstrumentationRegistry.getContext().

In build.gradle:

androidTestImplementation 'androidx.test.ext:junit:1.0.0-beta02'
androidTestImplementation 'androidx.test:runner:1.1.0-beta02'

Answer

karuto picture karuto · Oct 22, 2018

You can use InstrumentationRegistry.getInstrumentation().getTargetContext() in the most cases from androidx.test.platform.app.InstrumentationRegistry.

If you need the Application, you can use ApplicationProvider.getApplicationContext<MyAppClass>().

If you haven't already, I think you can also use the new test dependency: androidTestImplementation 'androidx.test:core:1.0.0-beta02'.