13

Is there a way to execute gradle task once after project Sync with Gradle files is complete?

I've tried to set task dependency to preBuild, as I've seen gradle:build is triggered when Sync is executing. But the problem is that dependency doesn't seem to work, task is not executed and I have to manually start the task after each Sync.

This is basically what I've tried so far

apply plugin: 'com.android.library' ... task myTask { ... } gradle.projectsEvaluated { preBuild.dependsOn(myTask) } 

I've also tried to set task dependency to other tasks that I see are triggered (:generate{Something}), but that wasn't successful either.

Is there anything I can to do force the gradle task to be executed after each Sync? I'm using Gradle 2.2.1 + Android Studio 1.0.2

3
  • The generateDebugSources task is run on project sync -- you can see this in the Gradle console. Commented Jan 7, 2015 at 17:05
  • I've tried to bind task that generateDebugTestSources depends on it (this one is executed as well with generateDebugSources), but that doesn't seem to help Commented Jan 7, 2015 at 19:56
  • generateDebugSources didn't work either, but I've managed to make it work without wrapping the dependency in projectsEvaluated Commented Jan 8, 2015 at 9:18

4 Answers 4

13

Finally, I've managed to trigger the task on every Sync event. Apparently gradle.projectsEvaluated is either not executed at all when syncing, or it is executed after build task, so the solution is to get rid of it completely

apply plugin: 'com.android.library' ... task myTask { ... } preBuild.dependsOn(myTask) 
Sign up to request clarification or add additional context in comments.

Comments

10

Inside the Gradle menu (usually located on the upper right corner of Android Studio), there is a list of tasks. By right clicking on the task, it is possible to set Execute After Sync.

enter image description here

2 Comments

One drawback of this beautiful approach is that the flag is not stored in a VC-backed file. So, unfortunately, it does not scale to bigger developer teams.
There is now a solution for limitation I noted above: stackoverflow.com/a/61285609/192373
5

Some while ago JetBrains extended their idea gradle plugin and now you can write something like

idea.project.settings { taskTriggers { afterSync tasks.getByName("myTask") } } 

You must apply the plugin, such as

plugins { id "org.jetbrains.gradle.plugin.idea-ext" version "0.7" } 

Comments

1

according to docs:

A Gradle build has three distinct phases

Initialization...

Configuration During this phase the project objects are configured. The build scripts of all projects which are part of the build are executed.

Execution...

Our build.gradle files are executed during Configuration phase. Task is a class (we extend org.gradle.api.DefaultTask when developing them). So let's just call our task's execute method:

task myStandaloneTask(type: MyStandaloneTaskImpl){ println("myStandaloneTask works!") } // let's call our task myStandaloneTask.execute() task myInsideGradleTask { println("myInsideGradleTask works!") } // let's call our task myInsideGradleTask 

where MyStandaloneTaskImpl is a Task developed in buildSrc or as Standalone project, details

P.S. No need to use parenthesis () after myInsideGradleTask because of Groovy

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.