首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >androidx.test.InstrumentationRegistry已弃用

androidx.test.InstrumentationRegistry已弃用
EN

Stack Overflow用户
提问于 2018-10-22 15:39:51
回答 6查看 24.9K关注 0票数 61

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

如果我做了下一个导入:import androidx.test.platform.app.InstrumentationRegistry,我就不能使用getContext()了。

例如:val context = InstrumentationRegistry.getContext()

build.gradle

代码语言:javascript
复制
androidTestImplementation 'androidx.test.ext:junit:1.0.0-beta02'
androidTestImplementation 'androidx.test:runner:1.1.0-beta02'
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2018-10-22 23:00:30

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

如果您需要该应用程序,您可以使用ApplicationProvider.getApplicationContext<MyAppClass>()

如果您还没有,我认为您还可以使用新的测试依赖项:androidTestImplementation 'androidx.test:core:1.0.0-beta02'

票数 102
EN

Stack Overflow用户

发布于 2018-12-19 04:00:09

当您使用Android X时,您需要确保您的应用程序的build.gradle文件中包含以下内容

代码语言:javascript
复制
androidTestImplementation 'androidx.test:core:1.1.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'

第二个是确保在测试中使用正确的AndroidJUnit4。

确保将这两个文件都导入。

代码语言:javascript
复制
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

现在,您可以使用如下所示的代码行,而不是使用val context = InstrumentationRegistry.getContext()

代码语言:javascript
复制
val context = InstrumentationRegistry.getInstrumentation().getTargetContext()
票数 50
EN

Stack Overflow用户

发布于 2019-05-24 13:27:49

以下代码现在已弃用:

代码语言:javascript
复制
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

请改用:

代码语言:javascript
复制
Context context = ApplicationProvider.getApplicationContext();
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52924431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档