Android插件基类扩展

所有Android插件的基本扩展。

你不需要直接使用这个,你可以选择下面几个合适的直接使用

以下内容将Android插件应用于应用程序的模块级build.gradle文件:

// Applies the application plugin and makes the 'android' block available to specify
// Android-specific build options.
apply plugin: 'com.android.application'

学习如何构建一个安卓应用程序,查看这里。 Projects Overview.

属性(Properties)

属性Property Description 描述
aaptOptions Specifies options for the Android Asset Packaging Tool (AAPT). 指定Android资产打包工具(AAPT)的选项。
adbExecutable Returns a path to the Android Debug Bridge (ADB) executable from the Android SDK. 从Android SDK返回一个指向Android Debug Bridge(ADB)可执行文件的路径。
adbOptions Specifies APK install options for the Android Debug Bridge (ADB). 指定APK安装选项[Android调试桥(ADB)]
buildToolsVersion Specifies the version of the SDK Build Tools to use when building your project. 指定构建项目时要使用的SDK Build Tools的版本。
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. 指定Java编译器选项,例如Java源代码的语言级别和生成的字节码。
compileSdkVersion Specifies the API level to compile your project against. The Android plugin requires you to configure this property. 指定用于编译项目的API级别。 Android插件要求您配置此属性。
dataBinding Specifies options for the Data Binding Library. 指定[数据绑定库]的选项
defaultConfig Specifies defaults for variant properties that the Android plugin applies to all build variants. 指定Android插件应用于所有构建变体的变体属性的默认值。
defaultPublishConfig Specifies the version of the module to publish externally. This property is generally useful only to library modules that you intend to publish to a remote repository, such as Maven. 指定要在外部发布的模块的版本。该属性通常仅对打算发布到远程存储库(如Maven)的库模块有用。
dexOptions Specifies options for the DEX tool, such as enabling library pre-dexing. 指定DEX工具的选项,例如启用库预分离。
externalNativeBuild Specifies options for external native build using CMake or ndk-build. 使用CMake或ndk-build指定外部原生构建的选项。
flavorDimensionList Specifies the names of product flavor dimensions for this project. 指定此项目的产品风味维度的名称。
generatePureSplits incubating,Specifies whether to build APK splits or multiple APKs from configurations in the Splitsblock. 孵化,指定是否根据Splits中的配置构建APK分裂或多个APK 嵌段。
jacoco deprecated,Configure JaCoCo version that is used for offline instrumentation and coverage report. 弃用,配置用于脱机检测和覆盖率报告的JaCoCo版本。
lintOptions Specifies options for the lint tool. 指定lint工具的选项
ndkDirectory The path to the Android NDK that Gradle uses for this project. NDK目录
packagingOptions Specifies options and rules that determine which files the Android plugin packages into your APK. 指定用于确定Android插件打包到APK中的哪些文件的选项和规则。
productFlavors Encapsulates all product flavors configurations for this project. 封装此项目的所有产品口味配置。
resourcePrefix Specifies the module's resource prefix to Android Studio for editor features, such as Lint checks. This property is useful only when using Android Studio. 指定模块的Android Studio编辑器功能的资源前缀,例如Lint检查。此属性仅在使用Android Studio时有用。
sdkDirectory The path to the Android SDK that Gradle uses for this project. Gradle用于此项目的Android SDK的路径。
signingConfigs Encapsulates signing configurations that you can apply to BuildType and ProductFlavor configurations. 封装可应用于BuildType和ProductFlavor配置的签名配置。
sourceSets Encapsulates source set configurations for all variants. 封装所有变体的源设置配置。
splits Specifies configurations for building multiple APKs or APK splits. 指定[构建多个APK的配置]
testOptions Specifies options for how the Android plugin should run local and instrumented tests. 指定Android插件如何运行本地和仪器测试的选项。
variantFilter Specifies variants the Android plugin should include or remove from your Gradle project. 指定Android插件应包含或从您的Gradle项目中删除的变体。

方法 Methods

