androidx.test.InstrumentationRegistry已弃用 [英] androidx.test.InstrumentationRegistry is deprecated
问题描述
切换到AndroidX
并已弃用:import androidx.test.InstrumentationRegistry
.
如果我进行下一次导入:import androidx.test.platform.app.InstrumentationRegistry
我不能使用getContext()
.
If I made next import: import androidx.test.platform.app.InstrumentationRegistry
I can't use getContext()
.
例如:
val context = InstrumentationRegistry.getContext()
.
在build.gradle
中:
androidTestImplementation 'androidx.test.ext:junit:1.0.0-beta02'
androidTestImplementation 'androidx.test:runner:1.1.0-beta02'
推荐答案
You can use InstrumentationRegistry.getInstrumentation().getTargetContext()
in the most cases from androidx.test.platform.app.InstrumentationRegistry
.
如果需要该应用程序,则可以使用ApplicationProvider.getApplicationContext<MyAppClass>()
.
If you need the Application, you can use ApplicationProvider.getApplicationContext<MyAppClass>()
.
如果您还没有,我想您也可以使用新的测试依赖项:
androidTestImplementation 'androidx.test:core:1.0.0-beta02'
.
If you haven't already, I think you can also use the new test dependency:
androidTestImplementation 'androidx.test:core:1.0.0-beta02'
.
这篇关于androidx.test.InstrumentationRegistry已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!