【问题标题】:Android Studio not recognizes Espresso importsAndroid Studio 无法识别 Espresso 导入
【发布时间】:2016-02-18 16:05:18
【问题描述】:

我正在尝试使用 Espresso 编写测试用例。

我正在使用 Android Studio 1.5.1(稳定通道)、Gradle 插件 1.5、Gradle 2.7。 问题是 Android Studio 无法识别与 Espresso 相关的任何导入(不仅如此)

所以,我尝试清理项目,重建,使缓存无效并重新启动,但没有。

我在我的应用模块中添加了这些依赖项:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'

我在 defaultConfig 中添加了跑步者:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

我在 androidTest 文件夹下创建了测试套件类:

请问我做错了什么?

更新

这是我的 build.gradle 的(部分):

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 5
        versionName '1.4'

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {
        //my build type configs
    }

    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    dexOptions {
        incremental true
        preDexLibraries = false
        jumboMode = false
        javaMaxHeapSize "2g"
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.viewpagerindicator:library:2.4.1'
    compile project(':libraries:RITracking')
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:support-annotations:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'com.android.support:gridlayout-v7:23.1.1'
    compile 'com.google.code.gson:gson:2.5'
    compile 'com.google.android.gms:play-services-plus:8.4.0'
    compile 'com.google.android.gms:play-services-base:8.4.0'
    compile 'de.greenrobot:eventbus:2.4.1'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.okhttp:okhttp:2.7.2'
    compile 'com.facebook.android:facebook-android-sdk:4.9.0'
    compile 'com.cocosw:bottomsheet:1.2.0@aar'                          //Bottom Sheet that implement material design used for ShareDialog
    compile 'com.github.bumptech.glide:glide:3.6.1'                     //Glide library
    compile 'com.googlecode.libphonenumber:libphonenumber:7.2.3'        //Library used to parse/merge phones number to E164 format
    compile 'me.leolin:ShortcutBadger:1.1.3@aar'                        //Used to show badge on application icon, library is optimized to work on most of devices
    compile 'com.stripe:stripe-android:1.0.3'                              //Stripe payment gateway, used to integrate credit card payment
    provided 'org.projectlombok:lombok:1.16.6'
    apt "org.projectlombok:lombok:1.16.6"
    compile 'com.jakewharton:butterknife:7.0.1'
    apt "com.jakewharton:butterknife:7.0.1"
    compile 'de.greenrobot:greendao:2.1.0'                              //Green Dao library is ORM implementation for Android SQL lite
    compile files('libs/libammsdk.jar')
    compile files('libs/apptimize-android-2.9.1.jar')

    testCompile 'junit:junit:4.12'

    androidTestCompile "com.android.support:support-annotations:23.1.1"
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
}

【问题讨论】:

    标签: android android-espresso


    【解决方案1】:

    也许您的 Build Variant 处于“发布”模式。您必须将其更改为调试。

    【讨论】:

      【解决方案2】:

      2016 年 11 月 30 日更新

      就像@Jaymes Bearden 在下面的评论中所说的那样。

      使用 testBuildType。 Android Studio 2.2,gradle 2.2.2

      android {
              testBuildType "yourBuildType"
      }
      

      老答案

      我没有找到解决方案,而是找到了一些问题的根源。在我的项目中,有很多 BuildTypes。尤其是不止一个调试版本。

      一切都只适用于默认的debug构建类型。我认为这是一些内部 Android Studio 错误。

      我的 AS 版本 - 2.1

      【讨论】:

      • 这也是我的问题。我有很多构建变体,但我选择了错误的一个。不过我找到了一个选项,您可以在应用程序的 gradle.build 中的 android 键下使用“testBuildType”选项。将其设置为您想要的任何变体。
      • @Artur 这个 testBuildType "yourBuildType" 是什么,我必须改变
      • 您将其更改为“buildTypes”下列出的任何您希望可测试的构建
      【解决方案3】:

      您需要使用调试构建变体。使用其他构建选项,espresso 将无法识别。

      【讨论】:

      • 或者如@Artur Latoszewski answer所述,明确声明您希望使用“testBuildType”调试哪些构建类型
      【解决方案4】:

      我很确定,您的配置中缺少一些东西。将您的build.gradle 与下面的进行比较

      def ASVersion = '23.1.1'
      
      buildscript {
          repositories {
              maven { url 'https://maven.fabric.io/public' }
          }
      
          dependencies {
              classpath 'io.fabric.tools:gradle:1.+'
          }
      }
      apply plugin: 'com.android.application'
      apply plugin: 'io.fabric'
      apply plugin: 'com.neenbedankt.android-apt'
      
      repositories {
          maven { url 'https://maven.fabric.io/public' }
      }
      
      
      android {
          dataBinding {
              enabled = true
          }
          compileSdkVersion 23
          buildToolsVersion '23.0.2'
      
          defaultConfig {
              applicationId "com.piotr.awesome"
              minSdkVersion 16
              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'
              }
          }
      }
      
      dependencies {
          compile fileTree(dir: 'libs', include: ['*.jar'])
          testCompile 'junit:junit:4.12'
      
          androidTestCompile "com.android.support:support-annotations:$ASVersion"
          androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
          androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
          androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
              exclude group: 'com.android.support', module: 'appcompat'
              exclude group: 'com.android.support', module: 'support-v4'
              exclude module: 'recyclerview-v7'
          }
          androidTestCompile 'com.android.support.test:runner:0.4.1'
          compile "com.android.support:appcompat-v7:$ASVersion"
          compile "com.android.support:support-v4:$ASVersion"
          compile "com.android.support:design:$ASVersion"
      
      }
      

      也选择File -> Invalidate cache/restart 并尝试第一个选项。

      希望对你有帮助

      【讨论】:

      • 感谢 piotrek 但仍然是同样的问题。我已经用我的 build.gradle 更新了这个问题。我可能想看看。
      • @DanieleVitali,问题解决了吗?
      • 不幸的是,我不再从事那个项目了。但是我可以在金丝雀频道上使用 AS 成功地在其他项目上运行 Expresso 测试。
      【解决方案5】:

      在 Android Studio 中:

      打开运行菜单 -> 编辑配置 - 添加新的 Android 测试配置 - 选择一个模块 添加特定的检测运行器:

      android.support.test.runner.AndroidJUnitRunner

      同时尝试从 SDK 管理器更新 Android 支持库。

      希望这会有所帮助。 你也可以试试这个话题:link

      祝你好运!

      【讨论】:

      • 已经尝试过了,它说 ut 找不到 AndroidJUnitRunner。支持库已更新。
      • 好的...您找到解决方案了吗?
      • 尚未解决。我已经用我的 build.gradle 更新了这个问题。
      • 我看到了你的更新,但我在你的代码中找不到这个: androidTestCompile 'org.hamcrest:hamcrest-library:1.3' 。你可以尝试添加它,看看会发生什么?
      • 我添加了它,但仍然存在同样的问题。无论如何,我认为这不是缺少依赖的问题。感觉更像是android studio无法识别导入或类似的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 2015-06-21
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多