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

2023-03-30 17:11:37 浏览数 (1)

文章目录

  • 一、BuildListener 构建监听器
  • 二、TaskExecutionGraphListener 任务执行图监听器

Android Plugin DSL Reference 参考文档 :

  • Android Studio 构建配置官方文档 : https://developer.android.google.cn/studio/build
  • 添加构建依赖项 参考文档 : https://developer.android.google.cn/studio/build/dependencies
  • Android Gradle 插件配置与 Gradle 配置关联 : 【Android Gradle 插件】Gradle 依赖管理 ① ( org.gradle.api.Project 配置 | Android Gradle 插件配置与 Gradle 配置关联 ) ★
  • Android Gradle 插件文档主页 : https://google.github.io/android-gradle-dsl/2.3/
  • BaseExtension 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.BaseExtension.html
  • AppExtension ( build.gradle#android 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.AppExtension.html
  • build.gradle#android 模块配置文档 : android-gradle-dsl/2.3/com.android.build.gradle.AppExtension.html
  • ProductFlavor ( build.gradle#android#defaultConfig 配置 ) 文档 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.ProductFlavor.html
  • ProductFlavor#externalNativeBuild 配置 ( build.gradle#android#defaultConfig#externalNativeBuild 配置 ) : com.android.build.gradle.internal.dsl.ProductFlavor:externalNativeBuild
  • ExternalNativeBuildOptions ( build.gradle#android#defaultConfig#externalNativeBuild 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.ExternalNativeBuildOptions.html
  • NdkBuildOptions ( build.gradle#android#defaultConfig#externalNativeBuild#ndkBuild 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.NdkBuildOptions.html
  • CmakeOptions ( build.gradle#android#defaultConfig#externalNativeBuild#cmake 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.CmakeOptions.html
  • JackOptions ( build.gradle#android#defaultConfig#jackOptions 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.JackOptions.html
  • AnnotationProcessorOptions ( 注解处理器配置 ) ( build.gradle#android#defaultConfig#javaCompileOptions配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.AnnotationProcessorOptions.html
  • NdkOptions ( build.gradle#android#defaultConfig#ndk 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.NdkOptions.html
  • NDK 参考文档 : Add C and C Code to Your Project.
  • VectorDrawablesOptions ( build.gradle#android#defaultConfig#vectorDrawables 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.VectorDrawablesOptions.html
  • BuildType ( build.gradle#android#buildTypes 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.BuildType.html
  • SigningConfig ( build.gradle#android#signingConfigs 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.SigningConfig.html
  • AaptOptions ( build.gradle#android#aaptOptions 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.AaptOptions.html
  • AndroidSourceSets ( build.gradle#android#sourceSets 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.api.AndroidSourceSet.html
  • AndroidSourceDirectorySet ( build.gradle#android#sourceSets#aidl/assets/java/jni/jniLibs 配置 ) 文档位置 :android-gradle-dsl/2.3/com.android.build.gradle.api.AndroidSourceDirectorySet.html
  • AndroidSourceFile ( build.gradle#android#sourceSets#manifest 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.api.AndroidSourceFile.html
  • DataBindingOptions ( build.gradle#android#dataBinding ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.DataBindingOptions.html
  • DexOptions ( build.gradle#android#dexOptions ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.DexOptions.html
  • LintOptions ( build.gradle#android#lintOptions ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.LintOptions.html
  • PackagingOptions ( build.gradle#android#packagingOptions ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.PackagingOptions.html
  • Splits ( build.gradle#android#splits ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.Splits.html
  • AbiSplitOptions ( build.gradle#android#splits#abi ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.AbiSplitOptions.html
  • LanguageSplitOptions ( build.gradle#android#splits#language ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.LanguageSplitOptions.html
  • DensitySplitOptions ( build.gradle#android#splits#density ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.DensitySplitOptions.html
  • TestOptions ( build.gradle#android#testOptions ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.TestOptions.html
  • Android 测试相关文档 : https://developer.android.google.cn/training/testing
  • UnitTestOptions ( build.gradle#android#testOptions#unitTests ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.TestOptions.UnitTestOptions.html
  • org.gradle.api.tasks.testing.Test 单元测试配置类 : https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html
  • LibraryExtension ( Library 依赖库的 build.gradle#android 配置 ) 文档 : android-gradle-dsl/2.3/com.android.build.gradle.LibraryExtension.html
  • 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.artifacts.dsl.DependencyHandler 配置 ( build.gradle#dependencies#implementation 配置 ) : https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/dsl/DependencyHandler.html
  • 添加构建依赖项 参考文档 : https://developer.android.google.cn/studio/build/dependencies
  • org.gradle.api.Task 配置 ( Gradle 任务类型 ) 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/Task.html
  • org.gradle.api.tasks.TaskContainer配置 ( Gradle 任务容器类型 ) 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/TaskContainer.html
  • org.gradle.api.DefaultTask 配置 ( Gradle 自定义任务类父类 ) 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/DefaultTask.html
  • org.gradle.api.tasks.Delete 自带任务 ( clean 任务类型 ) 文档 : https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Delete.html
  • org.gradle.api.tasks.bundling.Zip 自带任务 ( 任务类型 ) 文档 :https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Zip.html
  • Gradle 自带任务查询界面 ( 搜索 Task Types ) : https://docs.gradle.org/current/dsl/#N1045C
  • org.gradle.api.invocation.Gradle 文档 ( 可设置 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
  • org.gradle.execution.TaskExecutionGraphListener 构建监听器 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/execution/TaskExecutionGraphListener.html

一、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#addBuildListener 或 Gradle#addListener 函数 , 可设置 BuildListener 监听器 ;

BuildListener 监听器原型如下 :

代码语言:javascript复制
@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);
}

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

代码示例 :

代码语言:javascript复制
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) {
        // 构建完成时回调的函数
    }
})

二、TaskExecutionGraphListener 任务执行图监听器


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

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

Gradle 任务执行图 确定后 ,

TaskExecutionGraphListener 监听器原型如下 :

代码语言:javascript复制
@EventScope(Build.class)
public interface TaskExecutionGraphListener {
    void graphPopulated(TaskExecutionGraph var1);
}

TaskExecutionGraphListener#graphPopulated 函数 :

代码语言:javascript复制
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.
        任务图 . 不可为空 .

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

代码语言:javascript复制
// Gradle 任务执行图监听器
gradle.addListener(new TaskExecutionGraphListener() {
    @Override
    void graphPopulated(TaskExecutionGraph taskExecutionGraph) {
        println taskExecutionGraph.allTasks
    }
})

0 人点赞