Fork me on GitHub

Gradle构建学习

综述

LifeCycle

Initialization

Gradle supports single and multi-project builds. During the initialization phase, Gradle determines which projects are going to take part in the build, and creates a Project instance for each of these projects.

确定参与构建的项目,为每个项目创建一个project实例。Android中就是根据setting.grade中声明的项目来创建。具体步骤见下面CoreType-Project。

Configuration

During this phase the project objects are configured. The build scripts of all projects which are part of the build are executed.

执行每个项目自己的build scripts构建脚本。Android中就是执行个module中的build.grade

Execution

Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed. The subset is determined by the task name arguments passed to the gradle command and the current directory. Gradle then executes each of the selected tasks.

决定要执行的任务的子集(第二阶段中创建和配置的)。

CoreType

Project

Gradle对外的接口,通过这个接口来编程使用Gradle的各种功能。

Project和build.gradle文件是一对一关系,在Initialization时期,Gradle会为每个参与构建的项目创建一个project实例。步骤如下:

  1. 为本次构造创建一个Settings实例

  2. 读取settings.gradle来配置上面的Settings对象

  3. 通过上面的Settings对象来创建Project实例
  4. 执行每个项目的build.gradle来配置每个Project实例

Task

Project可以算是由一堆Task构成的集合。每个Task各司其职,比如编译代码、跑单元测试、压缩对齐等。

TaskContainer.create(java.lang.String))是用来添加Task的,通过TaskCollection.getByName(java.lang.String))也可以获取到某个具体的Task实例。

Plugins

Plugins可以用于模块化与项目配置复用。

Plugins can be applied using the PluginAware.apply(java.util.Map)) method, or by using the PluginDependenciesSpec plugins script block.

Android 编译流程

图例可以看之前的博客

这里主要基于Gradle的命令展开

用Gradle打个debug包的命令如下,—info是为了打印出详细信息 并输出到文件 方便观察

./gradlew –info assembleDebug > ~/Desktop/gradle.info

开始一步步看日志吧

Initialization

读取settings

1
2
3
4
Starting Build
Settings evaluated using settings file '/Users/wengxiaodong/AndroidStudioProjects/VideoLearn/settings.gradle'.
Projects loaded. Root project using build file '/Users/wengxiaodong/AndroidStudioProjects/VideoLearn/build.gradle'.
Included projects: [root project 'VideoLearn', project ':app']

Configuration

1
2
3
4
5
> Configure project :
Evaluating root project 'VideoLearn' using build file '/Users/wengxiaodong/AndroidStudioProjects/VideoLearn/build.gradle'.

> Configure project :app
Evaluating project ':app' using build file '/Users/wengxiaodong/AndroidStudioProjects/VideoLearn/app/build.gradle'.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
All projects evaluated.
Selected primary task 'assembleDebug' from project :
Tasks to be executed: [
task ':app:preBuild',
task ':app:preDebugBuild',
task ':app:compileDebugAidl', //编译AIDL
task ':app:compileDebugRenderscript',
task ':app:checkDebugManifest',
task ':app:generateDebugBuildConfig', //BuildConfig.java
task ':app:prepareLintJar',
task ':app:generateDebugSources',
task ':app:javaPreCompileDebug',
task ':app:mainApkListPersistenceDebug',
task ':app:generateDebugResValues', //Res
task ':app:generateDebugResources',
task ':app:mergeDebugResources',
task ':app:createDebugCompatibleScreenManifests',
task ':app:processDebugManifest', //Manifest
task ':app:processDebugResources',
task ':app:compileDebugJavaWithJavac', //javac编译
task ':app:compileDebugSources',
task ':app:mergeDebugShaders',
task ':app:compileDebugShaders',
task ':app:generateDebugAssets', //生成assets
task ':app:mergeDebugAssets', //合并assets
task ':app:checkDebugDuplicateClasses',
task ':app:mergeExtDexDebug', //dex
task ':app:mergeLibDexDebug',
task ':app:transformClassesWithDexBuilderForDebug',
task ':app:mergeProjectDexDebug',
task ':app:validateSigningDebug',
task ':app:signingConfigWriterDebug',
task ':app:mergeDebugJniLibFolders', //jni
task ':app:transformNativeLibsWithMergeJniLibsForDebug',
task ':app:transformNativeLibsWithStripDebugSymbolForDebug',
task ':app:processDebugJavaRes',
task ':app:transformResourcesWithMergeJavaResForDebug', //resources.arsc
task ':app:packageDebug', //打包
task ':app:assembleDebug' //apk
]

Execution

log太多自行查看 就是执行上面的task

参考

写给 Android 开发者的 Gradle 系列(一)基本姿势

通过gradle生成apk的过程

Gradle Build Language Reference

Android Plugin DSL Reference