Android Studio:无法解析符号 "Truth"(真相库)

Android Studio : Cannot resolve symbol "Truth" (Truth library)

遇到这个要点后:https://gist.github.com/chemouna/00b10369eb1d5b00401b, I noticed it was using the Google Truth library : https://google.github.io/truth/。所以我开始按照程序在 Android Studio 中的 build.gradle 文件中添加库:

buildscript {
  repositories.mavenLocal()
}

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

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

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

我收到无法解析符号 Truth 的错误。 我尝试重建我的项目并实施此处所述的解决方案:,主要是 运行 以下 gradle 任务:

但问题依然存在。

有什么帮助吗?

我真的应该在 build.gradle 文件中添加这些行吗? :

 buildscript {
  repositories.mavenLocal()
}

如果我已经有了这些:

repositories {
   mavenCentral()
   jcenter()
   google()
}

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

注意

你应该打电话给androidTestImplementation

androidTestImplementation "com.google.truth:truth::0.40" // Latest version 1.1.3

阅读有关 Truth - Fluent assertions for Java 的更多信息。

将适当的依赖项添加到您的构建文件中:

testImplementation "com.google.truth:truth:1.0.1"

当且仅当您正在测试 Java 8 代码(流、可选等)时,您才需要 Java 8 扩展:

testImplementation "com.google.truth.extensions:truth-java8-extension:1.0.1"

您需要将 mavenCentral 添加到您的存储库

repositories {
  mavenCentral()
}
dependencies {
  testImplementation "com.google.truth:truth:1.1.2"
}

别忘了使用

mavenCentral() instead of jcenter()

allprojects {
   repositories {
      google()
      mavenCentral()
   }
}

因为 android Gradle 插件需要 java 11. 请更新 Gradle JDK