Android Studio : Cannot resolve symbol "Truth" (Truth library)
Asked Answered
R

5

16

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:

  • ./gradlew app:dependencies
  • assembleAndroidTest

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()
}
Renal answered 21/5, 2018 at 11:35 Comment(0)
A
16

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 .

Antennule answered 21/5, 2018 at 11:43 Comment(7)
Thank you for your help @IntelliJ Amiya. I added the compile options and the depending in my build.gradle. But when I sync I got this error : Could not find method compileOptions() for arguments [build_4gfl49u8k92u69bygafbqspv9$_run_closure3@3a4b6224] on project ':app' of type org.gradle.api.Project.Renal
Should I keep both lines in the dependencies ? : testImplementation 'com.google.truth:truth:0.40' and testImplementation group: 'com.google.truth.extensions', name: 'truth-java8-extension', version: '0.40'Renal
@younes-makhtoum Restart IDE and see #23796904Antennule
@younes-makhtoum you can. no problemAntennule
The problem is still not solved. what pointed you to the fact that I might need to use the Java 8 extensions? From what I understood, it's not mandatory and only required when intending to test specific Java 8 assertions like Optional and Stream, which is not my case.Renal
@younes-makhtoum add androidTestImplementation "com.google.truth:truth::0.40"Antennule
Yes! it's fixed now! Thanks a lot. Please update your core response so that I can upvote it and mark it as the right one.Renal
H
3

Add the appropriate dependency to your build file:

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

You need the Java 8 extensions if and only if you're testing Java 8 code (Stream, Optional, etc):

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

Halie answered 2/8, 2020 at 20:17 Comment(0)
D
1

You need to add mavenCentral to your repositories

repositories {
  mavenCentral()
}
dependencies {
  testImplementation "com.google.truth:truth:1.1.2"
}
Disembodied answered 17/2, 2021 at 13:6 Comment(0)
C
0

JUST DON'T FORGET TO USE

mavenCentral() instead of jcenter()

allprojects {
   repositories {
      google()
      mavenCentral()
   }
}
Call answered 28/6, 2021 at 3:53 Comment(0)
B
0

As android Gradle plugin requires java 11. Please update Gradle JDK enter image description here

Beggarweed answered 8/2, 2022 at 7:58 Comment(1)
don't forget to resync the project with Gradle file. after JDK upgrade.Beggarweed

© 2022 - 2024 — McMap. All rights reserved.