26

I am adding Espresso to my project in Android Studio. I have installed the Support Repository and in fact have already been using pieces of it. Then I added these dependencies to app/build.gradle per the install instructions:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'

in writing my test, auto complete recognizes the existence of the artifacts. But when I run I get this error:

error: package android.support.test does not exist
error: package org.junit does not exist

and a number of other subpackages to these two.

So I removed the two above lines from build.gradle and attempted to add then in the GUI project structure/modules/dependencies

neither 'com.android.support.test.espresso:espresso-core:2.0' nor 'com.android.support.test:testing-support-lib:0.1' appear as options to choose from. However, in my file system there is <sdk>\extras\android\m2repository\com\android\support\test\espresso\espresso-core\2.0\ with the full complement of files including espresso-core-2.0.aar which I am able to open and navigate within it via winzip. In the file system it looks no different than the other libraries installed via SDK Manager with Support Repository.

Why doesn't android studio recognize this library?

Your help is greatly appreciated, no one else that I can find seems to have run into this problem. This is the closest I could find: Why do packages from library module does not exist upon compilation, even when Android Studio shows no errors in code?

I have tried reinstalling Support Repository twice.

Community
  • 1
  • 1
user2624395
  • 432
  • 1
  • 5
  • 10
  • 1
    answered here: http://stackoverflow.com/questions/29857695/android-tests-build-error-multiple-dex-files-define-landroid-support-test-build for my build, the 5/30/2015 version of the answer did the trick – user2624395 Jun 05 '15 at 02:30
  • Can you give the exact path of the class file, which exhibits the `error: package android.support.test does not exist` ? – KarolDepka Jan 14 '17 at 18:31
  • In my case, it was the problem with the release build. I have switched into debug and it was fine, but I still do not why... – Arkadiusz Cieśliński Jun 15 '17 at 10:47

7 Answers7

37

I had the same issue and I found that the dependencies with the androidTestCompile are visible only by default in the debug build variant.

You can change the build variant to be tested by adding this to your build.gradle:

android {
    ...
    testBuildType "staging"
}

where "staging" is just an example, you should replace it with one of your build variant.

Remember that only one variant is tested.

More information here https://code.google.com/p/android/issues/detail?id=98326

ebelli
  • 426
  • 3
  • 7
  • 1
    thanks for finding that I tried placing `testBuildType "staging"` in two difference places inside `android {...` and I get this error "Error:Test Build Type 'staging' does not exist." any thoughts on what else I might do different? – user2624395 Jun 04 '15 at 18:59
  • "staging" is just an example, you should replace it with one of your build variant. I've edited the answer to be more clear. – ebelli Jun 04 '15 at 21:25
  • Did it work for your project? Can my answer be accepted? – ebelli Jul 16 '15 at 14:47
  • That did not work for my project. Please see my comment under the question. It indicates what helped. Someone closed this question so I'm not able to answer it. I was only able to add a comment. – user2624395 Jul 21 '15 at 13:57
  • 1
    This answer should be marked as correct answer. I was looking for it a long time. @ebelli you help me a lot. Thanks – Weverton Peron Dec 17 '18 at 13:55
  • Thank you so much, this was my issue too. Annoying!! – Paul Aldred-Bann May 14 '19 at 08:19
12

There are 2 different test dependencies configurations:

  • testCompile - used by unit test suite (located in src/test folder and invoked by ./gradlew test)
  • androidTestCompile - used by integration test suite (located at src/androidTest folder and invoked by ./gradlew connectedAndroidTest).

My suspicion is that your test code is in the wrong test suite location

In your case your test code should go into src/androidTest folder and test suite should be executed by running ./gradlew connectedAndroidTest

Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83
  • My test code is in src/androidTest/java/. I added the package location by right clicking src and selected new/folder/Java Folder. added my package and created the source file. Is it possible I created the test source location incorrectly? – user2624395 Apr 17 '15 at 00:36
  • 1
    well, it sounds correct to me. should work. The only thing I can suggest - is to look to some other project where such configuration works and compare to your project to see the difference. Here is an example - https://github.com/chiuki/android-test-demo – Pavel Dudka Apr 17 '15 at 00:43
  • It's definitely my project, the samples from github execute successfully so that rules out my Android Studio environment. Though not yet able to discern what is missing from my project. Hypothesis: something to do with the conversion from Eclipse to Android Studio. – user2624395 Apr 28 '15 at 01:51
12

I had this issue as well and I have my android test cases under src/androidTests as recommended by Google, but this caused problems with build.gradle:

sourceSets {
    main {
        java.srcDirs = ['src']
    }
}

With the above it's trying to compile all my test cases within the normal debug compilation target, which does not include the espresso and other dependencies because they're listed under androidTestCompile.

In the end I fixed this by excluding the androidTest subdirectory from compilation and set the root of androidTest to the appropriate directory.

sourceSets {
    main {
        java.srcDirs = ['src']
        java.excludes = ['androidTest/**']
    }
    androidTest.setRoot('src/androidTest')
}
zai chang
  • 699
  • 9
  • 13
  • 3
    you sir, are a HERO ! – Noxymon Feb 22 '16 at 16:01
  • this seems to be the right approach, but in my case java.srcDir was under 'src/main/java' and java.excludes = ['src/androidTest/**''] – Stoycho Andreev Dec 09 '16 at 08:10
  • @zai-chang : Not sure, if it matters, but in the first sentence is mentioned `src/androidTests`, where it should be singular `Test`. Second, do you have your java test sources under `src/androidTest` or `src/androidTest/java`? Btw, Is that You from Bcn? Cheers, --Karol – KarolDepka Jan 14 '17 at 18:26
10

I've had the same problem and it was solved by hitting Clean Project from the Build tab in Android Studio.

After hitting Clean Project, watch the Gradle Console for potential errors and if it completes the cleaning successfully, simply go into any one of your test classes and type in "Espresso" and the smart code completion should offer suggestions. Everything should automatically import as you use Espresso after that.

Hope this helps!

Poptart
  • 331
  • 4
  • 16
6

My solution was easier and simpler, I just went to Android Studio File>Invalidate Caches/Restart and it worked properly, seems that android studio is keeping some cache that will not clean with Rebuild/Clean.

Eefret
  • 4,724
  • 4
  • 30
  • 46
0

I encountered this issue while forward-porting one of my apps into a new visual paradigm, and found my app-level build.gradle was missing the following:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
Louis Parkin
  • 800
  • 7
  • 15
0

1.Click the drop down menu beside the Run button in the toolbar.

2.Click Edit configuration

3.Now remove all others except app(within Android App) and the Default one.

This worked for me. Hope it helps.