Question

Just when I'd achieved an effective development and build environment with the android-maven-plugin, the new kid on the block, Gradle, starts making inroads into the Android circles. Not being hot on Groovy and with the android-gradle plugin almost as fragmented as the OS itself I've hit some issues. Specifically around building library projects, with flavours and our buddy Robolectric.

Short version

I am at a loss as to what my next move should be upon encountering the gradle error;

Cannot add a SourceSet with name 'testDebug' as a SourceSet with that name already exists.

The error emanates from having productFlavours on a library (i.e. moving to the 0.9.2 android build system) and the gradle-android-test-plugin recently forked by the team over at Robolectric from Jake's creation (see here). I have followed all lines of investigation to near exhaustion and can report that the meer existence of the 'android-test' plugin within my library gradle file sends things awry.

Longer version

Here is the abridged application build.gradle file with pertinent information retained;

apply plugin: 'android-library'
apply plugin: 'android-test'
...
android {
    compileSdkVersion 19
    buildToolsVersion '19.0.3'

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 19
        versionCode buildNumber().toInteger()
        versionName "1.0.0"
    }

    productFlavors {
        estimote {
            dependencies {
                compile '<flavour specific dependency>'
            }
        }

        radius {
            dependencies {
                compile '<flavour specific dependency>'
            }
        }
    }
}
...
dependencies {
    compile 'com.squareup.dagger:dagger:1.2.1'
    compile 'com.squareup.dagger:dagger-compiler:1.2.1'
    compile 'com.google.code.gson:gson:2.2.+'

    // Testing dependencies
    androidTestCompile 'org.mockito:mockito-core:1.9.5'
    androidTestCompile 'com.squareup:fest-android:1.0.7'
    androidTestCompile 'org.hamcrest:hamcrest-all:1.3'
    androidTestCompile 'org.robolectric:robolectric:2.2'
    androidTestCompile 'junit:junit:4.11'
}

And here is the abridged root build.gradle file.

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.2'
        classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.+'
    }
}

allprojects {
    repositories {
        mavenLocal()
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots'
        }
    }
}

If you've got this far down the page, give yourself a pat on the back. Now, the eagle eyed amongst you have probably noticed the omission on the sourceSets redirection with commands akin to;

sourceSets {
        androidTest {
            setRoot('src/test')
        }
    }

After the initial error is corrected these lines will need to be reinstated to inform gradle of the project's structure. The project's structure is standard and looks like;

- project_name
  + gradle
  - lib
     + flavour1
     + flavour2
     - main
        + java
     - test
        + java
    build.gradle
 build.gradle
 gradle.properties
 settings.gradle

What is being used

The app is using gradle-1.10-all, 0.9.2 android-gradle plugin and 0.9.+ gradle-android-test-plugin.

The question

How should the project be set-up/changed to facilitate Robolectric testing on a library with flavours? Is this even possible yet?

Was it helpful?

Solution

I ran into the same issue, dug into the code, fixed it, and submitted a pull request which has just now been merged. See my explanation on the PR for details, but it boils down to a bad optimization in the plugin code:

https://github.com/robolectric/robolectric-gradle-plugin/pull/70

As of today you need to clone the repo and build and install the plugin to your local maven repo. The next time they do a release to maven central (perhaps release 0.13.1?), you'll be able to use the plugin directly from there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top