Skip to content

Instantly share code, notes, and snippets.

@bademux
Last active August 29, 2015 14:10
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bademux/adcebd74753ec2d9398a to your computer and use it in GitHub Desktop.
Save bademux/adcebd74753ec2d9398a to your computer and use it in GitHub Desktop.
// please check https://github.com/bademux/robolectric-java-gradle-plugin instead
//build.gradle
//fixes AndroidStudion<>Robolectric integration
//fixes 'java.lang.RuntimeException: Stub!' and 'NoClassDefFoundError'
gradle.projectsEvaluated {
def imlFile = file(project.name + '.iml')
def parsedXml;
try {
parsedXml = (new XmlParser()).parse(imlFile)
} catch (Exception e) { //fix CI build
println 'iml file not found, nothing to do here!'
return
}
def jdkNode = parsedXml.depthFirst().orderEntry.find { it.'@type' == 'jdk' }
def component = jdkNode.parent()
boolean imlChanged = false
if (!component.children().last().equals(jdkNode)) {
println 'iml fix: moving <orderEntry type="jdk"> to the end'
component.remove(jdkNode)
component.append(jdkNode)
imlChanged = true
}
if (component.'output-test'.isEmpty()) {
println 'iml fix: adding <output-test>'
component.appendNode('output-test', ['url': "$project.buildDir/test-classes"])
imlChanged = true
}
if (imlChanged) {
new XmlNodePrinter(new PrintWriter(new FileWriter(imlFile))).print(parsedXml)
}
}
//fixes 'NoClassDefFoundError' (or run manually 'testDebugClasses' before test)
tasks.findByName("assembleDebug").dependsOn("testDebugClasses")
@fcduarte
Copy link

fcduarte commented Dec 6, 2014

Life saver! You should send this to @robolectric guys!

I did some small changes here in order to work:

  • Test if iml file exists. If you clone a repo from scratch or use a CI server you'll have an error
    def parsedXml;
    try {
        parsedXml = (new XmlParser()).parse(imlFile)
    }
    catch (Exception e) {
        println 'iml file not found, nothing to do here!'
        return
    }
    component.appendNode('output-test', ['url': "file://\$MODULE_DIR\$/build/test-classes"])

And it's working for me on both Android Studio (1.0 RC4) and IntelliJ (14.0.2 RC EAP) .. Hope you find useful!

@aat-antoine
Copy link

In my case it works (AS 1.0 RC4) to move jdk to the end of my iml file but I keep the error
Class not found
even if i run testDebugClasses before test

@bademux
Copy link
Author

bademux commented Dec 8, 2014

thanks, It was temporary fix.
I'm looking for a good solution, try https://github.com/bademux/robolectric-java-gradle-plugin .
And here the usage example https://github.com/bademux/deckard-gradle

Seems that robolectric guys aren't happy with my solution: https://groups.google.com/d/msg/robolectric/nJgHtdbkpi8/VUTn1mEcMqEJ

bugreports and patches are welcomed

@fcduarte I applied you patch, thanks. AS will magically replace $project.buildDir with $MODULE_DIR so no problem here.
But as I said, it is ugly hack for unsupported solution (placing Robolectric in the same project with Instrumention tests). It is better to find a way to move robolectric tests to another project and play with classpath.

@kageiit
Copy link

kageiit commented Dec 18, 2014

I wrote a Gradle plugin to integrate Robolectric tests into Android Studio with minimal configuration.
If you are curious, please check out: https://github.com/kageiit/gradle-robojava-plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment