Gradle Task from Plugin Doesn't Run on “Build”, But Does Run on “Clean”

20
February 12, 2019, at 04:30 AM

We have an Android project which requires a certain Gradle Plugin Task to run before we build the APK. (The plugin is written by us)

We want to run the task automatically before every build.

If we use the deprecated task.execute() then we get a warning that it will be unavailable starting with version 5.0 or something like that.

If we use the dependsOn as recommended, then testTask1 is not before BUILD, but only after CLEAN. (all explained in the comments below)

I've been over the gradle docs, and many other SO threads, but I have yet to find a solution.

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        flatDir { dirs 'libs' }
        jcenter()
        google()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:3.1.3"
        // our platform-tools plugin, in charge of some gradle tasks
        classpath 'sofakingforevre:test-plugin:1.0-SNAPSHOT'
    }
}

apply plugin: 'test-plugin'

allprojects {
    repositories {
        jcenter()
        google()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

// OPTION 1 - USING EXECUTE()
// this task works as expected when calling "clean", but also when calling "assemble".
// the problem here is that the "execute" method has been deprecated, and we want to prepare for Gradle 5.0
// CLEAN - testTask1 is called :)
// BUILD - testTask1 is called :)
// DEPRECATION WARNING :(
task buildPlatformExecute {
    println("executing")
    // this task is created by the plugin
    tasks.getByName("testTask1").execute()

}
clean.dependsOn buildPlatformExecute
// OPTION 2 - USING DEPENDSON()
// this tasks works as expected when calling "clean", but DOES NOT WORK when calling "assemble".
// If we call we call assemble, the "executing" text does print, but "testTask1" would not run.
// CLEAN - testTask1 is called :)
// BUILD - testTask1 is NOT CALLED :(
task buildPlatformDependency {
    println("executing")
    // this task is created by the plugin
    dependsOn 'testTask1'
}
clean.dependsOn buildPlatformDependency
Answer 1

Issues with your OPTION 1 solution

  • you are using deprecated task.execute() API (you are already aware of that)
  • you are using mixing configuration and execution phase (a common Gradle mistake...):

Since you did not wrap tasks.getByName("testTask1").execute() in a doLast {} of doFirst {} block in buildPlatformExecute task : the testTask1 task will always be executed, whatever task you will invoke. You do not even need to create dependency between clean task and your custom task ( e.g.: try to execute simple "help" task with./gradlew help you will see that testTask1 is also executed: this is certainly not what you want)

More information here : https://docs.gradle.org/current/userguide/build_lifecycle.html

Issue with your OPTION2 solution

You created a dependency between clean task andbuildPlatformDependency task :

  • when executing clean tasks, testTask1 task will be executed as expected, but
  • there is no dependency between build (or assemble) task and clean task: that's why when you execute build task, clean task is not executed (so testTask1 won't be triggered)

Solution

The best approach would be to hook you custom task testTask1 in the correct place in your project build lifecycle, using Task.dependsOn API. The "correct" place depends on what your task is responsible for in the build process: for example if your task needs to be executed BEFORE assemble task, simply create dependency assemble.dependsOn testTask1

READ ALSO
Android Things UDP Socket over IPv6

Android Things UDP Socket over IPv6

There is pretty much no error logs but it can't actually send anything over mesh network on udpSeems to receive without any issues but on send it doesn't go

23
Android X + Truth + Guava test compile issue

Android X + Truth + Guava test compile issue

I have an Android library (called api) gradle module as part of a larger projectI just migrated the whole project to AndroidX

14
Remove native base FABs ripple effect on android

Remove native base FABs ripple effect on android

I'm having problem with native base FAB button it shows a square ripple effect when i press it

33