Android Studio无法正确导入androidx测试类

3

我一直在研究一个UIAutomator示例项目。我使用AndroidX测试库创建了新的测试用例。

我能够从命令行构建并运行测试用例。但是当我尝试从Android Studio打开应用程序时,我发现大部分类没有正确导入。例如:

import androidx.test.uiautomator.UiObject2;
import org.junit.Before;

以上两个导入未被导入。同样的错误在许多类中都出现,除了android.content.Context、android.content.Intent等。
有人能否解释一下。我能够从命令行正确地执行测试用例,但无法从Android Studio中正确地执行它们。
我已经重启了电脑/Studio,并清除了缓存,问题仍然存在。
以下是我在app/build.gradle中添加的依赖项。
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
androidTestImplementation 'androidx.test:core:1.0.0'
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'junit:junit:4.12'

将以下代码添加到gradle.properties中

android.useAndroidX=true
android.enableJetifier=true

我大多数情况下都是按照android-testing-master/ui/uiautomator示例代码所提供的方式进行操作的,但不确定我是否遗漏了什么。


重新构建您的项目,如果问题仍然存在,请将项目与Gradle文件同步。 - nimi0112
1个回答

0

我也遇到了同样的问题(Android Studio 3.4.2),uiautomator 显示红色:

import androidx.test.uiautomator.UiDevice

即使清理项目、重建项目和无效化缓存都没有帮助,但选择另一个构建变体却解决了我的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接