Run Task Before Compilation Using Android Gradle Plugin

Run task before compilation using Android Gradle plugin

The proper way to run a task before Java compilation on Android is to make a compilation task for each variant depend on your task.

afterEvaluate {
android.applicationVariants.all { variant ->
variant.javaCompiler.dependsOn(generateSources)
}
}

Execute task before Android Gradle build?

You can do it in this way:

task build << {
println 'build'
}
task preBuild << {
println 'do it before build'
}
build.dependsOn preBuild

Thanks to that task preBuild will be automatically called before build task.

If you want to run preBuild in configuration phase (previous example run preBuild in execution phase) you can do it in this way:

task build << {
println 'build'
}
build.doFirst {
println 'do it before build'
}

More about gradle build lifecycle can be read here http://www.gradle.org/docs/current/userguide/build_lifecycle.html.

How to execute a task before executing or building the subprojects in gradle?

I have created a sample project for this. The root project is called 'protobuffer' and it has two sub projects as follows:

  • java-project
  • proto

'proto' project contains proto files for the java project. proto project's build.gradle.kts file is as follows:

import com.google.protobuf.gradle.*

plugins {
id ("com.google.protobuf")
}

tasks.check { dependsOn("generateProto") }

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.6.1"
}

generatedFilesBaseDir = File(project(":java-project").projectDir.toString(), "src").toString()
}

dependencies {
implementation("com.google.protobuf:protobuf-java:3.6.1")
}

Root project's build.gradle.kts file is as follows:

import com.google.protobuf.gradle.GenerateProtoTask
plugins {
java
id ("com.google.protobuf") version ("0.8.8") apply false
}

allprojects {
repositories {
mavenCentral()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
}

subprojects {
apply(plugin="java")
apply(plugin="idea")

if (name != "proto") {
tasks.withType<JavaCompile> {
dependsOn(project(":proto").tasks.withType<GenerateProtoTask>())
}
}
}

The settings.gradle.kts file in the root project is as follows:

rootProject.name = "protobuffer"
include("proto")
include("java-project")

pluginManagement {
repositories {
mavenLocal()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}

resolutionStrategy {
eachPlugin {
if (requested.id.id == "com.google.protobuf") {
useModule("com.google.protobuf:protobuf-gradle-plugin:${requested.version}")
}
}
}
}

Run custom task before ALL possible packaging tasks in gradle android project

Gradle has a TaskContainer accessible via (project.)tasks. This container is also a TaskCollection containing all tasks. One can query a subset TaskCollection via the matching method and, thanks to some magic, the new TaskCollection is live. So, whenever a new task gets added to the parent TaskCollection (or the TaskContainer) and it matches the closure of the matching method, the subset TaskCollection will contain it. Together with the TaskCollection all method one can handle each task following a pattern whenever it is created.

For the problem stated in your question, I wrote and tested the following build file:

task assembleX { }

task copySqlMigrations { }

task assembleY { }

tasks.matching { task ->
task.name.startsWith('assemble')
}.all { task ->
task.dependsOn copySqlMigrations
}

task assembleZ { }

You can call each assemble* task and it will call copySqlMigrations as a dependency. Of course, you can modify the matching closure to fit your needs.



Related Topics



Leave a reply



Submit