Robolectric RuntimeEnvironment.application is deprecated, where is ApplicationProvider.getApplicationContext()?

Charles Madere picture Charles Madere · Oct 19, 2018 · Viewed 9.1k times · Source

I switched to Robolectric 4.0 Beta 1 from 3.8 because I need compatibility with Android 9 (API 28).

With this change, RuntimeEnvironment.application is now deprecated.

The replacement is apparently to use:

androidx.test.core.app.ApplicationProvider.getApplicationContext()

I have no idea where exactly this code is. It must be within a separate dependency entirely. Where is it? How do I add it to my project?

Thank you!

Answer

dudi picture dudi · Oct 22, 2018

As described in the Robolectric Migration Guide to 4.0 you have to add the androidx test core dependencies testImplementation 'androidx.test:core:1.0.0' to your build.gradle After this you can use the ApplicationProvider.getApplicationContext() method