Gradle 使用指南 -- Android DSL 扩展

本文档介绍了Gradle在Android构建中的DSL扩展,特别是AppExtension。内容包括Android Plugin提供的额外脚本块,如何操作Task,如applicationVariants、libraryVariants等,以及如何通过过滤器筛选对象。同时,提到了Android Plugin DSL Reference作为详细参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的博客原文地址

概述

在前面博客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扩展类型:

类型描述
AppExtensioncom.android.application 插件的扩展
LibraryExtensioncom.android.library 插件的扩展
TestExtensioncom.android.test 插件的扩展
FeatureExtensioncom.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)描述
buildToolsVersionSpecifies the version of the SDK Build Tools to use when building your project.
applicationVariantsReturns a collection of build variants that the app project includes.
compileSdkVersionSpecifies the API level to compile your project against. The Android plugin requires you to configure this property.
testVariantsReturns 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都适用)

这三个都会分别返回一个 ApplicationVariantLibraryVariantTestVariantFeatureVariant 对象的 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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒江蓑笠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值