Skip to content

Gradle Task to Fix IML File

Emmet McPoland edited this page Jan 30, 2015 · 3 revisions

Gradle Task to Fix IML File

The following task can be used to automatically fix IntelliJ IML files that have been mangled by Android Studio. Add the following to your build.gradle:

task initGradleTest << {

    // Get project.iml File' Absolute Path
    def imlFile = project.projectDir.absolutePath + '/' + project.name + '.iml'

    if (!file(imlFile).exists()) return

    def parse = new XmlParser().parse(imlFile)

    def modulePath = parse.@'external.linked.project.path'

    // It's Robolectric Default ouputPath
    def outputTestPath = "file://$modulePath/build/test-classes"
    def moduleComponent = parse.component.find { it.@name == 'NewModuleRootManager' }
    def outputTest = moduleComponent.find {it.name() == 'output-test'}

    if (outputTest != null) {
        outputTest.@url = outputTestPath
    } else {
        moduleComponent.appendNode('output-test', [url : outputTestPath])
    }

    // jdk orderEntry must be last
    def orderEntry = moduleComponent.orderEntry
    def jdkOrderEntry = orderEntry.find { it.@type == 'jdk' }
    moduleComponent.remove(jdkOrderEntry)
    moduleComponent.append(jdkOrderEntry)

    // rewrite $project.iml file
    FileWriter fileWriter = new FileWriter(imlFile)
    new XmlNodePrinter(new PrintWriter(fileWriter)).print(parse)


}

tasks.preBuild.dependsOn initGradleTest

android.applicationVariants.all { variant ->

    if (variant.buildType.name == 'debug') {
        // only compile dependsOn debug
        def capitalizedFlavorName = variant.name.capitalize()

        def originCompileTestTask = tasks.getByName("compile${capitalizedFlavorName}TestJava")
        def robolectricCompileTask = tasks.getByName("compileTest${capitalizedFlavorName}Java")

        originCompileTestTask.dependsOn robolectricCompileTask
    }
}
Clone this wiki locally