切换到AndroidX并收到弃用的:import androidx.test.InstrumentationRegistry。
如果我做了下一个导入:import androidx.test.platform.app.InstrumentationRegistry,我就不能使用getContext()了。
例如:val context = InstrumentationRegistry.getContext()。
在build.gradle中
androidTestImplementation 'androidx.test.ext:junit:1.0.0-beta02'
androidTestImplementation 'androidx.test:runner:1.1.0-beta02'发布于 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'。
发布于 2018-12-19 04:00:09
当您使用Android X时,您需要确保您的应用程序的build.gradle文件中包含以下内容
androidTestImplementation 'androidx.test:core:1.1.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'第二个是确保在测试中使用正确的AndroidJUnit4。
确保将这两个文件都导入。
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4现在,您可以使用如下所示的代码行,而不是使用val context = InstrumentationRegistry.getContext()
val context = InstrumentationRegistry.getInstrumentation().getTargetContext()发布于 2019-05-24 13:27:49
以下代码现在已弃用:
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();请改用:
Context context = ApplicationProvider.getApplicationContext();https://stackoverflow.com/questions/52924431
复制相似问题