Method Description 描述
flavorDimensions(dimensions)` Specifies the names of product flavor dimensions for this project. 指定此项目的产品风味维度的名称。
useLibrary(name)` Includes the specified library to the classpath. 将指定的库包含到类路径中。
useLibrary(name, required)` Includes the specified library to the classpath. 将指定的库包含到类路径

脚本块 Script blocks

Block Description 描述
aaptOptions Specifies options for the Android Asset Packaging Tool (AAPT). 指定Android资产打包工具(AAPT)的选项。
adbOptions Specifies options for the Android Debug Bridge (ADB), such as APK installation options. 指定Android调试桥ADB的选项,例如APK安装选项。
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. 指定Java编译器选项,例如Java源代码的语言级别和生成的字节码。
dataBinding Specifies options for the Data Binding Library. 指定[数据绑定库]的选项
defaultConfig Specifies defaults for variant properties that the Android plugin applies to all build variants. 指定Android插件应用于所有构建变体的变体属性的默认值。
dexOptions Specifies options for the DEX tool, such as enabling library pre-dexing. 指定DEX工具的选项,例如启用库预分离。
externalNativeBuild Configures external native build using CMake or ndk-build. 使用CMake或ndk-build配置外部原生构建
jacoco deprecated,Configuring JaCoCo using this block is deprecated. 弃用,使用此块配置JaCoCo已弃用。
lintOptions Specifies options for the lint tool. 指定lint工具的选项。
packagingOptions Specifies options and rules that determine which files the Android plugin packages into your APK. 指定用于确定Android插件打包到APK中的哪些文件的选项和规则。
productFlavors Encapsulates all product flavors configurations for this project. 封装此项目的所有产品口味配置。
signingConfigs Encapsulates signing configurations that you can apply to BuildType and ProductFlavor configurations. 封装您可以应用于BuildType和ProductFlavor配置的签名配置。
sourceSets Encapsulates source set configurations for all variants. 封装所有变体的源设置配置。
splits Specifies configurations for building multiple APKs or APK splits. 指定用于构建多个APK或APK拆分的配置。
testOptions Specifies options for how the Android plugin should run local and instrumented tests. 指定Android插件如何运行本地和仪器测试的选项。

详细属性 Property details

AaptOptions aaptOptions

Specifies options for the Android Asset Packaging Tool (AAPT).
指定Android资产打包工具(AAPT)的选项。

File adbExecutable

Returns a path to the Android Debug Bridge (ADB) executable from the Android SDK.
从Android SDK返回Android调试桥(ADB)可执行文件的路径。

AdbOptions adbOptions

Specifies APK install options for the Android Debug Bridge (ADB).
为Android调试桥(ADB)指定APK安装选项

String buildToolsVersion

Specifies the version of the SDK Build Tools to use when building your project.
指定构建项目时使用的SDK构建工具的版本。

When using Android plugin 3.0.0 or later, configuring this property is optional. By default, the plugin uses the minimum version of the build tools required by the version of the plugin you're using. To specify a different version of the build tools for the plugin to use, specify the version as follows:

当使用Android插件3.0.0或更高版本时,配置此属性是可选的。默认情况下,插件使用您使用的插件版本所需的最低版本的构建工具。要为插件指定不同版本的构建工具,请按如下所示指定版本:

// Specifying this property is optional.
buildToolsVersion "<build-tools-version>"

For a list of build tools releases, read the release notes.
有关构建工具发布的列表,请阅读[发行说明]

Note that the value assigned to this property is parsed and stored in a normalized form, so reading it back may give a slightly different result.
请注意,分配给此属性的值会被解析并以规范化的形式存储,因此读回它可能会给出稍微不同的结果。

NamedDomainObjectContainer<BuildType> buildTypes

Encapsulates all build type configurations for this project.
封装此项目的所有构建类型配置。

Unlike using ProductFlavor to create different versions of your project that you expect to co-exist on a single device, build types determine how Gradle builds and packages each version of your project. Developers typically use them to configure projects for various stages of a development lifecycle. For example, when creating a new project from Android Studio, the Android plugin configures a 'debug' and 'release' build type for you. By default, the 'debug' build type enables debugging options and signs your APK with a generic debug keystore. Conversely, The 'release' build type strips out debug symbols and requires you to create a release key and keystore for your app. You can then combine build types with product flavors to create build variants.

与使用ProductFlavor创建不同版本的项目不同您希望在单个设备上共存,构建类型决定了Gradle如何构建和打包项目的每个版本。开发人员通常使用它们为开发生命周期的各个阶段配置项目。例如,当从Android Studio创建新项目时,Android插件会为您配置“调试”和“发布”构建类型。默认情况下,“调试”构建类型启用调试选项并使用通用调试密钥库在APK上签名。相反,'release'构建类型会去掉调试符号,并且需要为创建释放键和密钥库你的应用。然后,您可以将构建类型与产品风格结合起来创建构建变体

CompileOptions compileOptions

Specifies Java compiler options, such as the language level of the Java source code and generated bytecode.
指定Java编译器选项,例如Java源代码的语言级别和生成的字节码。

String compileSdkVersion

Specifies the API level to compile your project against. The Android plugin requires you to configure this property.
指定用于编译项目的API级别。 Android插件要求您配置此属性。

This means your code can use only the Android APIs included in that API level and lower. You can configure the compile sdk version by adding the following to the android block: compileSdkVersion 26.

这意味着您的代码只能使用该API级别及更低版本中包含的Android API。您可以通过将以下内容添加到android块:compileSdkVersion 26来配置编译SDK版本。

You should generally use the most up-to-date API level available. If you are planning to also support older API levels, it's good practice to use the Lint tool to check if you are using APIs that are not available in earlier API levels.

您通常应该使用最新的API级别。如果您打算还支持较旧的API级别,则使用Lint工具是一种很好的做法,以检查您是否使用不是在较早的API级别可用。

The value you assign to this property is parsed and stored in a normalized form, so reading it back may return a slightly different value.
分配给此属性的值将被解析并以标准化形式存储,因此读回它可能会返回稍微不同的值。

DataBindingOptions dataBinding

Specifies options for the Data Binding Library.
指定[数据绑定库]的选项

Data binding helps you write declarative layouts and minimize the glue code necessary to bind your application logic and layouts.
数据绑定可帮助您编写声明式布局,并最大限度地减少绑定应用程序逻辑和布局所需的胶合代码。

DefaultConfig defaultConfig

Specifies defaults for variant properties that the Android plugin applies to all build variants.
指定Android插件应用于所有构建变体的变体属性的默认值。

You can override any defaultConfig property when configuring product flavors.
您可以在[配置产品口味]时覆盖任何defaultConfig属性。

String defaultPublishConfig

Specifies the version of the module to publish externally. This property is generally useful only to library modules that you intend to publish to a remote repository, such as Maven.
指定要在外部发布的模块的版本。该属性通常仅对打算发布到远程存储库(如Maven)的库模块有用。

If you don't configure this property, the Android plugin publishes the release version of the module by default. If the module configures product flavors, you need to configure this property with the name of the variant you want the plugin to publish, as shown below:
如果你不配置这个属性,Android插件默认发布模块的发布版本。如果模块配置了[产品风格],则需要使用您希望插件的变体名称来配置此属性发布,如下所示:

// Specifies the 'demoDebug' build variant as the default variant
// that the plugin should publish to external consumers.
defaultPublishConfig 'demoDebug'

If you plan to only consume your library module locally, you do not need to configure this property. Android plugin 3.0.0 and higher use variant-aware dependency resolution to automatically match the variant of the producer to that of the consumer. That is, when publishing a module to another local module, the plugin no longer respects this property when determining which version of the module to publish to the consumer.
如果你打算只在本地使用你的库模块,你不需要配置这个属性。 Android插件3.0.0及更高版本使用[变体感知依赖性解析]自动匹配生产者的变种与消费者的变种。也就是说,当发布一个模块到另一个本地模块时,插件在决定发布给消费者的哪个版本的模块时不再考虑这个属性。

DexOptions dexOptions

Specifies options for the DEX tool, such as enabling library pre-dexing.
指定DEX工具的选项,例如启用库预分离。

Experimenting with DEX options tailored for your workstation may improve build performance. To learn more, read Optimize your build.
试验为您的工作站定制的DEX选项可能会提高构建性能。要了解更多信息,请阅读[优化您的构建]

ExternalNativeBuild externalNativeBuild

Specifies options for external native build using CMake or ndk-build.
使用CMake 或 ndk-build 指定外部原生构建的选项

When using Android Studio 2.2 or higher with Android plugin 2.2.0 or higher, you can compile C and C++ code into a native library that Gradle packages into your APK.
当[Android插件2.2.0或更高版本]使用Android Studio 2.2或更高版本时,您可以将C和C ++代码编译到Gradle打包到APK中的本地库中。

To learn more, read Add C and C++ Code to Your Project.

List<String> flavorDimensionList

Specifies the names of product flavor dimensions for this project.
指定此项目的产品风味维度的名称。

To configure flavor dimensions, use flavorDimensions. To learn more, read combine multiple product flavors.
要配置味道维度,请使用flavorDimensions要了解更多信息,请阅读

boolean generatePureSplits

note: this property is incubating and may change in a future version of the plugin.
注意:此属性[孵化],并可能在未来版本的插件中更改。

Specifies whether to build APK splits or multiple APKs from configurations in the Splits block.
指定是否根据[Splits]中的配置构建APK分裂或多个APK html的)块。

When you set this property to true, the Android plugin generates each object in the Splits block as a portion of a whole APK, called an APK split. Compared to building multiple APKs, each APK split includes only the components that each ABI or screen density requires. Generating APK splits is an incubating feature, which requires you to set BaseFlavor.minSdkVersion() to 21 or higher, and is currently supported only when publishing Android Instant Apps.

当您将此属性设置为“true”时,Android插件会在[Splits]中生成每个对象(http://google.github.io/android-gradle-dsl/current/com.android.build.gradle。 internal.dsl.Splits.html)作为整个APK的一部分阻止,称为* APK split *。与构建多个APK相比,每个APK分割仅包含每个ABI或屏幕密度需要的组件。生成APK分割是一项孵化功能,需要您设置[BaseFlavor.minSdkVersion()]设置为21或更高,目前仅在发布[Android Instant Apps]

When you do not configure this property or set it to false (default), the Android plugin builds separate APKs for each object you configure in the Splits block that you can deploy to a device. To learn more about building different versions of your app that each target a different Application Binary Interfaces or screen density, read Build Multiple APKs.
如果您未配置此属性或将其设置为“false”(默认值),则Android插件会为您在[Splits]中配置的每个对象构建单独的APK可以将其部署到设备。要了解更多有关构建不同版本的应用程序的信息,每个版本都针对不同的[应用程序二进制接口]或屏幕密度,请阅读[构建多个APK]

JacocoOptions jacoco

note: this property is deprecated and will be removed in a future version of the plugin.
请注意:此属性为[弃用],并且将在未来版本的插件中删除。

Configure JaCoCo version that is used for offline instrumentation and coverage report.
配置用于脱机检测和覆盖率报告的JaCoCo版本。

To specify the version of JaCoCo you want to use, add the following to build.gradlefile:
要指定要使用的JaCoCo版本,请将以下内容添加到build.gradle文件中

android {
    jacoco {
        version "<jacoco-version>"
    }
}

LintOptions lintOptions

Specifies options for the lint tool.
指定lint工具的选项。

Android Studio and the Android SDK provide a code scanning tool called lint that can help you to identify and correct problems with the structural quality of your code without having to execute the app or write test cases. Each problem the tool detects is reported with a description message and a severity level, so that you can quickly prioritize the critical improvements that need to be made.
Android Studio和Android SDK提供了一个名为lint的代码扫描工具,可以帮助您识别和纠正代码结构质量问题,而无需执行应用程序或编写测试用例。工具检测到的每个问题都会以一个描述消息和严重性级别报告,以便您可以快速确定需要进行的关键改进的优先级。

This property allows you to configure certain lint options, such as which checks to run or ignore. If you're using Android Studio, you can configure similar lint options from the IDE. To learn more about using and running lint, read Improve Your Code with Lint.
此属性允许您配置某些lint选项,例如运行或忽略哪些检查。如果您使用的是Android Studio,则可以从IDE中配置类似的lint选项。要了解有关使用和运行lint的更多信息,请阅读[用Lint改进您的代码]

File ndkDirectory

The path to the Android NDK that Gradle uses for this project.

You can install the Android NDK by either using the SDK manager or downloading the standalone NDK package.
您可以通过使用SDK manager或下载[独立NDK软件包]来安装Android NDK

PackagingOptions packagingOptions

Specifies options and rules that determine which files the Android plugin packages into your APK.
指定用于确定Android插件打包到APK中的哪些文件的选项和规则。

For example, the following example tells the plugin to avoid packaging files that are intended only for testing:
例如,以下示例告诉插件避免打包仅用于测试的文件:

packagingOptions {
    // Tells the plugin to not include any files in the 'testing-data/' directory,
    // which is specified as an absolute path from the root of the APK archive.
    // The exclude property includes certain defaults paths to help you avoid common
    // duplicate file errors when building projects with multiple dependencies.
    exclude "/testing-data/**"
}

To learn more about how to specify rules for packaging, merging, and excluding files, see PackagingOptions
要详细了解如何为打包,合并和排除文件指定规则,请参见[PackagingOptions]

NamedDomainObjectContainer<ProductFlavor> productFlavors

Encapsulates all product flavors configurations for this project.
封装此项目的所有产品口味配置。

Product flavors represent different versions of your project that you expect to co-exist on a single device, the Google Play store, or repository. For example, you can configure 'demo' and 'full' product flavors for your app, and each of those flavors can specify different features, device requirements, resources, and application ID's--while sharing common source code and resources. So, product flavors allow you to output different versions of your project by simply changing only the components and settings that are different between them.
产品风格代表您希望在单个设备,Google Play商店或存储库中共存的项目的不同版本。例如,您可以为应用配置“演示”和“完整”产品风格,并且每种风格都可以指定不同的功能,设备要求,资源和应用程序ID,同时共享常见的源代码和资源。因此,只需更改不同版本的组件和设置,产品风格就可以输出不同版本的项目。

Configuring product flavors is similar to configuring build types: add them to the productFlavors block of your module's build.gradle file and configure the settings you want. Product flavors support the same properties as the DefaultConfig block--this is because defaultConfig defines a ProductFlavor object that the plugin uses as the base configuration for all other flavors. Each flavor you configure can then override any of the default values in defaultConfig, such as the applicationId.
配置产品风格类似于[configuring build types]:将它们添加到模块的build.gradle文件的productFlavors块并配置所需的设置。产品风格支持与DefaultConfig块相同的属性 - 这是因为defaultConfig定义了一个[ProductFlavor]对象,插件使用该对象作为所有其他风格的基本配置。然后,您配置的每种口味都可以覆盖defaultConfig中的任何默认值,例如[applicationId]

When using Android plugin 3.0.0 and higher, each flavor must belong to a flavorDimensions value. By default, when you specify only one dimension, all flavors you configure belong to that dimension. If you specify more than one flavor dimension, you need to manually assign each flavor to a dimension. To learn more, read Use Flavor Dimensions for variant-aware dependency management.
当使用Android插件3.0.0及更高版本时,每种风味都必须属于[flavorDimensions]值。默认情况下,当您只指定一个维度时,所有配置的风格都属于该维度。如果您指定多个味道维度,则需要手动将每个味道分配给一个维度。要了解更多信息,请阅读[使用Flavor Dimensions进行变体感知依赖性管理]。

When you configure product flavors, the Android plugin automatically combines them with your BuildType configurations to create build variants. If the plugin creates certain build variants that you don't want, you can filter variants.
当您配置产品风味时,Android插件会自动将它们与您的BuildType配置结合create build variants。如果插件创建了一些你不想要的构建变体,你可以filter variants

String resourcePrefix

Specifies the module's resource prefix to Android Studio for editor features, such as Lint checks. This property is useful only when using Android Studio.
指定模块的Android Studio编辑器功能的资源前缀,例如Lint检查。此属性仅在使用Android Studio时有用。

Including unique prefixes for module resources helps avoid naming collisions with resources from other modules. For example, when creating a library with String resources, you may want to name each resource with a unique prefix, such as "mylib_" to avoid naming collisions with similar resources that the consumer defines. You can then specify this prefix, as shown below, so that Android Studio expects this prefix when you name module resources:
包含模块资源的唯一前缀有助于避免命名与其他模块的资源发生冲突。例如,当创建一个带有String资源的库时,可能需要为每个资源命名一个唯一的前缀,例如“mylib_”,以避免与消费者定义的类似资源命名冲突。然后,您可以指定此前缀,如下所示,以便Android Studio在命名模块资源时预期使用此前缀:

// This property is useful only when developing your project in Android Studio.
resourcePrefix 'mylib_'

File sdkDirectory

The path to the Android SDK that Gradle uses for this project.
Gradle用于此项目的Android SDK的路径。

To learn more about downloading and installing the Android SDK, read Update Your Tools with the SDK Manager.

NamedDomainObjectContainer<SigningConfig>signingConfigs

Encapsulates signing configurations that you can apply to BuildType and ProductFlavor configurations.

封装您可以应用于[BuildType]的签名配置和[ ProductFlavor]配置。

Android requires that all APKs be digitally signed with a certificate before they can be installed onto a device. When deploying a debug version of your project from Android Studio, the Android plugin automatically signs your APK with a generic debug certificate. However, to build an APK for release, you must sign the APK with a release key and keystore. You can do this by either using the Android Studio UI or manually configuring your build.gradle file.

Android要求所有APK在安装到设备上之前都先用证书进行数字签名。从Android Studio部署项目的调试版本时,Android插件会自动在您的APK中为通用调试证书签名。但是,要构建要发布的APK,您必须使用发行密钥和密钥库[签署APK]。您可以通过[使用Android Studio UI]或手动[配置您的build.gradle文件]

NamedDomainObjectContainer<AndroidSourceSet> sourceSets

Encapsulates source set configurations for all variants.
封装所有变体的源设置配置。

The Android plugin looks for your project's source code and resources in groups of directories called source sets. Each source set also determines the scope of build outputs that should consume its code and resources. For example, when creating a new project from Android Studio, the IDE creates directories for a main/ source set that contains the code and resources you want to share between all your build variants.
Android插件在名为source sets的目录组中查找项目的源代码和资源。每个源集还决定了应该使用其代码和资源的构建输出的范围。例如,当从Android Studio创建一个新项目时,IDE会为包含要在所有构建变体之间共享的代码和资源的“main /”源集创建目录。

You can then define basic functionality in the main/ source set, but use product flavor source sets to change only the branding of your app between different clients, or include special permissions and logging functionality to only "debug" versions of your app.
然后,您可以在main /源代码集中定义基本功能,但使用产品风味源集仅更改不同客户端之间应用程序的品牌,或者包含特殊权限和日志记录功能,以仅调试您的应用程序版本。

The Android plugin expects you to organize files for source set directories a certain way, similar to the main/ source set. For example, Gradle expects Java class files that are specific to your "debug" build type to be located in the src/debug/java/directory.
Android插件期望您以某种方式组织源文件集目录的文件,类似于main /源文件集。例如,Gradle期望特定于“debug”构建类型的Java类文件位于“src / debug / java /`目录中。

Gradle provides a useful task to shows you how to organize your files for each build type-, product flavor-, and build variant-specific source set. you can run this task from the command line as follows:
Gradle提供了一项有用的任务,向您展示如何为每个构建类型(产品风格)和构建特定于变体的源集合组织文件。您可以从命令行运行此任务,如下所示:

./gradlew sourceSets

The following sample output describes where Gradle expects to find certain files for the "debug" build type:
以下示例输出描述了Gradle希望为“调试”构建类型找到某些文件的位置:

------------------------------------------------------------
Project :app
------------------------------------------------------------

...

debug
----
Compile configuration: compile
build.gradle name: android.sourceSets.debug
Java sources: [app/src/debug/java]
Manifest file: app/src/debug/AndroidManifest.xml
Android resources: [app/src/debug/res]
Assets: [app/src/debug/assets]
AIDL sources: [app/src/debug/aidl]
RenderScript sources: [app/src/debug/rs]
JNI sources: [app/src/debug/jni]
JNI libraries: [app/src/debug/jniLibs]
Java-style resources: [app/src/debug/resources]

If you have sources that are not organized into the default source set directories that Gradle expects, as described in the sample output above, you can use the sourceSetblock to change where Gradle looks to gather files for each component of a given source set. You don't need to relocate the files; you only need to provide Gradle with the path(s), relative to the module-level build.gradle file, where Gradle should expect to find files for each source set component.
如果您的源未组织到Gradle预期的默认源集目录中(如上面的示例输出中所述),则可以使用sourceSet块更改Gradle的外观以收集给定源集的每个组件的文件。您不需要重定位文件;您只需向Gradle提供相对于模块级build.gradle文件的路径,其中Gradle应该期望为每个源集合组件找到文件。

