Umang
Umang

Reputation: 1070

Execute a custom independent gradle task in android studio

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.

Upvotes: 24

Views: 41195

Answers (4)

May be you are not giving the right command?

Process to run an independent task:

  1. Add a task to you app/build.gradle file. Eg:
task helloExecution { task ->
    doLast {
        println "Hello exececuted"
    }
}
  1. On your terminal after navigating to the project folder, type ./gradlew taskName
    Eg: ./gradlew helloExecution

Upvotes: 4

cyanide
cyanide

Reputation: 3964

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.

enter image description here


Also in Gradle view tree your task appears under YourModule/Tasks/other, unless group is explicitly specified for the task.

Upvotes: 11

Kalam Shah
Kalam Shah

Reputation: 51

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.

Upvotes: 5

JBirdVegas
JBirdVegas

Reputation: 11413

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.

Upvotes: 14

Related Questions