Android 仪器测试和库项目

标签 android android-studio android-gradle-plugin android-testing

我有一个图书馆项目和一些主要项目,这取决于那个图书馆项目。

我想写主项目的测试,但是测试的常用函数应该放在库项目中,减少代码重复。

我在库的 build.gradle 中添加了以下几行:

dependencies {
    compile Dependencies.appCompat
    compile Dependencies.recyclerView
    compile Dependencies.cardView
    compile Dependencies.playAds
    compile Dependencies.googleAnalytics

    compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
        transitive = true
    }

    // Unit testing dependencies
    androidTestCompile 'junit:junit:4.12'
    // Set this dependency if you want to use the Hamcrest matcher library
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'com.android.support.test:runner:+'
    androidTestCompile 'com.android.support.test:rules:+'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile 'org.mockito:mockito-core:1.+'
    androidTestCompile "com.google.dexmaker:dexmaker:1.2"
    androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}

在主要的 build.gradle 中有:

dependencies {
    compile project(':library')
}

但是在主项目的测试中我得到了以下错误: 错误:包 android.support.test.espresso 不存在以下行

import android.support.test.espresso.UiController;

但是 espresso 是在库项目中导入的。看起来,该库项目未编译为 android 测试。

这个问题可以解决吗?

最佳答案

您应该尝试为通用测试代码创建一个单独的库。否则,您将在应用程序 apk 中包含测试代码。

新建一个测试库(即“test-library”),将常用代码放入其中。然后在app目录的“build.gradle”中创建如下内容。

dependencies {
    compile project(':library')
    androidTestCompile(':test-library')
}

关于Android 仪器测试和库项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35521382/

相关文章:

performance - Android Studio占用内存过多

android - 如何在将库发布到 android 中的 bintray 时包含依赖项

android - gradle 可以处理无限的子项目深度吗?

android - Gradle 构建有一些错误

android - HttpURLConnection 发送带参数的图像、音频和视频文件可能(String 或 Json String)Android

android - 在 headless (headless) linux 机器上运行 android 模拟器

c# - Xamarin 自定义键盘

java - 线程在后台使用 JobService 时停止运行

java - 如何扫描所有 IP 地址范围?

Android Studio gradle 构建错误 : 'The first result of the daemon was empty.Most likely the process died immediately after connection'