简体   繁体   中英

Why is library module android.support.test not visible in add dependency

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.

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

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')
}

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

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!

我的解决方案更简单,更简单,我只是去Android Studio File>Invalidate Caches/Restart并且它工作正常,似乎android工作室保留了一些不会用Rebuild/Clean缓存。

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'
})

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM