Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Android Robospock Have
ext {
groovyVersion = '2.3.3'
spockVersion = '0.7-groovy-2.0'
roboSpockVersion = '0.4.4'
sdkDir = project(project.ext.robospock).android.sdkDirectory
buildscript {
repositories {
dependencies {
classpath "org.robospock:robospock-plugin:0.4.0"
repositories {
//Again the robospock-plugin does not add all the correct android dependencies
maven { url "${sdkDir}/extras/android/m2repository" }
maven { url "${sdkDir}/extras/google/m2repository" }
apply plugin: 'groovy'
dependencies {
compile "org.codehaus.groovy:groovy-all:$groovyVersion"
compile "org.spockframework:spock-core:$spockVersion"
compile "org.robospock:robospock:$roboSpockVersion"
// Disable the default test task otherwise commandline build fails because
// gradle test task tries to run the specs as if they were standard spock tests
test.onlyIf { false }
* The findCompileDependencies method of the org.robospock:robospock-plugin does not work with the
* latest gradle and android build tools.
* Consider fixing the plugin and submitting
org.robospock.RobospockAction.metaClass.findCompileDependencies = { Project androidProject ->
androidProject.configurations.all.find { == 'compile'
* The findCompileDependencies execute of the org.robospock:robospock-plugin does not take into account
* that the generated variants and placed in the 'generated' directory.
* Consider fixing the plugin and submitting
org.robospock.RobospockAction.metaClass.execute = { Project project ->
def androidProject = project.project(project.ext.robospock)
// collect and extract compiled classes in library project
def subprojects = getSubprojects(androidProject)
// collect and forward all maven dependencies
def allprojects = subprojects + androidProject
def mavenDependencies = collectMavenDependencies(allprojects)
mavenDependencies.each { dep ->
project.dependencies {
compile group:, name:, version: dep.version
def allSourceSets = ['src/main/java']
def allResourcesSets = ['src/main/res']
def testableVariant = { variant -> == 'devDebug'
["r/", "buildConfig/"].each {
allSourceSets.add(androidProject.buildDir.path + "/generated/source/" + it + testableVariant.dirName)
project.sourceSets.main {
java.srcDirs = allSourceSets
resources.srcDirs = allResourcesSets
Test test = project.tasks.create(org.robospock.RobospockAction.ROBOSPOCK_TASK_NAME, Test.class);
test.setDescription("Runs the unit tests using Robospock.")
test.workingDir = "${androidProject.projectDir}/src/main"
//Plugin must be applied last and must use qualified name when applied from a gradle script
apply plugin: org.robospock.RobospockPlugin

This comment has been minimized.

Copy link

DerSchimi commented Sep 22, 2014

Using your script I am getting this error while accessing testableVariant.dirName

Error:(97, 0) Cannot get property 'dirName' on null object

Can you give me a hint whats wrong with my setup?


This comment has been minimized.

Copy link

bluesliverx commented Oct 3, 2014

Just replace "devDebug" and "DevDebug" with "debug" and "Debug" respectively. I didn't have the "devDebug" variant, so using the debug one worked fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.