无法解析符号 'AndroidJUnit4'
Cannot resolve symbol 'AndroidJUnit4'
显然我需要正确的导入语句来解决这个问题。根据docs for AndroidJUnit4
,这应该是
import android.support.test.runner.AndroidJUnit4;
当我这样做时,Android Studio 以红色突出显示 runner
并抱怨 "Cannot resolve symbol 'runner'"。
背景
我按照 setting up tests using UI Automator. The first problem I encountered was that com.android.support:support-v4:22.2.0
and com.android.support.test:runner:0.2
depend on different versions of com.android.support:support-annotations
. I followed the suggestions from this Android bug report Android 开发者网站上的教程达到了这一点,并将以下内容添加到我项目 build.gradle
的 allprojects
中:
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}
这解决了直接错误,但我怀疑它导致了我当前的问题。有人对如何解决此问题有任何建议吗?
`./gradlew :app:dependencies
中的相关部分
androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.jayway.android.robotium:robotium-solo:5.2.1
+--- com.squareup:fest-android:1.0.8
| \--- org.easytesting:fest-assert-core:2.0M10
| \--- org.easytesting:fest-util:1.2.5
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | \--- org.hamcrest:hamcrest-core:1.1
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| \--- com.android.support:support-annotations:22.0.0 -> 22.2.0
+--- com.android.support.test:rules:0.2
| \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.uiautomator:uiautomator-v18:2.1.0
compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.2.0
| \--- com.android.support:support-v4:22.2.0
| \--- com.android.support:support-annotations:22.2.0
+--- com.android.support:support-v4:22.2.0 (*)
+--- com.google.android.gms:play-services:6.1.71
| \--- com.android.support:support-v4:20.0.0 -> 22.2.0 (*)
+--- com.crashlytics.android:crashlytics:1.+ -> 1.1.13
\--- com.jakewharton:butterknife:5.1.2
更新
Android 测试库现在是 AndroidX 的一部分。请务必使用 official documentation.
中找到的正确 Gradle 依赖项
原答案
我发现 here 测试支持库的版本比我使用的版本更新:
dependencies {
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
注意:请务必使用这些库的最新版本。这个问题是在 Android 测试支持库是新的并且这里的版本号非常过时的时候提出的。
我错误地将测试 类 放在了 src/test。将它们移动到 src/androidTest/java/ 后,依赖关系得到解决。
好的,这是你和我的错误!
如果我们要为 Local Unit Testing 编写一段代码,我们不应该使用 @RunWith(AndroidJUnit4.class)
因为我们不使用 AndroidJUnit4 但我们需要 Junit4。所以我们应该写@RunWith(JUnit4.class)
。当然,您的 java 测试文件位于 app/src/test/java/your.package.name
目录下。
否则,如果 (!!) 我们想写一些 Android Instrumented Unit Test,我们应该将测试 java 文件放在 app/src/androidTest/java/your.package.name
目录中,并使用像 @RunWith(AndroidJUnit4.class)
[= 这样的注释17=]
确保您的应用处于调试构建变体中。转到 Build > Select Build Variant... 应显示以下内容:
将此代码放入您的依赖项
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
我通过对应用程序的 build.gradle 文件进行小的改动解决了这个问题。在 dependencies { ... }
部分,确保包含以下行:
debugImplementation 'com.android.support.test:runner:1.0.1'
或当时最新的任何版本(...Compile
已弃用并已被 ...Implementation
取代)。 注意debugImplementation
的使用。 Android Studio 建议将其自动包含在 androidTestImplementation
中,但没有用。
我通过查看应用程序模块的依赖项下的项目结构找到了如何将其从测试更改为调试,您可以在其中更改每个依赖项的范围,请参见下文。
就我而言,这有助于发布变体:
android {
...
testBuildType "release"
}
正在添加
compile com.android.support.test:runner:0.5'
为我解决了这个问题。
如果还有人遇到这个问题:
Cannot resolve symbol 'AndroidJUnit4'
并使用API 27,在应用程序模块中的build.gradle
中,添加以下行:
testImplementation 'junit:junit:4.12'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
// Espresso dependencies
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
如答案列表所示,这可能是由几件事引起的。列表中的另一个:
我 运行 一个过分热心的 LINT,它删除了所有未使用的导入。这将产生相同的错误,而且很容易忽略这就是问题所在。
Android-studio 将突出显示测试代码中缺少的引用 - 并且将出现 ALT-ENTER 弹出窗口(这是容易错过的位)。
接下来,我需要从 LINT 中删除测试 - 或者至少禁用此警告。
编辑:@Code-Apprentice,缺少的行是:
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
所以文件中的第一个错误是 @RunWith(AndroidJUnit4.class)
在我的测试开始时 class。
如果您使用的项目具有多种构建类型,则必须在模块的 build.gradle 文件中使用 testBuildType 标记提及在构建变体 window 中选择的构建类型。
例如:如果您使用构建类型 debug 那么您应该添加 android{testBuildType "debug" }
,如果使用 stage 那么在 android 标签中添加 android{testBuildType "stage"}
语句。
经典的InvalidateCaches/Restart对我有帮助! :)
将测试 class 移至 src/androidTest/java/。然后依赖将解决。
请注意,这个 OP 现在是 2019 年,已有 4 年历史了,所以如果您使用 Android X,那么 AndroidJUnit4.class
已被弃用,您在那里有一个错误,这个 androidx.test.ext.junit.runners.AndroidJUnit4
。我建议阅读此链接以解决问题。
对我来说 Android Studio 建议替换
@RunWith(AndroidJUnit4.class)
已弃用
@RunWith(AndroidJUnit4ClassRunner.class)
还有这个
androidx.test.ext.junit.runners.AndroidJUnit4
有了这个
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
之后错误消失了,但我不知道以后的测试 while 运行 ok ?!
在您的 build.gradle 文件中添加此依赖项:
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
将最终版本 (1.1.1
) 更新为最新发布的版本。
短篇小说版:
Upgrade your Gradle to the latest Version
我将在 2020 年 2 月 15 日回答这个问题 post。
不幸的是,我已经用尽了这里和其他地方提到的所有可能的解决方案。
https://github.com/codepath/android_guides/wiki/UI-Testing-with-Espresso
https://github.com/udacity/AdvancedAndroid_TeaTime/issues/14
Android Espresso : cannot resolve symbol AndroidJUnit4.class
Android Espresso : cannot resolve symbol AndroidJUnit4.class
是的,以上 none 个有效。我使用内置函数 "Migrate to Andoridx",它可能会提醒我必须升级我的目标 SDK 版本和我的 gradle 版本。在我将 gradle 版本从 2.0.2 升级到 3.5.3 之后。它们可以正常工作,甚至旧的 import 语句也可以。
此问题的常见原因是添加以下依赖项时:
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
如果您要使用插桩测试(androidTest
java 包中的测试),这是一个正确的依赖关系
但是为了实现本地单元测试(test
java 包中的测试),同时使用上述依赖项;那么你将面临Cannot resolve symbol 'AndroidJUnit4'
这是因为 androidTestImplementation
指令用于在插桩测试中导入库,但在本地 JVM/unit 测试中没有。
如果您想在本地 JVM/unit 测试中使用 AndroidJUnit4
,请改用下面的依赖项
testImplementation 'androidx.test.ext:junit:1.1.1'
如果你在插桩测试中使用 AndroidJUnit4
时添加后者的依赖,同样的事情也适用,你也会得到 Cannot resolve symbol 'AndroidJUnit4'
;因为你使用了错误的指令。
当我遵循 Google IOSched 应用并使用三种构建类型 [debug,release,staging] 设置我的项目时,我发生了同样的错误,其中 debug 和 release 共享相同的源目录
sourceSets {
debug.java.srcDir 'src/debugRelease/java'
release.java.srcDir 'src/debugRelease/java'
}
在这种情况下,在模块级 build.gradle 文件中指定 testBuildType
,项目现在应该能够解析符号 'AndroidJUnit4'。
...
sourceSets {
debug.java.srcDir 'src/debugRelease/java'
release.java.srcDir 'src/debugRelease/java'
}
testBuildType "staging"
...
参考:https://github.com/google/iosched/blob/master/mobile/build.gradle
确保你有
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
您的应用 build.gradle 文件中的依赖项。误删了,测试停止了。
显然我需要正确的导入语句来解决这个问题。根据docs for AndroidJUnit4
,这应该是
import android.support.test.runner.AndroidJUnit4;
当我这样做时,Android Studio 以红色突出显示 runner
并抱怨 "Cannot resolve symbol 'runner'"。
背景
我按照 setting up tests using UI Automator. The first problem I encountered was that com.android.support:support-v4:22.2.0
and com.android.support.test:runner:0.2
depend on different versions of com.android.support:support-annotations
. I followed the suggestions from this Android bug report Android 开发者网站上的教程达到了这一点,并将以下内容添加到我项目 build.gradle
的 allprojects
中:
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}
这解决了直接错误,但我怀疑它导致了我当前的问题。有人对如何解决此问题有任何建议吗?
`./gradlew :app:dependencies
中的相关部分androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.jayway.android.robotium:robotium-solo:5.2.1
+--- com.squareup:fest-android:1.0.8
| \--- org.easytesting:fest-assert-core:2.0M10
| \--- org.easytesting:fest-util:1.2.5
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | \--- org.hamcrest:hamcrest-core:1.1
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| \--- com.android.support:support-annotations:22.0.0 -> 22.2.0
+--- com.android.support.test:rules:0.2
| \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.uiautomator:uiautomator-v18:2.1.0
compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.2.0
| \--- com.android.support:support-v4:22.2.0
| \--- com.android.support:support-annotations:22.2.0
+--- com.android.support:support-v4:22.2.0 (*)
+--- com.google.android.gms:play-services:6.1.71
| \--- com.android.support:support-v4:20.0.0 -> 22.2.0 (*)
+--- com.crashlytics.android:crashlytics:1.+ -> 1.1.13
\--- com.jakewharton:butterknife:5.1.2
更新
Android 测试库现在是 AndroidX 的一部分。请务必使用 official documentation.
中找到的正确 Gradle 依赖项原答案
我发现 here 测试支持库的版本比我使用的版本更新:
dependencies {
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
注意:请务必使用这些库的最新版本。这个问题是在 Android 测试支持库是新的并且这里的版本号非常过时的时候提出的。
我错误地将测试 类 放在了 src/test。将它们移动到 src/androidTest/java/ 后,依赖关系得到解决。
好的,这是你和我的错误!
如果我们要为 Local Unit Testing 编写一段代码,我们不应该使用 @RunWith(AndroidJUnit4.class)
因为我们不使用 AndroidJUnit4 但我们需要 Junit4。所以我们应该写@RunWith(JUnit4.class)
。当然,您的 java 测试文件位于 app/src/test/java/your.package.name
目录下。
否则,如果 (!!) 我们想写一些 Android Instrumented Unit Test,我们应该将测试 java 文件放在 app/src/androidTest/java/your.package.name
目录中,并使用像 @RunWith(AndroidJUnit4.class)
[= 这样的注释17=]
确保您的应用处于调试构建变体中。转到 Build > Select Build Variant... 应显示以下内容:
将此代码放入您的依赖项
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
我通过对应用程序的 build.gradle 文件进行小的改动解决了这个问题。在 dependencies { ... }
部分,确保包含以下行:
debugImplementation 'com.android.support.test:runner:1.0.1'
或当时最新的任何版本(...Compile
已弃用并已被 ...Implementation
取代)。 注意debugImplementation
的使用。 Android Studio 建议将其自动包含在 androidTestImplementation
中,但没有用。
我通过查看应用程序模块的依赖项下的项目结构找到了如何将其从测试更改为调试,您可以在其中更改每个依赖项的范围,请参见下文。
就我而言,这有助于发布变体:
android {
...
testBuildType "release"
}
正在添加
compile com.android.support.test:runner:0.5'
为我解决了这个问题。
如果还有人遇到这个问题:
Cannot resolve symbol 'AndroidJUnit4'
并使用API 27,在应用程序模块中的build.gradle
中,添加以下行:
testImplementation 'junit:junit:4.12'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
// Espresso dependencies
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
如答案列表所示,这可能是由几件事引起的。列表中的另一个:
我 运行 一个过分热心的 LINT,它删除了所有未使用的导入。这将产生相同的错误,而且很容易忽略这就是问题所在。
Android-studio 将突出显示测试代码中缺少的引用 - 并且将出现 ALT-ENTER 弹出窗口(这是容易错过的位)。
接下来,我需要从 LINT 中删除测试 - 或者至少禁用此警告。
编辑:@Code-Apprentice,缺少的行是:
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
所以文件中的第一个错误是 @RunWith(AndroidJUnit4.class)
在我的测试开始时 class。
如果您使用的项目具有多种构建类型,则必须在模块的 build.gradle 文件中使用 testBuildType 标记提及在构建变体 window 中选择的构建类型。
例如:如果您使用构建类型 debug 那么您应该添加 android{testBuildType "debug" }
,如果使用 stage 那么在 android 标签中添加 android{testBuildType "stage"}
语句。
经典的InvalidateCaches/Restart对我有帮助! :)
将测试 class 移至 src/androidTest/java/。然后依赖将解决。
请注意,这个 OP 现在是 2019 年,已有 4 年历史了,所以如果您使用 Android X,那么 AndroidJUnit4.class
已被弃用,您在那里有一个错误,这个 androidx.test.ext.junit.runners.AndroidJUnit4
。我建议阅读此链接以解决问题。
@RunWith(AndroidJUnit4.class)
已弃用
@RunWith(AndroidJUnit4ClassRunner.class)
还有这个
androidx.test.ext.junit.runners.AndroidJUnit4
有了这个
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
之后错误消失了,但我不知道以后的测试 while 运行 ok ?!
在您的 build.gradle 文件中添加此依赖项:
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
将最终版本 (1.1.1
) 更新为最新发布的版本。
短篇小说版:
Upgrade your Gradle to the latest Version
我将在 2020 年 2 月 15 日回答这个问题 post。 不幸的是,我已经用尽了这里和其他地方提到的所有可能的解决方案。
https://github.com/codepath/android_guides/wiki/UI-Testing-with-Espresso
https://github.com/udacity/AdvancedAndroid_TeaTime/issues/14
Android Espresso : cannot resolve symbol AndroidJUnit4.class
Android Espresso : cannot resolve symbol AndroidJUnit4.class
是的,以上 none 个有效。我使用内置函数 "Migrate to Andoridx",它可能会提醒我必须升级我的目标 SDK 版本和我的 gradle 版本。在我将 gradle 版本从 2.0.2 升级到 3.5.3 之后。它们可以正常工作,甚至旧的 import 语句也可以。
此问题的常见原因是添加以下依赖项时:
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
如果您要使用插桩测试(androidTest
java 包中的测试),这是一个正确的依赖关系
但是为了实现本地单元测试(test
java 包中的测试),同时使用上述依赖项;那么你将面临Cannot resolve symbol 'AndroidJUnit4'
这是因为 androidTestImplementation
指令用于在插桩测试中导入库,但在本地 JVM/unit 测试中没有。
如果您想在本地 JVM/unit 测试中使用 AndroidJUnit4
,请改用下面的依赖项
testImplementation 'androidx.test.ext:junit:1.1.1'
如果你在插桩测试中使用 AndroidJUnit4
时添加后者的依赖,同样的事情也适用,你也会得到 Cannot resolve symbol 'AndroidJUnit4'
;因为你使用了错误的指令。
当我遵循 Google IOSched 应用并使用三种构建类型 [debug,release,staging] 设置我的项目时,我发生了同样的错误,其中 debug 和 release 共享相同的源目录
sourceSets {
debug.java.srcDir 'src/debugRelease/java'
release.java.srcDir 'src/debugRelease/java'
}
在这种情况下,在模块级 build.gradle 文件中指定 testBuildType
,项目现在应该能够解析符号 'AndroidJUnit4'。
...
sourceSets {
debug.java.srcDir 'src/debugRelease/java'
release.java.srcDir 'src/debugRelease/java'
}
testBuildType "staging"
...
参考:https://github.com/google/iosched/blob/master/mobile/build.gradle
确保你有
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
您的应用 build.gradle 文件中的依赖项。误删了,测试停止了。