1. foreword
When we use Gradle to build tasks, we sometimes want to count the time consumption of each task, so as to find out the running time of each task and whether there is room for optimization.
train of thought
1.Custom Gradle plugins
2.Use Listener to monitor
3.Register Listener in custom Gradle plugin
Gradle provides many build lifecycle hook functions
- We can use TaskExecutionListener to monitor the execution of tasks throughout the build process:
1 |
|
We can collect information before each task execution, record the start time of the task execution, and record the execution end time after the task execution is completed, so that we can count the execution time of the task.
- Use BuildListener to monitor whether the entire build is complete. After the build is complete, output all executed task information and the execution time of each task
1 | package org.gradle; |
In the buildFinished method, monitor the completion and success of the build, and print the time-consuming information we collected.
2.code implementation
Create main project
Create the main project TaskBuildTimeDemo, there is only one MainActivity in the current project, as follows:
Custom Gradle plugins
First create a new module in the TaskBuildTimeDemo project, select the Android Library type, and name it task_build_time_plugin.
Configure the plug-in environment, delete all the contents of the build.gradle of the task_build_time_plugin module, and change it to the following:
Because the Gradle plug-in is written in the groovy language, it is necessary to create a new groovy directory to store the .groovy classes related to the plug-in. The structure is as follows
Then, create a directory com.example.task_build_time_plugin in groovy and create a class TaskBuildTimePlugin.groovy file in this directory. Rewrite the apply method in TaskBuildTimePlugin to implement the plug-in logic, first simply print a log log to see the effect
Next, register the plug-in, create a new resources/META-INF/gradle-plugins directory under main, and create a com.example.taskbuildtime.properties file. This file name is the name referenced by other modules when passing the apply plugin. The content in it specifies the plug-in class full class name
Publish the plugin to the local warehouse, because we referenced the maven-publish plugin, double-click publish to publish to the specified local warehouse path
Go to the specified local warehouse to see if the corresponding plugin is generated
test plugin
Depend on the plugin in the outermost build.gradle of the project
Then import the plugin in the corresponding module
Execute the assemble Debug command of gradle, if the log in the plug-in class is printed out, it means that the custom plug-in is successful
- Task time-consuming tool class implementation
1 | package com.example.task_build_time_plugin |
Register the task time-consuming monitoring tool class in the plug-in
Task time-consuming statistical results
So far, the custom Gradle plug-in to realize the time-consuming function of statistical tasks has been basically completed. We can find out the time-consuming tasks and see if there is room for optimization, so as to improve the efficiency of packaging