androidx.test.InstrumentationRegistry 已弃用 [英] androidx.test.InstrumentationRegistry is deprecated

查看:64
本文介绍了androidx.test.InstrumentationRegistry 已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到 AndroidX 并收到弃用:import androidx.test.InstrumentationRegistry.

Switched to AndroidX and received deprecated: 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'

推荐答案

您可以在大多数情况下使用 InstrumentationRegistry.getInstrumentation().getTargetContext() 来自 androidx.test.platform.app.InstrumentationRegistry.

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

如果需要应用程序,可以使用ApplicationProvider.getApplicationContext().

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