从上面的图中可以看出,Gradle 的生命周期分为三个阶段,初始化阶段,配置阶段,执行阶段。具体的生命周期流程可以在 AndroidStudio 的控制台中输入如下命令可以看出:
./gradlew build
在执行该命令之后,可以在控制台上看到很多日志输出,大体有Initialization -> Configuration -> Execution 在三个阶段,和图中的介绍是一致的。 同时,我们可以看到有很多的Task,我们在控制台中输入的 build 就是一个task,该task依赖了很多其他的task,只有其他的task执行完成之后,才会执行build的task任务。
Gradle的生命周期的三大流程的作用:
1. 初始化阶段:settings.gradle 文件执行,该阶段用于确定整个工程有多少个 Project
2. 配置阶段:解析各个Project中的 task,生成有向无环图,并建立各个task之间的依赖关系
3. 执行阶段:执行具体的task之前先执行其所依赖的task
我们可以监听每个阶段,然后做些Hack操作,比如在初始化阶段做些事情,在配置阶段后做些事情,在执行阶段后做些事情,比如统计整个build过程的时间大小。
常见的gradle命令:
./gradlew clean // clean 清除build目录
./gradlew build // build 项目
./gradlew release // 编译release版本
./gradlew tasks //查看所有的tasks
./gradlew clean 是最简单且执行最快的task,可以很快的得知 gradle 的生命周期。
Gradle生命周期监听的几个方法
//settings.gradle
include ':buildSrc'
include ':app', ':repository:viewpagerindicator', ':oema', ':oemb'
println '初始化阶段开始....'
// 根目录下的build.gradle中添加 apply from: "xx" 相当于在xml中添加include
apply from: "config.gradle"
//config.gradle
//初始化之后,配置阶段开始前的监听回调
this.beforeEvaluate {
println "配置阶段开始前..."
}
//配置阶段完成以后,执行之前的监听回调
this.afterEvaluate {
println "配置阶段结束之后..."
}
//gradle执行完毕后的回调监听
this.gradle.buildFinished {
println '执行阶段结束之后..'
}
注意:这里的this是project对象,表示是一个项目的对象,它有好多的API,后面会介绍到。
zfz:GradleLearn zhangfengzhou$ ./gradlew clean
Parallel execution is an incubating feature.
初始化阶段开始....
> Configure project :
-----------
Root Project
-----------
Root Project :app
....
配置阶段结束之后...
> Configure project :app
...
BUILD SUCCESSFUL in 5s
6 actionable tasks: 1 executed, 5 up-to-date
执行阶段结束之后..
其他类型的监听
//gradle其他的监听回调
this.gradle.beforeProject {
println 'beforeProject...'
}
this.gradle.afterProject {
println 'afterProject...'
}
this.gradle.addBuildListener()
this.gradle.addListener()
/**
* Adds a {@link BuildListener} to this Build instance.
*
* The listener is notified of events which occur during the execution of the build.
*
* @param buildListener The listener to add.
*/
void addBuildListener(BuildListener buildListener);
BuildListener 接口回调的方法
import org.gradle.api.initialization.Settings;
import org.gradle.api.invocation.Gradle;
/**
* <p>A {@code BuildListener} is notified of the major lifecycle events as a build is executed.</p>
*
* @see org.gradle.api.invocation.Gradle#addListener(Object)
*/
public interface BuildListener {
/**
* <p>Called when the build is started.</p>
*
* @param gradle The build which is being started. Never null.
*/
void buildStarted(Gradle gradle);
/**
* <p>Called when the build settings have been loaded and evaluated. The settings object is fully configured and is
* ready to use to load the build projects.</p>
*
* @param settings The settings. Never null.
*/
void settingsEvaluated(Settings settings);
/**
* <p>Called when the projects for the build have been created from the settings. None of the projects have been
* evaluated.</p>
*
* @param gradle The build which has been loaded. Never null.
*/
void projectsLoaded(Gradle gradle);
/**
* <p>Called when all projects for the build have been evaluated. The project objects are fully configured and are
* ready to use to populate the task graph.</p>
*
* @param gradle The build which has been evaluated. Never null.
*/
void projectsEvaluated(Gradle gradle);
/**
* <p>Called when the build is completed. All selected tasks have been executed.</p>
*
* @param result The result of the build. Never null.
*/
void buildFinished(BuildResult result);
}
遇到的问题:
Could not HEAD 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-stdlib-jre8/1.2.0/kotlin-stdlib-jre8-1.2.0.pom'. Received status code 502 from server: Bad Gateway
Disable Gradle 'offline mode' and sync project
经过查找,有网友提示是插件版本不对:
kotlin-stdlib-jre8 自 1.2.0 起被弃用,应替换为 kotlin-stdlib-jdk8
更新插件版本之后就好了
classpath 'com.android.tools.build:gradle:3.5.2'
//kotlin插件感觉没用
//classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50"
//另外需要更新下gradle-warpper.properties中的版本
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip