androidx.test.InstrumentationRegistry is deprecated

AndroidDeprecatedAndroidxAndroid Testing

Android Problem Overview


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'

Android Solutions


Solution 1 - Android

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'.

Solution 2 - Android

When you're using Android X you need to make sure you have the following in your app's build.gradle file

androidTestImplementation 'androidx.test:core:1.1.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'

The second one is to make sure you have the correct AndroidJUnit4 to use in your tests.

Make sure you import both of these.

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

Now instead of using val context = InstrumentationRegistry.getContext() you can use the line shown below

val context = InstrumentationRegistry.getInstrumentation().getTargetContext()

Solution 3 - Android

The following code is deprecated now:

Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

Instead use:

Context context = ApplicationProvider.getApplicationContext();

Solution 4 - Android

I spent a lot of time moving dependency with testImplementation instead of androidTestImplementation and reverse, in the app build.gradle.

My fault was that I have created the test class in the test folder instead of androidTest folder so getting unresolved error for AndroidJuit4 and InstrumentationRegistry.

When I shifted my test file to the androidTest folder then issue been solved with depandency implementation of test libraries with androidTestImplementation in the build.gradle.

Solution 5 - Android

For Kotlin usage, in order to get Context:

InstrumentationRegistry.getInstrumentation().targetContext

Solution 6 - Android

Use below import

import androidx.test.platform.app.InstrumentationRegistry

Kotlin ex
   InstrumentationRegistry.getInstrumentation().context,

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMorozovView Question on Stackoverflow
Solution 1 - AndroidkarutoView Answer on Stackoverflow
Solution 2 - AndroidMark O'SullivanView Answer on Stackoverflow
Solution 3 - AndroidAkgun StudioView Answer on Stackoverflow
Solution 4 - AndroidAjayView Answer on Stackoverflow
Solution 5 - AndroidlomzaView Answer on Stackoverflow
Solution 6 - AndroidSamView Answer on Stackoverflow