Corneliu Corneliu - 1 month ago 10
Android Question

Gradle execute command lines in custom task

In my gradle file I defined the following task:

task text_example <<
{
//?!? commandLine ''
println 'Fam Flinstone'
}


I want to put inside this task some commands line. How can I do that ?

I'm using a library to automatically publish in google play. My project is based on Product Flavors and I need to pass in terminal command line by command line for every and each one of my flavors. So I want to pass all commands line in
test_example
task.

Answer

You basically have two major convenient options:

  1. Use Gradle Exec task type

    task fooExec(type: Exec) {
        workingDir "${buildDir}/foo"
        commandLine 'echo', 'Hello world!'
    } << {
        println "Executed!"
    }
    
  2. Use Gradle Project.exec method

    task execFoo << {
        exec {
            executable 'echo'
            args 'Hello world!'
        }
        println "Executed!"
    }
    

In both cases inside the closure that you can specify execution parameters using methods of ExecSpec. Standard output and error output from the executed commands will be redirected to stdout and stderr of the gradle process.

Comments