Working with robolectric on android studio is great but is so dificult to get to it, change the iml and know exactly what you have to do to make it work.

So I just tried to do a small piece of gradle code that modifies the iml to do all the workaround to make it work. (See my post Android Studio + Robolectric workarounds using gradle)

Well and this was the excuse to lean a bit more of gradle... after watching some of the videos of the Droidcon London 2014
explaining how to work with gradle, so be aware the code might not be perfect and that I'm still learning :)

Basically I defined a task that modifies the iml file inside the project and attach it to all "assemble.*" task, also attach the compileTest to compile the tests.

If you wanna try, you can just add

apply from: ''  

after applying the robolectric plugin or just copy
al the script at the end of your build.gradle.

Happy Testing :)

project.afterEvaluate {  
    // Modifies the iml to move the orderEntry to the last
    task robolectrictAndroidStudioModifyIml {
        def imlTree = fileTree(dir: ".", include: "*.iml")
        inputs.files imlTree
        outputs.files imlTree

        imlTree.each { File imlFile ->
            def xml = new XmlParser().parse(imlFile)
            def jdk = xml.depthFirst().grep { it."@type" == "jdk" }.first()

            // move the orderEntry of type jdk to the bottom of the file
            def jdk_parent = jdk.parent()

            def outputTest = xml.component."output-test"[0] ?: new Node(jdk_parent, "output-test");
            outputTest.@url = "file://\$MODULE_DIR\$/build/test-classes"

            def writer = new StringWriter()
            new XmlNodePrinter(new PrintWriter(writer)).print(xml)
            imlFile.text = writer.toString()

    // compileTest task from robolectric
    def compileTestTask = project.tasks.findAll { ==~ /compileTest.*/

    if(compileTestTask) {
        project.tasks.findAll { // for all assemble tasks
   ==~ /assemble.*/
        }.each {
            it.dependsOn compileTestTask // compile the tests
            it.dependsOn robolectrictAndroidStudioModifyIml // modify the iml