如何将Android测试库与Android Studio中的应用程序库分开?

Joe*_*nde 5 android integration-testing gradle android-studio android-gradle-plugin

我在build.gradle文件中设置了我的模块依赖项:

dependencies
{
   compile fileTree(dir: 'libs', include: ['*.jar'])
   compile 'com.android.support:support-v4:22.2.0'
   androidTestCompile fileTree(dir: 'libs/test', include: ['*.jar'])
}
Run Code Online (Sandbox Code Playgroud)

在我的libs文件夹中,我拥有构建应用程序所需的所有库.我放置了在名为"test"的子文件夹中运行测试所需的库

这似乎适用于我的代码编译,我可以创建我的模块.但是,当我尝试运行任何测试时,它无法找到我试图包含的测试库.Gradle控制台中的错误实际上指向我的代码中没有显示任何错误的部分.

此外,我确实正确设置了测试,因为我可以通过将所需的jar移动到libs文件夹并将我的依赖项更改为:来运行它们:

dependencies
{
   compile fileTree(dir: 'libs', include: ['*.jar'])
   compile 'com.android.support:support-v4:22.2.0'
}
Run Code Online (Sandbox Code Playgroud)

但是,如果它不起作用,我真的没有看到androidTestCompile的重点.如果我不需要,我也不想在我的apk中包含这些jar文件.

如果它有帮助,我使用Android studio版本1.2.2和Gradle插件版本1.2.3.

在此先感谢您的帮助.

Nov*_*ile 5

见下面的例子.

创建与src文件并行的测试文件夹.将所有测试用例创建到该文件夹​​中.如果您将文件夹创建到另一个路径,则需要对此进行调用.(已添加到代码中.)

 dependencies {
    androidTestCompile files('libs/dexmaker-1.1.jar')
    androidTestCompile files('libs/org.mockito:mockito-core:3.9.5')
    androidTestompile files('espresso-1.0-SNAPSHOT-bundled.jar')
    }

defaultConfig {

    testApplicationId "xxx.xx.abc"
    testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"

}

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']

    }

    // Move the tests to tests/java, tests/res, etc...
    instrumentTest.setRoot('tests')

    instrumentTest {

        java.srcDirs = ['tests/src']

    }

    androidTest.setRoot('tests')
    androidTest.java.srcDirs = ['tests/src']
Run Code Online (Sandbox Code Playgroud)