2016-01-05 57 views
4

的测试中,我有一个多模块项目的gradle看起来像这样:摇篮 - 添加依赖到另一个模块

Parent 
|--server 
|--application (android module) 
+--common 

服务器测试对通用模块测试的依赖性。为此,我添加了

testCompile files(project(':common').sourceSets.test.output.classesDi 
compileTestJava.dependsOn tasks.getByPath(':common:testClasses') 

它工作得很好。不幸的是,当我试图对应用程序模块做同样的事情时,它也对通用模块测试具有依赖性,但它不起作用。它失败:

Build file 'application\build.gradle' line: 103 
A problem occurred evaluating project ':application'. 
Could not find property 'sourceSets' on project ':common' 

google搜索了一下后,我也试过

project.evaluationDependsOn(':common') 
    testCompile files(project(':common').sourceSets.test.output.classesDir) 

但失败,另一个异常:

Project application: Only Jar-type local dependencies are supported. Cannot handle: common\build\classes\test 

关于如何解决此问题的任何想法?

回答

7

在本文中有几种解决导入测试类问题的方法。 https://softnoise.wordpress.com/2014/09/07/gradle-sub-project-test-dependencies-in-multi-project-builds/我使用的是:

代码共享模块:根据共享模块模块

task jarTest (type: Jar) { 
    from sourceSets.test.output 
    classifier = 'test' 
} 

configurations { 
    testOutput 
} 

artifacts { 
    testOutput jarTest 
} 

代码: dependencies{ testCompile project(path: ':common', configuration: 'testOutput') }

而且似乎是它的一个插件,以及! https://plugins.gradle.org/plugin/com.github.hauner.jarTest/1.0

+0

就像一个魅力@sakis kaliakoudas –

+2

当我定义了新的任务,我得到一个错误:“为SourceSet无法获取不明财产‘测试’容器“,但我不知道这是什么意思 – Apperside

+1

@Apperside检查[答案由droidpl](http://stackoverflow.com/a/36062014/1427177)不会抛出该错误。 – jenzz

3

遵循sakis的方法,这应该是您需要从Android平台中的其他项目获得测试的配置(针对调试变体完成)。 共享模块:

task jarTests(type: Jar, dependsOn: "assembleDebugUnitTest") { 
    classifier = 'tests' 
    from "$buildDir/intermediates/classes/test/debug" 
} 
configurations { 
    unitTestArtifact 
} 
artifacts { 
    unitTestArtifact jarTests 
} 

您的模块:

dependencies { 
    testCompile project(path: ":libName", configuration: "unitTestArtifact") 
}