androidtestingandroid-espressogoogle-truth

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


After coming across this gist : https://gist.github.com/chemouna/00b10369eb1d5b00401b, I noticed it was using the Google Truth library : https://google.github.io/truth/. So I started by following the procedure to add the library in my build.gradle file in Android Studio:

buildscript {
  repositories.mavenLocal()
}

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

But when I wanted to add the static imports for Truth’s entry points for my assertions java class:

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

I got the error that the symbol Truth cannot be resolved. I tried to rebuild my project and implement the solutions stated here: AndroidTestCompile dependencies not recognized in the imports, mainly running the following gradle tasks:

but the problem persists.

Any help on that?

Should I actually add these lines in my build.gradle file ? :

 buildscript {
  repositories.mavenLocal()
}

if I already have these :

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

Solution

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

    NOTE

    You should call androidTestImplementation

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

    Read more information about Truth - Fluent assertions for Java .