Gradle Tasks and Plugins, Something more Customized!
1. Creating Tasks
You may want Gradle to output an APK file containing the build date in its name. One possible solution to this is to create a custom Gradle task.
Add the following code in your module-level build.gradle file at the same level as android
block:
// 1
task addCurrentDate() {
// 2
android.applicationVariants.all { variant ->
// 3
variant.outputs.all { output ->
// 4
def date = new Date().format("dd-MM-yyyy")
// 5
def fileName = variant.name + "_" + date + ".apk"
// 6
output.outputFileName = fileName
}
}
}
Here’s what’s is going on:
- You define an
addCurrentDate()
task. - You iterate through all the output build variants.
- You iterate over all the APK files.
- You create an instance of
Date
and format it. - You create a new filename appending the current date to the initial name.
- You set the new filename to the current APK file.
Now you need to execute this task at a specific point of the build process. Add the following code below the task addCurrentDate()
block:
gradle.taskGraph.whenReady {
addCurrentDate
}
The task specified in the whenReady
the block will be called once when the current graph is filled with tasks and ready to start executing them. Here, you specify the name of your addCurrentDate
task.
Now, Run the following command to assemble a build:
./gradlew assemblePaidRelease
After the task has completed, go to the output directory and check if the build has been named correctly, You should get a similar output:
output.json paidRelease_12-11-2017.apk
If your task executed correctly, all your builds will be named with this convention.
2. Creating Custom Plugins
Usually, it’s a good idea to factor out your code into smaller pieces so it can be reused. Similarly, you can factor out your tasks into a custom behavior for the building process as a plugin. This will allow you to reuse the same behavior in other modules you may add to your project.
To create a plugin, add the following class below the addCurrentDate
task in the module-level build.gradle file:
class DatePlugin implements Plugin<Project> {
void apply(Project project) {
project.task('addCurrentDatePluginTask') {
project.android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def date = new Date().format("dd-MM-yyyy")
def fileName = variant.name + "_" + date + ".apk"
output.outputFileName = fileName
}
}
}
}
}
Add the name of your plugin at the top of this file along with the other apply plugin
definitions:
apply plugin: DatePlugin
Conceptually, the code in the plugin is doing the same thing as the task. The only difference is that you define a class that implements Plugin
and its single method apply(Project project)
.
In this method, you’re adding your plugin to the target Project
. By calling the task(String name, Closure configureClosure)
the method you’re creating a new task with a specific name and behavior and adding it to the project.
Now modify the whenReady
block to call a new task:
gradle.taskGraph.whenReady {
addCurrentDatePluginTask
}
and remove the task addCurrentDate()
block you added earlier.