Android Espresso:无法parsing符号AndroidJUnit4.class

我正在尝试在新的Android项目中创buildEspresso UItesting,但是我面临以下问题。

如果我试图创build一个空的testing类:

import android.content.Intent; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import android.test.ActivityInstrumentationTestCase2; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; @RunWith(AndroidJUnit4.class) public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> { } 

我总是得到这个错误消息:

 cannot resolve symbol AndroidJUnit4.class 

几乎所有的导入库都被标记​​为未使用。

build.gradle文件包含以下内容:

 apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.0" defaultConfig { applicationId "com.some.thing.xxx" minSdkVersion 14 targetSdkVersion 23 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } lintOptions { abortOnError false } packagingOptions { exclude 'LICENSE.txt' } } repositories { mavenCentral() maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } maven { url "https://jitpack.io" } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.0' compile 'com.google.android.gms:play-services:7.8.0' compile 'com.mcxiaoke.volley:library:1.0.18' compile 'com.orhanobut:logger:1.11' // App dependencies compile 'com.android.support:support-annotations:23.0.0' // TESTING DEPENDENCIES androidTestCompile 'com.android.support.test:runner:0.3' // Set this dependency to use JUnit 4 rules androidTestCompile 'com.android.support.test:rules:0.3' // Set this dependency to build and run Espresso tests androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2' // add this for intent mocking support androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2' // add this for webview testing support androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2' // Set this dependency to build and run UI Automator tests androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2' } 

如果我把这些设置放在我的另一个testing项目上,那么我就不知道什么是错的?

我按照这个教程:“

http://www.vogella.com/tutorials/AndroidTestingEspresso/article.html

我试图通过以下问题来解决它: 无法parsing符号'AndroidJUnit4'

但没有运气。

非常感谢您的任何build议。

Solutions Collecting From Web of "Android Espresso:无法parsing符号AndroidJUnit4.class"

我也尝试过来自于vogella的同一个教程,遇到了很多问题。 我遇到的第一个问题是v23库的注释版本和Espresso库之间的依赖冲突。

然后,我发现另一个最近更新的教程,从罗杰胡“ 使用浓咖啡的UItesting ”。 我注意到一个说法,咖啡不支持棉花糖呢。

