Daniele Vitali
Daniele Vitali

Reputation: 3858

Android Studio not recognizes Espresso imports

I am trying to write a test case using Espresso.

I am using Android Studio 1.5.1 (Stable channel), Gradle plugin 1.5, Gradle 2.7. The problem is that Android Studio doesn't recognize any import related to Espresso (and not only)enter image description here

So, I tried to clean the project, rebuild, invalidate cache and restart, but nothing.

I added these dependencies in my app module:

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'

I added the runner in defaultConfig:

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

I created the test suite class under androidTest folder:

enter image description here

What am I doing wrong please?

UPDATE

Here is (part) of my 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'
}

Upvotes: 7

Views: 4712

Answers (6)

Mario Tp
Mario Tp

Reputation: 83

I had the same problem. Here is how i fixed it:

  1. Go the src directory of your project in finder/explorer, select the directory containing your test.
  2. Rename the directory with the prefix "androidTest" followed by the name of the flavor you are trying to test.
  3. Open Android Studio, Invalidate Caches and Restart.

Upvotes: 0

BoshRa
BoshRa

Reputation: 788

Perhaps your Build Variant is on "release" mode. you have to change it to debug. enter image description here

Upvotes: 7

Artur Latoszewski
Artur Latoszewski

Reputation: 793

UPDATE 30/11/2016

Just like @Jaymes Bearden said in comment below.

Use testBuildType. Android Studio 2.2, gradle 2.2.2

android {
        testBuildType "yourBuildType"
}

OLD ANSWER

I found not solution but some source of problem. In my project I have, a lot of BuildTypes. Especially more than one debug build.

Everything works only in default debug build type. I think that this is some internal Android Studio error.

My AS version - 2.1

Upvotes: 5

Bingo
Bingo

Reputation: 279

You need to use debug build variant. With other build options espresso will not be recongnised.

Upvotes: 3

piotrek1543
piotrek1543

Reputation: 19361

I'm pretty sure, that you're something missing in your configuration. compare your build.gradle with mine below

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"

}

Also choose File -> Invalidate cache/restart and try first option.

Hope it help

Upvotes: 2

sunlover3
sunlover3

Reputation: 2049

In Android Studio:

Open Run menu -> Edit Configurations - Add a new Android Tests configuration - Choose a module Add a specific instrumentation runner:

android.support.test.runner.AndroidJUnitRunner

Also try to update the Android Support Library from SDK Manager.

Hope this helped. You can also try this topic: link

Good luck!

Upvotes: 0

Related Questions