解决方案是将 HTTP 服务器的项目分离到一个单独的项目中,并使用Gradle 的多项目功能来设置 Android 测试以依赖于正在启动的 HTTP 服务器。因为单独的项目是一个 Groovy 项目,所以 IntelliJ 从项目的依赖项中读取要使用的 Groovy 版本,一切正常。
dependencies {
compile "com.android.support:support-v13:+"
stubs project(":integration-server")
}
/*
* The android plugin defers creation of tasks in such a way that they can't be accessed eagerly as usual
* @see http://stackoverflow.com/questions/16853130/run-task-before-compilation-using-android-gradle-plugin
*/
gradle.projectsEvaluated {
connectedAndroidTest.dependsOn(":integration-server:startBackgroundServer")
// Note: finalizedBy is still @Incubating
connectedAndroidTest.finalizedBy(":integration-server:stopBackgroundServer")
}
集成服务器/build.gradle
apply plugin: "groovy"
apply plugin: "spawn"
dependencies {
compile "org.codehaus.groovy:groovy:2.3.4"
compile "org.codehaus.groovy:groovy-ant:2.3.4"
compile "com.github.tomakehurst:wiremock:1.46"
}
task startBackgroundServer(type: SpawnProcessTask, dependsOn: "build") {
def cp = sourceSets.main.runtimeClasspath.asPath
command "java -cp $cp server"
ready "Started DelayableSocketConnector@0.0.0.0:8089"
}
task stopBackgroundServer(type: KillProcessTask)
为了防止 Gradle 构建阻塞,我使用Gradle Spawn 插件在后台启动 HTTP 服务器。