Gradle execution time series details, gradle time series details
What is Gradle?
A flexible generic build tool like Ant
A customizable contract-based framework like maven
Supports powerful multi-project construction
Powerful dependency management (based on ApacheIvy)
Support existing maven and ivy Repositories
Supports the management of transmission dependencies without the need for remote repositories, pom. xml, Or ivy configuration files.
Ant-style tasks and build are preferred.
Groovy-based build script
There are rich domain models to describe your build
Use Gradle wrapper
If you have installed Gradle locally, you can use the gradle command to build it directly. If it is not installed locally, it can be built through gradle wrapper, with./gradlew for Linux and MAC, while gradlew for Windows, and gradle version can be configured in Gradle/gradle-wrapper.properties.
Execution sequence of Gradle scripts
The execution of the Gradle script is divided into three processes:
Initialization
Analyze which modules will be built and create corresponding project instances for each module. At this time, the settings. gradle file will be parsed.
Configuration: process build scripts of all modules, process dependencies, attributes, and so on. In this case, the build of each module. the gradle file will be parsed and configured. At this time, a linked list of the entire task will be built (the linked list here only refers to the set of tasks with dependencies, not the linked list of data structures ).
Run: execute a specific task according to the task linked list. All other tasks on which the task depends will be executed in advance.
The following is a detailed example. Here we still use the VirtualAPK open-source project for demonstration.
Take its host as an example. The host has the following modules:
In fact, buildSrc is virtualapk-gradle-plugin. To facilitate debugging, I renamed it buildSrc. Their dependencies are as follows:
The app module depends on CoreLibrary and buildSrc, and CoreLibrary depends on AndroidStub. For better understanding, add the following log.
/***** Settings.gradle *****/println "settings start"include ':app'include ':CoreLibrary'include ':AndroidStub'println "settings end"
/***** VirtualAPK.gradle *****/println "virtualapk start"allprojects { repositories { mavenCentral() jcenter() }}task clean(type: Delete) { delete rootProject.buildDir}println "virtualapk end"
/***** app.gradle *****/println "app config start"apply plugin: 'com.android.application'apply plugin: 'com.didi.virtualapk.host'dependencies { compile project (":CoreLibrary")}project.afterEvaluate { println "app evaluate start" println "app evaluate end"}println "app config end"
/***** CoreLibrary.gradle *****/apply plugin: 'com.android.library'println "corelib config start"dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.2.0' provided project(':AndroidStub')}apply from: 'upload.gradle'println "corelib config end"
/***** AndroidStub.gradle *****/println "androidstub config start"dependencies { compile 'com.android.support:support-annotations:22.2.0'}println "androidstub config end"
/***** buildSrc *****/public class VAHostPlugin implements Plugin
{ @Override public void apply(Project project) { println "VAHostPlugin config start" project.afterEvaluate { println "VAHostPlugin evaluate start" project.android.applicationVariants.each { ApplicationVariant variant -> generateDependencies(variant) backupHostR(variant) backupProguardMapping(variant) } println "VAHostPlugin evaluate end" } println "VAHostPlugin config end" }}
Execute a task at will, for example./Gradlew clean, the following logs will be output. You should be able to understand the execution sequence of the Gradle script by comparing the logs.
VirtualAPK renyugang$ ./gradlew cleansettings startsettings endvirtualapk startvirtualapk endandroidstub config startandroidstub config endIncremental java compilation is an incubating feature.corelib config startcorelib config endapp config startVAHostPlugin config startVAHostPlugin config endapp config endVAHostPlugin evaluate startVAHostPlugin evaluate endapp evaluate startapp evaluate end:clean:AndroidStub:clean:CoreLibrary:clean:app:cleanBUILD SUCCESSFULTotal time: 12.381 secs
As you can see, Gradle follows the following sequence during execution:
1. parse settings. gradle to obtain the module information. This is the initialization phase;
2. Then, configure each module. The task is not executed during configuration;
3. After the configuration is complete, there will be an important callbackProject. afterEvaluate, which indicates that all modules have been configured and can be executed;
4. Run the specified task.
Note: If you have registered multipleProject. afterEvaluate callback, the execution order is equivalent to the registration order. In the above example, because the callback registration in buildSrc is earlier, it is also executed first.