Android Studio does not recognize the import of espresso

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 does not recognize imports related to Espresso (and not only) enter image description here

So, I tried to clear the project, rebuild, invalidate the cache and reload, but nothing.

I added these dependencies to my application 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 runner to defaultConfig:

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

I created a test suite class in the androidTest folder:

enter image description here

What am I doing wrong?

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' } 
+7
android android-espresso
source share
5 answers

UPDATE 11/30/2016

Just like @Jaymes Bearden said in a comment below.

Use testBuildType. Android Studio 2.2, gradle 2.2.2

 android { testBuildType "yourBuildType" } 

OLD ANSWER

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

Everything works only by default debug . I think this is some internal error of Android Studio.

My AS version is 2.1

+4
source share

You need to use the debug build option. In other versions, the assembly of espresso will not be reconstructed.

+2
source share

Your build option may be in release mode. You must change it for debugging. enter image description here

+2
source share

I am sure that you have 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 select File -> Invalidate cache/restart and try the first option.

Hope this helps

+1
source share

In Android Studio:

Open the Run menu โ†’ โ€œChange Configurationsโ€ - Add a New Android Tests Configuration - Select a Module Add a Special Measuring Instrument:

android.support.test.runner.AndroidJUnitRunner

Also try updating your Android support library from the SDK manager.

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

Good luck

0
source share

All Articles