0

Background

I'm trying to get robospock (roboelectric + spock-framework) to work as a work-around for the fact that the android gradle plugin is stupid and doesn't let me apply plugin: 'groovy'.

However, robospock doesn't seem to work... I'm trying to override org.robospock.RobospockAction.metaClass.execute, but everything I've tried so far has either worked on an android-library project or an android project but not vice versa.

I have tried

When testing an android-library project, both of these worked: (full source: http://pastebin.com/pbxr4vUJ)

org.robospock.RobospockAction.metaClass.execute = { Project project ->
    // Project under test
    def put = project.project( project.ext.robospock )

    // collect and extract compiled classes in library project
    def subProjects = getSubprojects( put ).unique()
    def allProjects = subProjects + put

    // separate android & normal projects from each other.
    def androidProjects = allProjects.findAll { isAndroid( it ) }
    def normalProjects  = allProjects.findAll { !isAndroid( it ) }

    // collect and forward all maven dependencies
    def mavenDependencies = collectMavenDependencies( allProjects ).unique()
    mavenDependencies.each { dep ->
        project.dependencies {
            compile group: dep.group, name: dep.name, version: dep.version
        }
    }

    // add normal projects to dependencies.
    normalProjects.each { proj ->
        project.dependencies { compile proj }
    }

    // add android projects to dependencies, in an odd way.
    def main   = project.sourceSets.main
    def ssJava = main.java.srcDirs
    def ssRes  = main.resources.srcDirs

    androidProjects.each { proj ->
        // Find generated dir.
        def variants = proj.plugins.hasPlugin( 'android-library' )
                     ? proj.android.libraryVariants
                     : proj.android.applicationVariants
        def genDir = variants.find { it.name == 'debug' }.dirName

        // Add resource sets.
        def androidMain = proj.android.sourceSets.main
        ssRes.addAll( androidMain.res.srcDirs )

        // Add source sets.
        ssJava.addAll( androidMain.java.srcDirs )
        def sourceDir =  proj.buildDir.path + "/generated/source/"
        ["r/", "buildConfig/"].each {
            ssJava.addAll( sourceDir + it + genDir )
        }
    }
    // ...
}

or:

org.robospock.RobospockAction.metaClass.execute = { Project project ->
    // Project under test
    def put = project.project( project.ext.robospock )

    project.dependencies { compile ':' + put.name + ':debugVersion' }
    // ...
}

When I try to use the first version for a normal android project, the build fails with: http://pastebin.com/n3AZRMg0 This claims that for example: package android.support.v4.app does not exist - while it also states that com.android.support:support-v4:20.0.0 is a dependency (everything works when i do gradle assembleDebug).

The second version complains about not finding the project suffixed with :debugVersion in various ways - is :debugVersion an android-library only thingy?

When change it to project.dependencies { compile put } it works, but does a release build as well, which is a big no-no due to signing-configs, and long build times.

TL;DR / Question

1) How do I get gradle to only use an (android project as a) dependency with debug version only - or how do I force it to at least skip doing the whole release part?

2) Why are the mysterious error outputs coming in the first version?

3) Any other ways I can solve my problem of getting robospock or spock based testing for android to work?

Centril
  • 2,549
  • 1
  • 22
  • 30
  • 1
    we'd love to help you with integrating robospock into your project. could you please post your project (relevant part of it) somewhere, like on github? here is a proof that it works ;-) https://github.com/DerSchimi/RoboSpockExample/pull/1 – pelotasplus Sep 30 '14 at 06:00
  • Thanks, any help would be much appreciated! Here's the feature-branch that uses robospock: https://github.com/Centril/sleepfighter/tree/librarize Each directory suffixed with -test has a corresponding directory without it that contains the actual non-testing code. I've not yet added any unit tests for them, but things fail in the CLI for :sleepfighter-test:robospock. – Centril Sep 30 '14 at 06:18
  • Also relevant: https://github.com/pjakubczyk/robospock-plugin/issues/8 The pull-request linked uses Gradle 1.10... I'm using Gradle 2.1 – Centril Sep 30 '14 at 06:22
  • I've made my own robospock-plugin for gradle now: https://github.com/Centril/gradle-plugin-robospock – Centril Oct 28 '14 at 08:46

0 Answers0