关于测试:Android Studio:无法解析符号“ Truth”(Truth库)

Android Studio : Cannot resolve symbol “Truth” (Truth library)

遇到以下要点后:https://gist.github.com/chemouna/00b10369eb1d5b00401b,我注意到它正在使用Google Truth库:https://google.github.io/truth/。 因此,我首先按照以下步骤在Android Studio的build.gradle文件中添加了库:

1
2
3
4
5
6
7
buildscript {
  repositories.mavenLocal()
}

dependencies {
  testImplementation"com.google.truth:truth:0.40"
}

但是,当我想为我的断言java类的Truth入口点添加静态导入时:

1
2
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

我收到符号Truth无法解析的错误。
我试图重建我的项目并实现此处所述的解决方案:导入中未识别的AndroidTestCompile依赖项,主要运行以下gradle任务:

  • ./gradlew应用程序:依赖项
  • 组装AndroidTest

但问题仍然存在。

有什么帮助吗?

我是否应该在build.gradle文件中实际添加这些行? :

1
2
3
 buildscript {
  repositories.mavenLocal()
}

如果我已经有这些:

1
2
3
4
5
repositories {
   mavenCentral()
   jcenter()
   google()
}

To use the Java 8 extensions, also include
com.google.truth.extensions:truth-java8-extension:0.40.

注意

您应该致电androidTestImplementation

1
androidTestImplementation"com.google.truth:truth::0.40"

读取Truth - Fluent assertions for Java