Note: You should specify only static paths whenever possible. Specifying dynamic paths reduces build speed and consistency.
注意:您应该尽可能只指定静态路径。指定动态路径会降低构建速度和一致性。

The following code sample maps sources from the app/other/ directory to certain components of the main source set and changes the root directory of the androidTest source set:
以下代码示例将app / other /目录中的源映射到main源集的某些组件,并更改androidTest源集的根目录:

android {
  ...
  sourceSets {
    // Encapsulates configurations for the main source set.
    main {
        // Changes the directory for Java sources. The default directory is
        // 'src/main/java'.
        java.srcDirs = ['other/java']

        // If you list multiple directories, Gradle uses all of them to collect
        // sources. Because Gradle gives these directories equal priority, if
        // you define the same resource in more than one directory, you get an
        // error when merging resources. The default directory is 'src/main/res'.
        res.srcDirs = ['other/res1', 'other/res2']

        // Note: You should avoid specifying a directory which is a parent to one
        // or more other directories you specify. For example, avoid the following:
        // res.srcDirs = ['other/res1', 'other/res1/layouts', 'other/res1/strings']
        // You should specify either only the root 'other/res1' directory, or only the
        // nested 'other/res1/layouts' and 'other/res1/strings' directories.

        // For each source set, you can specify only one Android manifest.
        // By default, Android Studio creates a manifest for your main source
        // set in the src/main/ directory.
        manifest.srcFile 'other/AndroidManifest.xml'
        ...
    }

    // Create additional blocks to configure other source sets.
    androidTest {
        // If all the files for a source set are located under a single root
        // directory, you can specify that directory using the setRoot property.
        // When gathering sources for the source set, Gradle looks only in locations
        // relative to the root directory you specify. For example, after applying the
        // configuration below for the androidTest source set, Gradle looks for Java
        // sources only in the src/tests/java/ directory.
        setRoot 'src/tests'
        ...
    }
  }
}

