切换到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-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()
https://stackoverflow.com/questions/52924431
复制相似问题