2016-01-05 53 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") 
}