Splits splits

Specifies configurations for building multiple APKs or APK splits.
指定构建多个APK或APK分割的配置。

To generate APK splits, you need to also set generatePureSplits to true. However, generating APK splits is an incubating feature, which requires you to set BaseFlavor.minSdkVersion() to 21 or higher, and is currently supported only when publishing Android Instant Apps.
要生成APK分割,您还需要设置generatePureSplitsTRUE;。然而,生成APK分割是一项孵化功能,需要您设置BaseFlavor.minSdkVersion()21或更高,目前仅在发布Android Instant Apps

TestOptions testOptions

Specifies options for how the Android plugin should run local and instrumented tests.
指定Android插件如何运行本地和仪器测试的选项。

To learn more, read Configure Gradle test options.

Action<VariantFilter>variantFilter

Specifies variants the Android plugin should include or remove from your Gradle project.
指定Android插件应包含或从您的Gradle项目中删除的变体。

By default, the Android plugin creates a build variant for every possible combination of the product flavors and build types that you configure, and adds them to your Gradle project. However, there may be certain build variants that either you do not need or do not make sense in the context of your project. You can remove certain build variant configurations by creating a variant filter in your module-level build.gradle file.

默认情况下,Android插件会为您配置的产品风格和构建类型的每种可能组合创建一个构建变体,并将它们添加到您的Gradle项目中。但是,可能会有某些构建变体,或者您不需要或在项目上下文中没有意义。您可以在模块级别的build.gradle文件中通过creating a variant filter删除特定的构建变体配置。

