【问题标题】:Android Studio: Cannot import libraries JUnit and TruthAndroid Studio:无法导入库 JUnit 和 Truth
【发布时间】:2021-11-03 22:11:02
【问题描述】:

在像official documentation 这样为单元测试创​​建类之后,我导入了 com.google.common.truth.Truth.assertThatorg.junit.Test 库,但是android studio 无法获取并显示错误“无法解析符号“Truth””和“无法解析符号“测试””。

在搜索了解决此问题的方法后,我在 StackOverflow 上遇到了一个question,它询问了类似的问题。我做了那里推荐的所有事情,但我仍然无法导入这些类。

我的项目 build.gradle

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

我的项目模块 build.gradle

dependencies {
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'com.google.truth:truth:1.1.3'
}

我的测试课

import com.google.common.truth.Truth.assertThat;
import org.junit.Test;

【问题讨论】:

  • 您的测试在哪个文件夹中? test 还是 androidTest? (他们应该在test

标签: java android android-studio junit google-truth


【解决方案1】:

Android gradle 插件需要 java 11 才能运行。

请将 gradle java 插件更改为 11,如下图所示。

【讨论】:

  • 不要忘记将项目与 Gradle 文件重新同步。 Gradle JDK 更新后
猜你喜欢
  • 2018-10-31
  • 1970-01-01
  • 2016-01-19
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多