Android Gradle 构建系统
Android 应用构建流程
Section titled “Android 应用构建流程”Android Studio 使用 Gradle(配合 Android Gradle Plugin,AGP)管理构建流程:
- 编译器将源码转换成 DEX 文件(Dalvik 可执行文件,包含在 Android 设备上运行的字节码;单个 DEX 方法总数上限为 65,536,即 64K 限制),并将其他内容转换成编译后的资源。
- APK 打包器将 DEX 文件与编译后的资源组合成 APK。
- APK 打包器使用调试或发布密钥库为 APK 签名。
- APK 打包器使用
zipalign对 APK 进行对齐优化,减少在设备上运行时占用的内存。
Gradle 核心配置文件
Section titled “Gradle 核心配置文件”Gradle 配置文件后缀为 .gradle,使用 Groovy 语言描述构建逻辑。
1. 顶层 build.gradle
Section titled “1. 顶层 build.gradle”位于项目根目录,定义所有模块共用的仓库、构建插件依赖与全局属性:
buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:4.0.0' }}
allprojects { repositories { google() jcenter() }}
ext { compileSdkVersion = 28 supportLibVersion = "28.0.0"}buildscript:配置 Gradle 自身所需的仓库与插件依赖(如 AGP)。allprojects:配置项目中所有子模块共用的仓库和依赖。ext:定义项目全局扩展属性。
2. 模块级 build.gradle
Section titled “2. 模块级 build.gradle”位于每个模块目录下,通过 apply plugin: 'com.android.application' 引入 Android 编译插件:
apply plugin: 'com.android.application'
android { compileSdkVersion 28 buildToolsVersion "29.0.2"
defaultConfig { applicationId 'com.example.myapp' minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" }
buildTypes { release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
flavorDimensions "tier" productFlavors { free { dimension "tier" applicationId 'com.example.myapp.free' } paid { dimension "tier" applicationId 'com.example.myapp.paid' } }}
dependencies { implementation project(":lib") implementation 'com.android.support:appcompat-v7:28.0.0' implementation fileTree(dir: 'libs', include: ['*.jar'])}compileSdkVersion:指定编译应用的 Android API 级别;buildToolsVersion可选,默认使用本地最新版本。defaultConfig:所有构建变体的默认基准配置。buildTypes:编译类型,默认包含debug和release;在release中开启minifyEnabled true与shrinkResources true可启用 R8 代码缩减混淆与资源缩减。productFlavors:配置多产品变种,可覆盖defaultConfig中的配置。
3. 属性文件
Section titled “3. 属性文件”gradle.properties:定义项目全局 Gradle 设置(查看官方文档)。local.properties:定义本地环境属性(如本机 Android SDK 路径)。
源代码集(Source Sets)优先级
Section titled “源代码集(Source Sets)优先级”每个模块的 main 目录存放所有构建变体共用的代码与资源,也可为特定变体或类型创建专属目录。例如构建 fullDebug 版本时,Gradle 会合并 src/fullDebug/、src/debug/、src/full/ 与 src/main/。
若多个源代码集包含同名文件,优先级从高到低如下:
构建变体 > 构建类型 > 产品变种 > 主源代码集 > 库依赖项
依赖项类型与配置
Section titled “依赖项类型与配置”三种常见依赖方式
Section titled “三种常见依赖方式”- 模块依赖:
implementation project(':mylibrary') - 文件依赖:
implementation fileTree(dir: 'libs', include: ['*.jar']) - 远程依赖:
implementation 'com.example.android:app-magic:12.3'
五种依赖配置关键词
Section titled “五种依赖配置关键词”| 配置关键词 | 作用与特点 |
|---|---|
implementation |
编译并打包到输出,没有传递性 |
api |
与 implementation 类似,但有传递性 |
compileOnly |
只编译、不打包(常用于解决依赖冲突、减少冗余) |
runtimeOnly |
不编译、只打包(适合组件化代码隔离) |
annotationProcessor |
添加对编译期注解处理器的依赖 |
若只想为特定变体添加依赖,可在配置前加上变体前缀(如 freeImplementation、testImplementation、androidTestImplementation);针对“产品变种 + 构建类型”的组合变体(如 freeDebug),需先在 configurations {} 中初始化配置名称(如 freeDebugRuntimeOnly {})。
Gradle 命令行与 Task 语法
Section titled “Gradle 命令行与 Task 语法”- 常用命令行选项:
-D:指定系统变量-P:指定项目变量-q:安静模式--init-script init.gradle:指定初始化脚本
- 常用 Task 语法:
tasks.register('hello'):注册新 Tasktasks.named('hello'):在已有 Task 中添加额外动作doFirst/doLast:在 Task 开头和结尾添加动作defaultTasks 'hello':指定未传入 Task 参数时的默认执行 Task