Gradle 使用指南 -- Android(安卓)DSL 扩展
我的博客原文地址
概述
在前面博客Gradle 使用指南 – 基础配置 中介绍了一些 Gradle 配置的基本命令,其中有一个名称为 android
的函数不知道有没有引起大家的注意:
android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { ...... } buildTypes { release { ...... } }}
该函数接收闭包作为参数,然而其实在Gradle的API文档中是不存在这个函数的。那么 android
脚本怎么会出现在这里呢? 答案就是最上面的 apply plugin: 'com.android.application'
。这个插件提供了 Android 构建所需要的各种 script。
Android Plugin DSL Reference:Android 插件 DSL 扩展文档,各个版本的都有。
下面简单介绍一些 Android Plugin 对 Gradle 一些扩展的知识,没有涉及的地方可以参考官方文档。
扩展介绍
下面列出了各个插件使用的Gradle扩展类型:
类型 | 描述 |
---|---|
AppExtension | 对 com.android.application 插件的扩展 |
LibraryExtension | 对 com.android.library 插件的扩展 |
TestExtension | 对 com.android.test 插件的扩展 |
FeatureExtension | 对 com.android.feature 插件的扩展,Gradle 3.0新增 |
下面是 Android Plugin 一些通用的 Script blocks,以上四种类型的扩展除了对下面的支持之外,还有自己类型的一些扩展。
方法(Script blocks) | 描述 |
---|---|
aaptOptions { } | Specifies options for the Android Asset Packaging Tool (AAPT) |
adbOptions { } | Specifies options for the Android Debug Bridge (ADB), such as APK installation options |
buildTypes { } | Encapsulates all build type configurations for this project. |
compileOptions { } | Specifies Java compiler options, such as the language level of the Java source code and generated bytecode. |
dataBinding { } | Specifies options for the Data Binding Library. |
defaultConfig { } | Specifies defaults for variant properties that the Android plugin applies to all build variants. |
dexOptions { } | Specifies options for the DEX tool, such as enabling library pre-dexing. |
externalNativeBuild { } | Configures external native build using CMake or ndk-build. |
jacoco { } | Configuring JaCoCo using this block is deprecated. |
lintOptions { } | Specifies options for the lint tool. |
packagingOptions { } | Specifies options and rules that determine which files the Android plugin packages into your APK. |
productFlavors { } | Encapsulates all product flavors configurations for this project. |
signingConfigs { } | Encapsulates signing configurations that you can apply to BuildType and ProductFlavor configurations. |
sourceSets { } | Encapsulates source set configurations for all variants. |
splits { } | Specifies configurations for building multiple APKs or APK splits. |
testOptions { } | Specifies options for how the Android plugin should run local and instrumented tests. |
AppExtension
下面仅列觉一下 AppExtension
的部分扩展:
方法(Script blocks) | 描述 |
---|---|
buildToolsVersion | Specifies the version of the SDK Build Tools to use when building your project. |
applicationVariants | Returns a collection of build variants that the app project includes. |
compileSdkVersion | Specifies the API level to compile your project against. The Android plugin requires you to configure this property. |
testVariants | Returns a collection of Android test build variants. |
… | … |
操作 Task
Android项目中会有大量相同的task,并且它们的名字基于Build Types和Product Flavor生成。
android 对象有两个属性:
- applicationVariants(只适用于app plugin)
- libraryVariants(只适用于library plugin)
- featureVariants (只适用于feature plugin)
- testVariants(三个plugin都适用)
这三个都会分别返回一个 ApplicationVariant
、LibraryVariant
、 TestVariant
和 FeatureVariant
对象的 DomainObjectCollection
。
ApplicationVariant 源码
DomainObjectCollection
继承自 Collection
,可以查看 DomainObjectCollection文档说明
注意: 使用这四个
collection
中的其中一个都会触发生成所有对应的task。这意味着使用collection
之后不需要更改配置。
DomainObjectCollection
可以直接访问所有对象,或者通过过滤器进行筛选。
android.applicationVariants.all { variant -> def name = variant.name println "android "+name }
DomainObjectCollection
可以直接访问所有对象,或者通过过滤器进行筛选。
参考文档
http://google.github.io/android-gradle-dsl/current/
更多相关文章
- Android(安卓)3.0 r1中文API文档(104) —— ViewTreeObserver
- Android(安卓)Gradle插件入门
- 有米Android(安卓)SDK开发者文档
- 去掉Android(安卓)USB授权框
- 学习android前的环境配置,准备工作,安装android-sdk atd myeclipse
- Android的开发环境的搭建
- android相关技术文档汇总
- Unity与Android交互-扩展Unity的MainActivity
- Android(安卓)API中文文档(111) ―― MailTo