The following example tells the plugin to ignore all variants that combine the "dev" product flavor, which you can configure to optimize build speeds during development, and the "release" build type:
以下示例告诉插件忽略所有将dev产品风格相结合的变体,您可以将其配置为optimize build speeds(优化构建速度),以及“发布”版本类型:

android {
    ...
    variantFilter { variant ->

        def buildTypeName = variant.buildType*.name
        def flavorName = variant.flavors*.name

        if (flavorName.contains("dev") && buildTypeName.contains("release")) {
            // Tells Gradle to ignore each variant that satisfies the conditions above.
            setIgnore(true)
        }
    }
}

During subsequent builds, Gradle ignores any build variants that meet the conditions you specify. If you're using Android Studio, those variants no longer appear in the drop down menu when you click Build > Select Build Variant from the menu bar.
在后续的构建过程中,Gradle会忽略符合您指定条件的任何构建变体。如果您使用Android Studio,那么当您从菜单中点击Build> Select Build Variant时,这些变体将不再出现在下拉菜单。

Method details

void flavorDimensions(String...dimensions)

Specifies the names of product flavor dimensions for this project.
指定此项目的产品风味维度的名称。

When configuring product flavors with Android plugin 3.0.0 and higher, you must specify at least one flavor dimension, using the flavorDimensions property, and then assign each flavor to a dimension. Otherwise, you will get the following build error:
使用Android插件3.0.0及更高版本配置产品风格时,必须使用[flavorDimensions]至少指定一个风格维度。flavorDimensions(java.lang.String []))属性,然后将每个flavor指定给一个维度。否则,你会得到以下的构建错误:

