当前位置:   article > 正文

【Android Gradle 插件】Gradle 构建生命周期 ③ ( BuildListener 构建监听器 | TaskExecutionGraphListener 任务执行图监听器 )_gradle buildlistener

gradle buildlistener

Android Plugin DSL Reference 参考文档 :





一、BuildListener 构建监听器



org.gradle.api.Project 配置 ( build.gradle 根配置 ) 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html

org.gradle.api.initialization.Settings 配置 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/initialization/Settings.html

org.gradle.api.invocation.Gradle 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/invocation/Gradle.html

org.gradle.BuildListener 构建监听器 文档 : https://docs.gradle.org/current/javadoc/org/gradle/BuildListener.html


调用 Gradle#addBuildListenerGradle#addListener 函数 , 可设置 BuildListener 监听器 ;

BuildListener 监听器原型如下 :

@EventScope(Build.class)
public interface BuildListener {
    /** @deprecated */
    @Deprecated
    void buildStarted(Gradle var1);

    @Incubating
    default void beforeSettings(Settings settings) {
    }

    void settingsEvaluated(Settings var1);

    void projectsLoaded(Gradle var1);

    void projectsEvaluated(Gradle var1);

    void buildFinished(BuildResult var1);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

文档地址 : https://docs.gradle.org/current/javadoc/org/gradle/BuildListener.html


代码示例 :

gradle.addBuildListener(new BuildListener() {
    @Override
    void buildStarted(Gradle gradle) {
        // 构建开始时回调该函数
    }

    @Override
    void settingsEvaluated(Settings settings) {
        // 分析 settings.gradle 函数完成后回调该函数
    }

    @Override
    void projectsLoaded(Gradle gradle) {
        // 工程加载完毕回调
    }

    @Override
    void projectsEvaluated(Gradle gradle) {
		// 工程分析完毕回调 
    }

    @Override
    void buildFinished(BuildResult buildResult) {
        // 构建完成时回调的函数
    }
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26




二、TaskExecutionGraphListener 任务执行图监听器



org.gradle.execution.TaskExecutionGraphListener 构建监听器 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/execution/TaskExecutionGraphListener.html


调用 Gradle#addListener 函数 , 可设置 TaskExecutionGraphListener 任务执行图监听器 ;

Gradle 任务执行图 确定后 ,

TaskExecutionGraphListener 监听器原型如下 :

@EventScope(Build.class)
public interface TaskExecutionGraphListener {
    void graphPopulated(TaskExecutionGraph var1);
}
  • 1
  • 2
  • 3
  • 4

TaskExecutionGraphListener#graphPopulated 函数 :

void graphPopulated​(TaskExecutionGraph graph)
This method is called when the TaskExecutionGraph has been populated, and before any tasks are executed.TaskExecutionGraph已填充并且在执行任何任务之前调用此方法。

Parameters:
graph - The graph. Never null.
        任务图 . 不可为空 .
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

代码示例 : 调用 TaskExecutionGraph 任务执行图的 List<Task> getAllTasks(); 函数 , 可以获取到所有的 Gradle 任务 ;

// Gradle 任务执行图监听器
gradle.addListener(new TaskExecutionGraphListener() {
    @Override
    void graphPopulated(TaskExecutionGraph taskExecutionGraph) {
        println taskExecutionGraph.allTasks
    }
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号