用于Espresso测试的Android Studio项目设置 [英] Android Studio project setup for Espresso tests
问题描述
大约一周前,我问了这个问题
About a week ago I asked this question
为什么是库模块android .support.test在添加依赖项中不可见
经过大量的抓挠和项目设置变形之后,我发现此app/build.gradle依赖项配置使我比以往任何时候都受益匪浅,至少可以编译项目,但在尝试运行时无法编译:
After much head scratching and project setup morphing I discovered that this app/build.gradle dependency configuration gets me so much further than ever before in that at least the project compiles but does not build when attempting to run:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion '22.0.1'
defaultConfig {
applicationId "com.my.package.name"
minSdkVersion 8
targetSdkVersion 22
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets { main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } }
packagingOptions {
exclude 'LICENSE.txt'
}
}
dependencies {
compile 'com.android.support:appcompat-v7:22.1.0'
compile 'com.android.support:support-v4:22.1.0'
compile 'com.android.support.test.espresso:espresso-core:2.1'
compile 'com.android.support.test:testing-support-lib:0.1'
}
关于测试支持依赖项的
通知compile
而不是androidTestCompile
.使用androidTestCompile
会导致上面我在前面的问题中指出的问题.
Notice compile
instead of androidTestCompile
on the testing support dependencies. Using androidTestCompile
results in the problems noted in my previous question identified above.
还要注意sourceSets声明中的两个srcDirs.当我手动创建androidTest/java目录时,该声明会自动设置.应该以不同的方式声明吗?
Also notice the two srcDirs in the sourceSets declaration. That declaration was automatically set when I manually created the androidTest/java directory. Should this be declared differently?
同样,ide中的getDefaultProguardFile
也用工具提示无法解析符号'getDefaultProguardFile'"加下划线.万一这很关键.
Also too, getDefaultProguardFile
is underlined in the ide with the tool tip "cannot resolve symbol 'getDefaultProguardFile'". In case that is crucial.
这个新的启示将androidTestCompile
切换为compile
,使我相信我为单元测试设置项目结构的方式存在问题.这是我的项目结构的图片:
This new revelation, switching androidTestCompile
to compile
, leads me to believe there is something wrong with the way I set up the project structure for the unit test. Here is a picture of my project structure:
我应该提到这个项目/模块结构是从Eclipse迁移时自动创建的.通过图标修饰,看起来至少可以将我的测试班识别为测试班.
I should mention that this project/module structure was automatically created when migrating from Eclipse. By the icon decorations it looks like it at least recognizes my test class as a test class.
但是现在我被这个错误困住了:
But now I am stuck at this error:
错误:任务':mymodule:dexDebug'的执行失败. com.android.ide.common.internal.LoggedErrorException:无法运行命令: C:\ Users \ myuser \ AppData \ Local \ Android \ sdk \ build-tools \ 22.0.1 \ dx.bat --dex --no-optimize --output C:\ Users \ myuser \ AndroidstudioProjects \ myproject \ mymodule \ build \ intermediates \ dex \ debug --input-list = C:\ Users \ myuser \ AndroidstudioProjects \ myproject \ mymodule \ build \ intermediates \ tmp \ dex \ debug \ inputList.txt 错误代码: 2个 输出: 超出预期的最高级别: com.android.dex.DexException:多个dex文件定义了Landroid/support/test/BuildConfig; 在com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596) 在com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554) 在com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535) 在com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) 在com.android.dx.merge.DexMerger.merge(DexMerger.java:189) 在com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) 在com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) 在com.android.dx.command.dexer.Main.run(Main.java:246) 在com.android.dx.command.dexer.Main.main(Main.java:215) 在com.android.dx.command.Main.main(Main.java:106)
Error:Execution failed for task ':mymodule:dexDebug'. com.android.ide.common.internal.LoggedErrorException: Failed to run command: C:\Users\myuser\AppData\Local\Android\sdk\build-tools\22.0.1\dx.bat --dex --no-optimize --output C:\Users\myuser\AndroidstudioProjects\myproject\mymodule\build\intermediates\dex\debug --input-list=C:\Users\myuser\AndroidstudioProjects\myproject\mymodule\build\intermediates\tmp\dex\debug\inputList.txt Error Code: 2 Output: UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Landroid/support/test/BuildConfig; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) at com.android.dx.command.dexer.Main.run(Main.java:246) at com.android.dx.command.dexer.Main.main(Main.java:215) at com.android.dx.command.Main.main(Main.java:106)
由于在support.test库上使用了compile,因此不确定这是来自我的项目设置还是来自库.
Not sure if this is coming from my project setup or if it is coming from the libraries due to using compile on the support.test libraries.
对于为什么androidTestCompile
不起作用的任何建议,我们深表感谢.
Any advice on why androidTestCompile
doesn't work is appreciated.
我只是在两天前就将SDK Manager的所有最新更新都更新到了22.0.1,但无济于事.
Also I just 2 days ago took all the latest updates from SDK Manager to 22.0.1 to no avail.
我注意到的另一件事是,我似乎发现唯一一个包含com.google.common.*软件包的jar位于espresso库中. (com.google.common.*是测试库的依赖项.)但是,当我使用Winzip检查espresso库jar时,所有com.google.common.*包文件夹结构都存在,但其中没有.class文件软件包文件夹或子文件夹.只是提到,以防万一它提供了一个线索.也许我也错过了其他地方.
Another thing I noticed is the only jar I seem to find containing package com.google.common.* is in the espresso library. (com.google.common.* is a dependency of the testing libraries.) But when I inspect the espresso library jar with winzip all of the com.google.common.* package folder structure is there but there are no .class files in the package folders nor subfolders. Just mentioning that in case it lends a clue. Perhaps it is somewhere else also that I had missed.
谢谢!
我的源集现在看起来像这样
my source sets now look like this
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
}
和依赖项
dependencies {
compile 'com.android.support:appcompat-v7:22.1.0'
compile 'com.android.support:support-v4:22.1.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
在测试类中我仍然无法解析android.support.test
(这些行上的test
被涂成红色):
I still get can't resolve android.support.test
in my test class (test
is colored red on these lines):
import android.support.test.InstrumentationRegistry;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
我的外部库中是否看不到测试支持,espresso-core和espresso-contrib?
shouldn't I see a testing-support, espresso-core and espresso-contrib in my External libraries?
推荐答案
下面是我在 Android Studio 1.1.0 + Espresso 2.0 +支持库v11 + gradle 环境下运行的Espresso gradle设置.您似乎错过了androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
库.
Below is my working Espresso gradle setup based on Android Studio 1.1.0 + Espresso 2.0 + Support Library v11 + gradle environment. It looks likely that you missed androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
library.
dependencies {
// ...
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
android {
defaultConfig {
minSdkVersion 10
targetSdkVersion 21
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets {
packagingOptions {
//...
exclude 'META-INF/LICENSE.txt'
exclude 'LICENSE.txt'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src/main/java']
res.srcDirs = ['res']
}
}
}
关于目录,是的.只需维护src
文件夹下的androidTest
和main
.但是,您的设置不会使IDE识别androidTest
文件夹为测试项目文件夹.为此,我认为两个具有相同程序包名称的项目会导致多dex错误.使用androidTestCompile
可以在gradle依赖项中清理与咖啡相关的行,并清理/重建项目.
About the directory, yes. just maintain androidTest
and main
under src
folder. But, your setting does not make IDE to recognize androidTest
folder as a test project folder. For that, two projects with same package name causes multi dex error I think. Use androidTestCompile
to espresso-related lines in gradle dependencies and clean/rebuild project.
请注意,您必须将所有引用的项目设置为同一testInstrumentationRunner.
Note that you have to set all referenced projects gradle to same testInstrumentationRunner.
I also spent times to make it work. I wrote Korean tutorial to setup and run Espresso. Sorry about the language but screenshots might be helpful or you can try Google translate to read it.
编辑.附加在下面
我想过为什么您的IDE在androidTest
文件夹中不显示绿色(表示测试项目),并发现gradle设置似乎是错误的.
I thought why your IDE did not show green color in androidTest
folder (means Test Project), and found your gradle setup seems to be wrong.
线
{ main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } }
使IDE识别androidTest
是 Project文件夹,而不是 Test Project 文件夹.删除第二个. androidTest
将被自动 识别为测试项目.
The line
{ main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } }
make IDE to recognize androidTest
is a Project folder not Test Project folder. remove second one. androidTest
will be automatically recognized as a Test Project.
这篇关于用于Espresso测试的Android Studio项目设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!