Java 为什么库模块 android.support.test 在添加依赖项中不可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29687897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is library module android.support.test not visible in add dependency
提问by user2624395
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:
我正在将 Espresso 添加到我在 Android Studio 中的项目中。我已经安装了 Support Repository,实际上已经在使用它的一部分。然后我app/build.gradle
按照安装说明添加了这些依赖项:
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
所以我从 build.gradle 中删除了上面两行,并尝试在 GUI 项目结构/模块/依赖项中添加
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.
'com.android.support.test.espresso:espresso-core:2.0' 和 'com.android.support.test:testing-support-lib:0.1' 都没有出现作为可供选择的选项。但是,在我的文件系统中有<sdk>\extras\android\m2repository\com\android\support\test\espresso\espresso-core\2.0\
完整的文件,包括espresso-core-2.0.aar
我可以通过 winzip 打开和导航的文件。在文件系统中,它看起来与通过带有 Support Repository 的 SDK Manager 安装的其他库没有什么不同。
Why doesn't android studio recognize this library?
为什么android studio不识别这个库?
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?
非常感谢您的帮助,我找不到其他人似乎遇到了这个问题。这是我能找到的最接近的:为什么编译时库模块中的包不存在,即使 Android Studio 没有显示代码错误?
I have tried reinstalling Support Repository twice.
我曾尝试重新安装 Support Repository 两次。
回答by Pavel Dudka
There are 2 different test dependencies configurations:
有 2 种不同的测试依赖项配置:
testCompile
- used by unit test suite (located insrc/test
folder and invoked by./gradlew test
)androidTestCompile
- used by integration test suite (located atsrc/androidTest
folder and invoked by./gradlew connectedAndroidTest
).
testCompile
- 由单元测试套件使用(位于src/test
文件夹中并由 调用./gradlew test
)androidTestCompile
- 由集成测试套件使用(位于src/androidTest
文件夹并由 调用./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
在您的情况下,您的测试代码应该进入src/androidTest
文件夹,并且应该通过运行来执行测试套件./gradlew connectedAndroidTest
回答by ebelli
I had the same issue and I found that the dependencies with the androidTestCompile are visible only by default in the debug build variant.
我遇到了同样的问题,我发现 androidTestCompile 的依赖项仅在调试构建变体中默认可见。
You can change the build variant to be tested by adding this to your build.gradle:
您可以通过将其添加到 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
这里有更多信息https://code.google.com/p/android/issues/detail?id=98326
回答by Poptart
I've had the same problem and it was solved by hitting Clean Project from the Build tab in Android Studio.
我遇到了同样的问题,通过从 Android Studio 的 Build 选项卡点击 Clean Project 解决了这个问题。
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.
点击 Clean Project 后,观察 Gradle 控制台是否有潜在的错误,如果它成功完成了清理,只需进入任何一个测试类并输入“Espresso”,智能代码完成就会提供建议。之后,当您使用 Espresso 时,所有内容都应自动导入。
Hope this helps!
希望这可以帮助!
回答by zai chang
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:
我也有这个问题,我在 src/androidTests 下有我的 android 测试用例,按照谷歌的建议,但这导致 build.gradle 出现问题:
sourceSets {
main {
java.srcDirs = ['src']
}
}
With the above it's trying to compile all my test cases within the normal debugcompilation target, which does not include the espresso and other dependencies because they're listed under androidTestCompile.
通过上面的内容,它尝试在正常的调试编译目标中编译我的所有测试用例,其中不包括 espresso 和其他依赖项,因为它们列在androidTestCompile下。
In the end I fixed this by excluding the androidTest subdirectory from compilation and set the root of androidTest to the appropriate directory.
最后,我通过从编译中排除 androidTest 子目录并将 androidTest 的根目录设置为适当的目录来解决此问题。
sourceSets {
main {
java.srcDirs = ['src']
java.excludes = ['androidTest/**']
}
androidTest.setRoot('src/androidTest')
}
回答by Eefret
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
.
我的解决方案更简单更简单,我刚刚去了 Android StudioFile>Invalidate Caches/Restart
并且它工作正常,似乎 android studio 保留了一些不会用Rebuild/Clean
.
回答by Louis Parkin
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:
我在将我的一个应用程序向前移植到新的视觉范式时遇到了这个问题,并发现我的应用程序级 build.gradle 缺少以下内容:
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
回答by user7406268
1.Click the drop down menu beside the Run button in the toolbar.
1. 单击工具栏中运行按钮旁边的下拉菜单。
2.Click Edit configuration
2.点击编辑配置
3.Now remove all others except app(within Android App) and the Default one.
3.现在删除除应用程序(在Android应用程序内)和默认应用程序之外的所有其他应用程序。
This worked for me. Hope it helps.
这对我有用。希望能帮助到你。