Specifying test dependencies with the Gradle Android build system

前端 未结 2 768
终归单人心
终归单人心 2020-12-09 15:33

I want to specify dependencies for my tests and after reading the Gradle Dependency Management Basics I though I could just add testCompile calls to my dependen

相关标签:
2条回答
  • 2020-12-09 15:49

    Just to make this question answered in full this is what is needed to resolve the issue. Since Junit is wanted for local testing and guava might be wanted for androidTest the android DSL should contain this:

    android {
        :
        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src/main/java']
                res.srcDirs = ['res']
                assets.srcDirs = ['assets']
                    :
            }
            test {
                java.srcDirs = ['src/test/java']
                resources.srcDirs = ['src/test/resources']
                    :
            }
            androidTest.setRoot('tests')
        }
        :
    }
    

    The : means other statements that you need. Please also note that the folders can be others than the ones above.

    0 讨论(0)
  • 2020-12-09 15:53

    The android build system doesn't use the standard Gradle Java plugin.

    Its documentation says:

    As mentioned previously, next to the main sourceSet is the androidTest sourceSet, located by default in src/androidTest/

    Additionally, the sourceSet can be configured to have its own dependencies. By default, the application and its own dependencies are added to the test app classpath, but this can be extended with

    dependencies {
        androidTestCompile 'com.google.guava:guava:11.0.2'
    }
    

    Update

    As of May 2017 Doc, testCompile is deprecated and you should use testImplementation

    dependencies {
            androidTestImplementation 'com.google.guava:guava:11.0.2'
    }
    
    0 讨论(0)
提交回复
热议问题