Running a Finalize task on failure of any task in Android Project build


(Ipoo Doh) #1

Hi

i have a multi project android build rootProject, AndroidLibrary & AndroidApp

├── AndroidApp
│
 └── build.gradle
│
├── AndroidLibrary
│ └── build.gradle
│
├── build.gradle
└── settings.gradle

The root gradle file just does the workspace setup and calls build of the library and AndroidApp and then tags and sends email in the end The gradle file under the library and app, just setup dependency and apply required plugins

Sample Root gradle file

task BuildDist (dependsOn: ['Init','Clean', 'Fetch', 'Build','Publish']) <<{
  }
BuildDist.finalizedBy 'Finalize'
  task Init <<{
 //Initializes
}
...
...
  task Build (dependsOn: [':AndroidLibrary:build', ':AndroidApp:build'])<<{
  }
  ...
  task Finalize <<{
   //Tag Src if build successfull
 //Update DB of the results
 //Send Email
 }

It works fine, if the build succeeds but if the build fails to compile the Finalize task does not get executed, so am not able to do tasks that needs to be done at the end of every build, whether successful or fail.

I cannot set explicitly set :AndroidLibrary:build to be finalized by Finalize task. i tried iterating through all the tasks in the project and adding finalizedBy ‘Finalize’ but it ends up giving out of index error. I would like to know if there is a way for me to execute a specific task at the end of the build, doesnt matter what tasks fail in between, android plugin adds a lot of tasks

Cheers Ipoo Doh


(Peter Niederwieser) #2

One way to achieve this is to use the ‘gradle.buildFinished {}’ callback. For details, see ‘Gradle’ in the Gradle Build Language Reference.


(Ipoo Doh) #3

Thanks Peter that did it