Android Studio 无法解析 org.junit
Android Studio cannot resolve org.junit
我在 Android Studio 1.2.2 中使用 JUnit 测试。测试运行没有问题。唯一让我困惑的是 Android Studio 无法解析实际的 org.junit 包。显然它确实解决了问题,否则测试不会 运行。但是导入和注释在 Android Studio 中标记为红色,如此处所示。这是 Android Studio 中的错误吗?我尝试重建应用程序并重新启动 Android Studio,但这并不能解决问题。
import org.junit.Test;
import static org.junit.Assert.*; // cannot resolve symbol 'junit'
public class CanadaTest
{
@Test
public void testCountryName() throws Exception
{
int x = 0;
x++;
}
}
尝试File -> Invalidate Caches / Restart
。
将此放入您的 gradle 文件中:
testCompile 'junit:junit:4.12'
如果none以上工作,可能是你的测试类在错误的目录中。
我 运行 进入这个问题是因为我的文本 类 在 /main/java
目录中而不是 /test
目录中它们应该在的地方......
简而言之:
确保您的测试 类 在 /test
目录中!
如果您重新安装了 android Studio,请记住默认情况下它不会使用您的系统 JDK(项目结构 -> SDK 位置 -> jdk 位置)。
我遇到了这个问题,结果证明这只是因为我将构建变体设置为 "release"。一旦我将其改回 "debug",Android Studio 进行了构建,当它完成时,junit 注释上的红色错误消失了。
pic of build variant setting in android studio
我通过结合一些答案和一些更改解决了这个问题:
1.Make 一定要设置 prepare targetSdkVersion
, compileSdkVersion
和 buildToolsVersion
.
2.I 使用最后一个 Android Gradle Plugin Version
和 Gradle Version
.
3.Add 在应用级别 buil.gradle 的一些行(我用 '\*' 标记它们):
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.zima.testapp"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" //*
apply plugin: 'maven' //*
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
dependencies {
testImplementation 'junit:junit:4.12' //*
implementation 'junit:junit:4.12' //*
androidTestImplementation 'com.android.support:support-annotations:28.0.0' //*
androidTestImplementation 'com.android.support.test:runner:1.0.2' //*
androidTestImplementation 'com.android.support.test:rules:1.0.2' //*
implementation fileTree(dir: 'libs', include: ['*.jar']) //*
implementation fileTree(dir: 'libs', include: 'Parse-*.jar') //*
}
}
4.build.gradle 在项目层面是这样的:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
apply plugin: 'java' //*
}
}
allprojects {
repositories {
jcenter()
google()
maven {
url "https://jitpack.io"
}
}
}
问题
类 和 org.junit.* 的注释未在仪器测试 (androidTest) 中找到。这些 类 是在 junit 测试 (test) 中找到的。
解决方案:
将 Build Variant 从 Release 切换到 Debug.
可能在您的 gradle 文件中有这样的内容:
configurations {
debugImplementation.exclude group: "junit", module: "junit"
}
删除后一切正常。
将junit
更改为最新版本
testImplementation 'junit:junit:'
到
testImplementation 'junit:junit:4.13.2'
我在 Android Studio 1.2.2 中使用 JUnit 测试。测试运行没有问题。唯一让我困惑的是 Android Studio 无法解析实际的 org.junit 包。显然它确实解决了问题,否则测试不会 运行。但是导入和注释在 Android Studio 中标记为红色,如此处所示。这是 Android Studio 中的错误吗?我尝试重建应用程序并重新启动 Android Studio,但这并不能解决问题。
import org.junit.Test;
import static org.junit.Assert.*; // cannot resolve symbol 'junit'
public class CanadaTest
{
@Test
public void testCountryName() throws Exception
{
int x = 0;
x++;
}
}
尝试File -> Invalidate Caches / Restart
。
将此放入您的 gradle 文件中:
testCompile 'junit:junit:4.12'
如果none以上工作,可能是你的测试类在错误的目录中。
我 运行 进入这个问题是因为我的文本 类 在 /main/java
目录中而不是 /test
目录中它们应该在的地方......
简而言之:
确保您的测试 类 在 /test
目录中!
如果您重新安装了 android Studio,请记住默认情况下它不会使用您的系统 JDK(项目结构 -> SDK 位置 -> jdk 位置)。
我遇到了这个问题,结果证明这只是因为我将构建变体设置为 "release"。一旦我将其改回 "debug",Android Studio 进行了构建,当它完成时,junit 注释上的红色错误消失了。 pic of build variant setting in android studio
我通过结合一些答案和一些更改解决了这个问题:
1.Make 一定要设置 prepare targetSdkVersion
, compileSdkVersion
和 buildToolsVersion
.
2.I 使用最后一个 Android Gradle Plugin Version
和 Gradle Version
.
3.Add 在应用级别 buil.gradle 的一些行(我用 '\*' 标记它们):
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.zima.testapp"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" //*
apply plugin: 'maven' //*
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
dependencies {
testImplementation 'junit:junit:4.12' //*
implementation 'junit:junit:4.12' //*
androidTestImplementation 'com.android.support:support-annotations:28.0.0' //*
androidTestImplementation 'com.android.support.test:runner:1.0.2' //*
androidTestImplementation 'com.android.support.test:rules:1.0.2' //*
implementation fileTree(dir: 'libs', include: ['*.jar']) //*
implementation fileTree(dir: 'libs', include: 'Parse-*.jar') //*
}
}
4.build.gradle 在项目层面是这样的:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
apply plugin: 'java' //*
}
}
allprojects {
repositories {
jcenter()
google()
maven {
url "https://jitpack.io"
}
}
}
问题
类 和 org.junit.* 的注释未在仪器测试 (androidTest) 中找到。这些 类 是在 junit 测试 (test) 中找到的。
解决方案:
将 Build Variant 从 Release 切换到 Debug.
可能在您的 gradle 文件中有这样的内容:
configurations {
debugImplementation.exclude group: "junit", module: "junit"
}
删除后一切正常。
将junit
更改为最新版本
testImplementation 'junit:junit:'
到
testImplementation 'junit:junit:4.13.2'