Error:All flavors must now belong to a named flavor dimension.
The flavor 'flavor_name' is not assigned to a flavor dimension.

By default, when you specify only one dimension, all flavors you configure automatically belong to that dimension. If you specify more than one dimension, you need to manually assign each flavor to a dimension, as shown in the sample below.
默认情况下,只指定一个维度时,所有配置的风格都会自动属于该维度。如果您指定了多个维度,则需要手动将每个风格分配给一个维度,如下面的示例所示。

Flavor dimensions allow you to create groups of product flavors that you can compine with flavors from other flavor dimensions. For example, you can have one dimension that includes a 'free' and 'paid' version of your app, and another dimension for flavors that support different API levels, such as 'minApi21' and 'minApi24'. The Android plugin can then combine flavors from these dimensions—including their settings, code, and resources—to create variants such as 'debugFreeMinApi21' and 'releasePaidMinApi24', and so on. The sample below shows you how to specify flavor dimensions and add product flavors to them.

风味维度允许您创建可以与其他风味维度的风味进行合并的产品风味组。例如,您可以拥有一个包含应用“免费”和“付费”版本的维度,以及另一个支持不同API级别的维度维度,例如minApi21minApi24。然后,Android插件可以将这些维度的风格(包括其设置,代码和资源)组合起来,以创建诸如debugFreeMinApi21releasePaidMinApi24之类的变体,等等。下面的示例显示了如何指定味道维度并为其添加产品口味。

android {
    ...
    // Specifies the flavor dimensions you want to use. The order in which you
    // list each dimension determines its priority, from highest to lowest,
    // when Gradle merges variant sources and configurations. You must assign
    // each product flavor you configure to one of the flavor dimensions.
    flavorDimensions 'api', 'version'

    productFlavors {
      demo {
        // Assigns this product flavor to the 'version' flavor dimension.
        dimension 'version'
        ...
    }

      full {
        dimension 'version'
        ...
      }

      minApi24 {
        // Assigns this flavor to the 'api' dimension.
        dimension 'api'
        minSdkVersion '24'
        versionNameSuffix "-minApi24"
        ...
      }

      minApi21 {
        dimension "api"
        minSdkVersion '21'
        versionNameSuffix "-minApi21"
        ...
      }
   }
}

To learn more, read Combine multiple flavors.

void useLibrary(String name)

Includes the specified library to the classpath.
将指定的库包含到类路径中。

You typically use this property to support optional platform libraries that ship with the Android SDK. The following sample adds the Apache HTTP API library to the project classpath:
您通常使用此属性来支持随Android SDK一起提供的可选平台库。以下示例将Apache HTTP API库添加到项目类路径中:

android {
    // Adds a platform library that ships with the Android SDK.
    useLibrary 'org.apache.http.legacy'
}

To include libraries that do not ship with the SDK, such as local library modules or binaries from remote repositories, add the libraries as dependencies in the dependencies block. Note that Android plugin 3.0.0 and later introduce new dependency configurations. To learn more about Gradle dependencies, read Dependency Management Basics.
要包含SDK附带的库,例如本地库模块或远程存储库的二进制文件,将库添加为依赖关系 dependencies块。请注意,Android插件3.0.0和更高版本引入了新的依赖配置。要了解更多关于Gradle依赖关系的信息,请阅读 依赖管理基础知识

void useLibrary(String name, boolean required)

Includes the specified library to the classpath.
将指定的库包含到类路径中。

You typically use this property to support optional platform libraries that ship with the Android SDK. The following sample adds the Apache HTTP API library to the project classpath:
您通常使用此属性来支持随Android SDK一起提供的可选平台库。以下示例将Apache HTTP API库添加到项目类路径中:

android {
    // Adds a platform library that ships with the Android SDK.
    useLibrary 'org.apache.http.legacy'
}

To include libraries that do not ship with the SDK, such as local library modules or binaries from remote repositories, add the libraries as dependencies in the dependencies block. Note that Android plugin 3.0.0 and later introduce new dependency configurations. To learn more about Gradle dependencies, read Dependency Management Basics.
要包含SDK附带的库,例如本地库模块或远程存储库的二进制文件,add the libraries as dependenciesdependencies块。请注意,Android插件3.0.0和更高版本引入了new dependency configurations。要了解更多关于Gradle依赖关系的信息,请阅读Dependency Management Basics

脚本块详细信息 Script block details

aaptOptions { }

Specifies options for the Android Asset Packaging Tool (AAPT).
指定Android资产打包工具(AAPT)的选项。

For more information about the properties you can configure in this block, see AaptOptions.
有关可在此块中配置的属性的更多信息,请参阅[AaptOptions]

AaptOptions from aaptOptions

adbOptions { }

Specifies options for the Android Debug Bridge (ADB), such as APK installation options.
指定Android调试桥(ADB)的选项,例如APK安装选项。

