如何将 Espresso UI 测试编译成一个版本?

How can I compile Espresso UI Tests into a release?

我目前正在尝试将 Espresso 测试添加到 release APK 中。在我们的例子中,我们必须测试 实际 被发布的内容,因此 UI 测试必须包含在发布中。

所以我的主要问题是:如何最好地完成这项工作?到目前为止,我在互联网上的研究没有给我任何结果。

我已经尝试将构建脚本中的 androidDebugTest 更改为 compile:

compile 'com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
}

还有这里:

compile 'com.android.support.test.espresso:espresso-contrib:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'design'
    exclude group: 'com.android.support', module: 'recyclerview-v7'
}

但调用后

./gradlew assembleRelease --info --debug

我收到此错误消息:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithJarMergingForRelease'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: javax/annotation/Generated.class
...
BUILD FAILED

此外,通常有两个 gradle 任务 assembleDebugassembleDebugAndroidTest 与 Espresso 一起工作。但是,由于没有 assembleReleaseAndroidTest 任务,发布版本在做什么?

提前感谢您的建议!

您无法将测试编译到应用 APK 中,但您可以针对发布构建类型进行测试。在您的情况下,您的发布工件将包含两个 APK,即常规应用 APK 和测试 APK。参见 。