I have an android project with multiple modules. I am trying to run a custom gradle task from one of the modules, but each time I run the task all the other gradle tasks in the module as well as in the other modules. My task is not dependent on any other tasks. Tasks :
task helloTask{
println "Hello task"
}
I have tried running this task through the terminal window in studio as well as from command line.
Press ⌃⌃ (macOS), or Ctrl+Ctrl (Windows/Linux), type "gradle" followed by the gradle task name or names. We can, of course, run Gradle commands from the terminal window inside IntelliJ IDEA. Open this with ⌥F12 (macOS), or Alt+F12 (Windows/Linux).
buildSrc is a separate build whose purpose is to build any tasks, plugins, or other classes which are intended to be used in build scripts of the main build, but don't have to be shared across builds.
Gradle will execute all the tasks not declared with <<
during the configuration phase. If you want to delay the execution of a task till the execution phase then you could just add the <<
In your build.gradle
task helloConfiguration { task ->
println "Hello configuration phase task! $task.name"
}
/* Notice the `<<` this denotes to gradle to not execute
* the closure during the configuration phase. Instead
* delay closure's execution till the execution phase.
*/
task helloExecution << { task ->
println "Hello execution phase task! $task.name"
}
helloExecution.dependsOn helloConfiguration
Then when executing the helloExecution
task we see both run, order ensured. Next if we only want to run the tasks that configure the build we can do that separately if we want and only run a single task.
$ gradle helloExecution
Hello configuration phase task! helloConfiguration
Hello execution phase task! helloExecution
:helloConfiguration UP-TO-DATE
:helloExecution UP-TO-DATE
BUILD SUCCESSFUL
Total time: 0.64 secs
$ gradle helloConfiguration
Hello configuration phase task! helloConfiguration
:helloConfiguration UP-TO-DATE
BUILD SUCCESSFUL
Total time: 0.784 secs
Tasks that run during the configuration phase will ALWAYS be executed even if no tasks are supplied, which is the behavior I expect your seeing. So given the example above. Notice the configuration task ran but not the execution.
$ gradle
Hello configuration phase task! helloConfiguration
:help
Welcome to Gradle 2.10.
To run a build, run gradle <task> ...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL
Total time: 0.651 secs
So if you have 5 tasks that run in the configuration phase then you would see all of them execute, regardless of the task the command line args attempted to invoke for the execution phase's target.
In Android Studio bring up Gradle view (top right corner of Android Studio Window)
Press Run Gradle Task (round button).
From the list of modules select the module containing build.gradle, then select task from the list of tasks.
Also in Gradle view tree your task appears under YourModule/Tasks/other
, unless group
is explicitly specified for the task.
You can use Run Configurations to achieve the same. Refer: https://developer.android.com/studio/run/rundebugconfig.html
Go to Run -> Edit Configurations -> Click on + to add a new configuration -> Select Gradle from the list that comes up. Finally select the app, and type in the task that you want to run. Android Studio will even provide autocomplete for the same.
Later, running that task will be available as an option directly in the "Run" menu.
May be you are not giving the right command?
Process to run an independent task:
app/build.gradle
file.
Eg: task helloExecution { task ->
doLast {
println "Hello exececuted"
}
}
./gradlew taskName
./gradlew helloExecution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With