依赖项被添加如下:

 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') { // Necessary if your app targets Marshmallow (since Espresso // hasn't moved to Marshmallow yet) exclude group: 'com.android.support', module: 'support-annotations' } androidTestCompile('com.android.support.test:runner:0.3') { // Necessary if your app targets Marshmallow (since the test runner // hasn't moved to Marshmallow yet) exclude group: 'com.android.support', module: 'support-annotations' } 

这解决了我的依赖冲突,我没有看到其他问题的发生。

我通过手动导入以下内容解决了问题,我认为应该自动导入,但是没有:

 import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard; import static android.support.test.espresso.action.ViewActions.typeText; import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; 

我用常数来解决它

 minSdkVersion 

build.gradle文件中的版本18。

以下gradle.file正在工作:

 apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.0" defaultConfig { applicationId "com.something.xxx" minSdkVersion 18 targetSdkVersion 23 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } lintOptions { abortOnError false } packagingOptions { exclude 'LICENSE.txt' } } repositories { mavenCentral() maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } maven { url "https://jitpack.io" } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.0' compile 'com.google.android.gms:play-services:7.8.0' compile 'com.mcxiaoke.volley:library:1.0.18' compile 'com.orhanobut:logger:1.11' // TESTING DEPENDENCIES androidTestCompile 'com.android.support:support-annotations:23.0.0' androidTestCompile 'com.android.support.test:runner:0.3' // Set this dependency to use JUnit 4 rules androidTestCompile 'com.android.support.test:rules:0.3' // Set this dependency to build and run Espresso tests androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2' // add this for intent mocking support androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2' // add this for webview testing support androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2' // Set this dependency to build and run UI Automator tests androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2' } 

根据上面给出的gradle变更:

 androidTestCompile 'com.android.support.test:runner:0.3' 

你需要改变

 androidTestCompile('com.android.support.test:runner:0.3') { exclude group: 'com.android.support', module: 'support-annotations' } 

对我来说,即使有上面​​的改变也不行,所以我注意到我错过了下面的内容:

 androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' 

对我来说工作得很好

完整的build.gradle可以find如下:

  apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "21.1.2" lintOptions { // IMPORTANT: We are disabling this rule to avoid build errors on PrettyTime. Although //pretty time references an InvalidPackage it does not do it in the code sections we use //given how easy this library is to use I would prefer not to replace it with something //like Joda-Time which is overkill for such a small section of the app. disable 'InvalidPackage' } packagingOptions { exclude 'LICENSE.txt' } defaultConfig { applicationId "co.test.dialer" minSdkVersion 18 targetSdkVersion 22 versionCode 15 versionName "0.6.15." renderscriptTargetApi 22 renderscriptSupportModeEnabled true testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } signingConfigs { production { storeFile file("keystore.jks") storePassword "hello" keyAlias "production" keyPassword "android" } debug { storeFile file("keystore.jks") storePassword "hello" keyAlias "debug" keyPassword "android" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.production } debug { minifyEnabled false debuggable true applicationIdSuffix ".debug" signingConfig signingConfigs.debug } internal_test { minifyEnabled false debuggable true applicationIdSuffix ".test" signingConfig signingConfigs.debug } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:support-v13:23.0.1' compile 'com.android.support:cardview-v7:23.0.1' compile 'com.android.support:design:23.0.1' compile 'com.android.support:recyclerview-v7:23.0.1' compile 'com.google.android.gms:play-services-gcm:8.1.0' compile 'com.jakewharton:butterknife:6.1.0' compile 'com.afollestad:material-dialogs:0.7.8.0' compile 'com.googlecode.libphonenumber:libphonenumber:3.1' compile 'com.mcxiaoke.volley:library:1.0.15' compile 'squizbit.com.jsonobjectified:jetjson:1.0.3@aar' compile 'com.google.android.gms:play-services-analytics:8.1.0' releaseCompile 'co.test.dialersdk:dialersdk:1.0.8@aar'; debugCompile 'co.test.dialersdk:dialersdk-debug:1.0.8@aar'; internal_testCompile 'co.test.dialersdk:dialersdk-internal_test:1.0.8@aar'; androidTestCompile('com.android.support.test:runner:0.3') { exclude group: 'com.android.support', module: 'support-annotations' } androidTestCompile('com.android.support.test:rules:0.3') { exclude group: 'com.android.support', module: 'support-annotations' } androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') { exclude group: 'com.android.support', module: 'support-annotations' } androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2') { exclude group: 'com.android.support', module: 'support-annotations' } androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') { exclude group: 'com.android.support', module: 'support-annotations' exclude group: 'com.android.support', module: 'appcompat' exclude group: 'com.android.support', module: 'support-v4' exclude module: 'recyclerview-v7' } androidTestCompile('com.android.support.test.espresso:espresso-web:2.2') { exclude group: 'com.android.support', module: 'support-annotations' } androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' } 

希望这肯定能够帮助别人,因为即使在完成了vogella教程的全部步骤之后,我也一直在努力修复它。

我有同样的问题,我解决了改变我的构build变种。 我在发布版本中运行testing。

当我改变“debugging”,它的作品

你可以参考这个答案 。

“我犯了错误,把testing类放在src / test,把它们移到src / androidTest / java / …之后,依赖已经解决了,也许这也是你的问题。

您得到该错误消息的原因可能是因为您的文件夹所在的testing位置与规范不匹配。 该文件夹必须是src / androidTest / java

看看这篇文章说…

运行仪器化unit testing要运行仪器化testing,请按照下列步骤操作:

通过单击工具栏中的“同步项目”,确保您的项目与Gradle同步。 以下列方式之一运行testing:要运行单个testing,请打开“项目”窗口,然后右键单击testing,然后单击“运行”。 要testing类中的所有方法,请右键单击testing文件中的类或方法,然后单击运行。 要在目录中运行所有testing,请右键单击该目录并select运行testing。 Gradle的Android插件编译位于默认目录(src / androidTest / java /)中的testing代码,构buildtestingAPK和生产APK,在连接的设备或模拟器上安装这两个APK,并运行testing。 Android Studio然后在“运行”窗口中显示检测到的testing执行结果。

所以乡亲们,对于仪器testing的文件夹一定是(别忘了这个情况)

SRC / androidTest / JAVA

对于本地testing文件夹必须是

的src /testing/ JAVA

然后,您可以使您的软件包文件夹与您的应用程序软件包匹配

希望这有助于社区!

也许你可能有多个构buildtypes,默认Android项目创build两个构buildtypes(debugging/发布),更改构build变体debugging或设置下面的值

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing

 Currently only one Build Type is tested. By default it is the debug Build Type, but this can be reconfigured with: android { ... testBuildType "staging" }