For more information about the properties you can configure in this block, see AdbOptions.

Delegates to:
AdbOptions from adbOptions

buildTypes{ }

Encapsulates all build type configurations for this project.
封装此项目的所有构建类型配置。

For more information about the properties you can configure in this block, see BuildType.

Delegates to:
NamedDomainObjectContainer<BuildType> from buildTypes

compileOptions{ }

Specifies Java compiler options, such as the language level of the Java source code and generated bytecode.
指定Java编译器选项,例如Java源代码的语言级别和生成的字节码。

For more information about the properties you can configure in this block, see CompileOptions.

Delegates to:
CompileOptions from compileOptions

dataBinding { }

Specifies options for the Data Binding Library.
指定[数据绑定库]的选项

For more information about the properties you can configure in this block, see DataBindingOptions.

Delegates to:
DataBindingOptions from dataBinding

defaultConfig { }

Specifies defaults for variant properties that the Android plugin applies to all build variants.
指定Android插件应用于所有构建变体的变体属性的默认值。

You can override any defaultConfig property when configuring product flavors.
当[configuring product flavors]时,您可以覆盖任何defaultConfig属性

For more information about the properties you can configure in this block, see ProductFlavor.

Delegates to:>
DefaultConfig from defaultConfig

dexOptions { }

Specifies options for the DEX tool, such as enabling library pre-dexing.
指定DEX工具的选项,例如启用库预分离。

For more information about the properties you can configure in this block, see DexOptions.

Delegates to:
DexOptions from dexOptions

externalNativeBuild { }

Configures external native build using CMake or ndk-build.
使用CMakendk-build配置外部本机构建。

For more information about the properties you can configure in this block, see ExternalNativeBuild.

Delegates to:
ExternalNativeBuild from externalNativeBuild

jacoco { }

note: this script block is deprecated and will be removed in a future version of the plugin.
请注意:此脚本块为弃用,将在未来版本的删除。

Configuring JaCoCo using this block is deprecated.
使用此块配置JaCoCo已弃用。

To specify the version of JaCoCo you want to use, you now need to include it as a buildscript dependency in your project-level build.gradle file, as follows:

buildscript {
    dependencies {
        classpath "org.jacoco:org.jacoco.core:<jacoco-version>"
        ...
    }
}

lintOptions { }

Specifies options for the lint tool.
指定lint工具的选项。

For more information about the properties you can configure in this block, see LintOptions.

Delegates to
LintOptions from lintOptions

packagingOptions{ }

Specifies options and rules that determine which files the Android plugin packages into your APK.
指定用于确定Android插件打包到APK中的哪些文件的选项和规则。

For more information about the properties you can configure in this block, see PackagingOptions.

Delegates to:
PackagingOptions from packagingOptions

productFlavors{ }

Encapsulates all product flavors configurations for this project.
封装此项目的所有产品口味配置。

For more information about the properties you can configure in this block, see ProductFlavor

Delegates to:
NamedDomainObjectContainer<ProductFlavor> from productFlavors

signingConfigs { }

Encapsulates signing configurations that you can apply to BuildType and ProductFlavor configurations.
封装您可以应用于[BuildType]的签名配置和[ ProductFlavor]配置。

For more information about the properties you can configure in this block, see SigningConfig

Delegates to:
NamedDomainObjectContainer<SigningConfig> from signingConfigs

sourceSets { }

Encapsulates source set configurations for all variants.
封装所有变体的源设置配置。

Note that the Android plugin uses its own implementation of source sets. For more information about the properties you can configure in this block, see AndroidSourceSet.
请注意,Android插件使用其自己的源集实现。有关可在此块中配置的属性的更多信息,请参阅AndroidSourceSet。

Delegates to:
NamedDomainObjectContainer<AndroidSourceSet> from sourceSets

splits { }

Specifies configurations for building multiple APKs or APK splits.
指定building multiple APKs或APK分割的配置。

For more information about the properties you can configure in this block, see Splits.

Delegates to:
Splits from splits

testOptions { }

Specifies options for how the Android plugin should run local and instrumented tests.

指定Android插件如何运行本地和仪器测试的选项。

For more information about the properties you can configure in this block, see TestOptions.

Delegates to:
TestOptions from testOptions

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,080评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,422评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,630评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,554评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,662评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,856评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,014评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,752评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,212评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,541评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,687评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,347评论 4 331
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,973评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,777评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,006评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,406评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,576评论 2 349

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,804评论 25 707
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,412评论 2 45
  • 转自:http://blog.csdn.net/highning0007/article/details/3799...
    某人在阅读 1,657评论 0 0
  • 郭沈垲,无子无号,现年十四岁,面黄无须。生于瓜沥,祖籍牌头,可谓背井离乡之人。出生之年,早无帝王,可谓无君。...
    土山豆阅读 6,136评论 1 4
  • 国庆长假,准备回家看看老爸老妈。考虑到以往1号出行的堵车壮观景象,打算晚上提前出发。在市区时,暗暗庆幸自己决策的英...
    可妈手记阅读 253评论 0 2