文章目录
概览
Android Studio 构建系统以Gradle为基础,Android Gradle plugin 添加了几项专用于构建 Android 应用的功能。
Gradle 和 Android 插件独立于 Android Studio 运行。这意味着,您可以在 Android Studio 内、计算机上的命令行或未安装 Android Studio 的计算机(如持续集成服务器)上构建 Android 应用。命令行构建应用
buildscript {
repositories {
// Gradle 4.1 and higher include support for Google's Maven repo using
// the google() method. And you need to include this repo to download
// Android Gradle plugin 3.0.0 or higher.
google()
jcenter()
...
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
下表列出了各个 Android Gradle 插件版本所需的 Gradle 版本。
Plugin version | Gradle version | Build Tools |
---|---|---|
1.0.0 - 1.1.3 | 2.2.1 - 2.3 | |
1.2.0 - 1.3.1 | 2.2.1 - 2.9 | |
1.5.0 | 2.2.1 - 2.13 | |
2.0.0 - 2.1.2 | 2.10 - 2.13 | |
2.1.3 - 2.2.3 | 2.14.1+ | |
2.3.0+ | 3.3+ | |
3.0.0+ | 4.1+ | 26.0.2+ |
3.1.0+ | 4.4+ | |
3.2.0 - 3.2.1 | 4.6+ | |
3.3.0 - 3.3.2 | 4.10.1+ | |
3.4.0 - 3.4.1 | 5.1.1+ | |
3.5.0+ | 5.4.1 - 5.6.4 |
3.5.0 (2019/8)
Android Gradle 插件 3.5.0 和 Android Studio 3.5 是一个重大版本,同时也是 Project Marble 计划的成果,该计划主要围绕 Android 开发者工具的三大核心领域进行改进,分别是系统运行状况
、功能完善
和错误修复
。
This version of the Android plugin requires the following:
- Gradle 5.4.1 或更高版本。
- SDK Build Tools 28.0.3 或更高版本。
3.4.0 (2019/4)
This version of the Android plugin requires the following:
- Gradle 5.1.1 or higher.
- SDK Build Tools 28.0.3 or higher.
3.3.0 (2019/1)
3.2.0 (2018/9)
3.1.0 (2018/3)
3.0.0 (2017/10)
此版本的 Android 插件需要使用以下工具:
- Gradle 4.1 或更高版本。
- Build Tools 26.0.2 或更高版本。经过此次更新,您无需再指定 Build Tools 的版本。默认情况下,该插件会使用所需的最低版本。因此,您现在可以移除 android.buildToolsVersion 属性了。
Android Gradle 插件 3.0.0 包含各种旨在解决大型项目性能问题的更改。
优化
- Gradle 通过不重新编译无法访问该依赖项 API 的模块来加快编译速度。您应利用 Gradle 的新依赖项配置(implementation、api、compileOnly 和 runtimeOnly)限制哪些依赖项会将其 API 泄露给其他模块。
- 借助每个类的 dex 处理,可加快增量编译速度。每个类现在都会编译成单独的 DEX 文件,并且只会对修改过的类重新进行 dex 处理。
- 通过优化某些任务来使用缓存的输出,从而加快编译速度。要利用此项优化,您需要先启用 Gradle 编译缓存。
- 使用 AAPT2(现已默认处于启用状态)改进了增量资源处理。