官网原文:Configure Your Build
编译配置
Android构建系统会编译应用资源和源代码,并将其打包到APK中,您可以对其进行测试,部署,签名和发行。 Android Studio使用Gradle(一种高级构建工具包)来自动化和管理构建过程,同时允许您定义灵活的自定义构建配置。 每个构建配置可以定义自己的一组代码和资源,同时重用您的应用程序的所有版本的共同部分。 Gradle的Android插件与构建工具包配合使用,以提供特定于构建和测试Android应用程序的过程和可配置设置。
Gradle和Android插件独立于Android Studio运行。 这意味着您可以从Android Studio,计算机上的命令行或未安装Android Studio的计算机(例如持续集成服务器)构建Android应用程序。 如果您不使用Android Studio,您可以了解如何从命令行构建和运行应用程序。 无论您是从命令行构建项目,在远程计算机上还是使用Android Studio,构建的输出都是相同的。
注意:由于Gradle和Android插件独立于Android Studio运行,因此您需要单独更新构建工具。 阅读发布说明,了解如何更新Gradle和Android插件。
构建过程
构建过程涉及许多工具和过程,将您的项目转换为Android应用程序包(APK)。 构建过程非常灵活,因此了解一些正在发生的事情是有用的。
图1.典型的Android应用程序模块的构建过程。
典型的Android应用程序模块的构建过程。如图1所示,遵循以下一般步骤:
- 编译器将您的源代码转换为DEX(Dalvik Executable)文件,其中包括在Android设备上运行的字节码,其他所有内容都转换为编译资源。
- APK Packager将DEX文件和已编译的资源合并为一个APK。 但是,在您的应用程序可以安装并部署到Android设备上之前,APK必须已签名。
- APK Packager使用调试或发布密钥库对您的APK进行签名:
a. 如果你正在构建的应用程序是调试版本,也就是一个只打算测试和分析的应用程序,打包程序会使用调试密钥库对应用程序进行签名。 Android Studio使用调试密钥库自动配置新项目。
b. 如果您正在构建需要向外部发布的应用程序的版本,则打包程序使用发布密钥库对应用程序进行签名。 要创建发布密钥库,请参阅在Android Studio中对应用程序进行签名。 - 在生成您的最终APK之前,打包程序使用zipalign工具优化您的应用程序,以便在设备上运行时使用更少的内存。
在构建过程结束时,您可以使用调试APK或发布APK来部署,测试或对外发布给用户。
自定义构建配置
Gradle和Android插件可以帮助您配置构建的以下方面:
构建类型
构建类型定义Gradle在构建和打包应用程序时使用的某些属性,并且通常针对开发生命周期的不同阶段进行配置。 例如,调试生成类型启用调试选项,并使用调试密钥对APK进行签名,而发布版本类型可能会收缩,模糊处理并使用发布密钥签署APK。 您必须至少定义一个构建类型才能构建您的应用程序 - 默认情况下,Android Studio创建调试和发布构建类型。 要开始自定义应用程序的打包设置,请了解如何配置构建类型。
产品风格
产品风格代表您可以向用户发布不同的应用程序版本,例如应用程序的免费和付费版本。 您可以自定义产品风格以使用不同的代码和资源,同时共享和重用您的应用程序的所有版本常见的部分。 产品风格是可选的,您必须手动创建它们。 要开始创建应用程序的不同版本,请了解如何配置产品风格。
构建变量
构建变量是构建类型和产品风格的交叉产品,也是Gradle用来构建应用程序的配置。 使用构建变量,您可以在开发过程中构建产品风格的调试版本,或者分发您的产品风格的签名版本。 虽然您不直接配置构建变量,但您可以用他们配置构建类型和产品风格。 创建其他构建类型或产品类型还会创建其他构建变量。 要了解如何创建和管理构建变体,请阅读配置构建变体概述。
清单条目
您可以在构建变量配置中指定清单文件的某些属性的值。 这些构建值将覆盖清单文件中的现有值。 如果您想为您的模块生成多个APK,而每个apk文件具有不同的应用程序名称,最低SDK版本或目标SDK版本,则此功能非常有用。 当存在多个清单时,Gradle合并清单设置。
依赖
构建系统从本地文件系统和远程存储库管理项目依赖项。 这可以避免您手动搜索,下载并将依赖项的二进制包复制到项目目录中。 要了解更多信息,请了解如何声明依赖关系。
签名
构建系统允许您在构建配置中指定签名设置,并且可以在构建过程中自动为您的APK签名。 构建系统使用默认密钥、证书和已知凭据签署调试版本,以避免在构建时出现密码提示。 构建系统不签署发布版本,除非您明确定义此构建的签名配置。 如果您没有发布密钥,则可以按照应用程序签名中所述生成一个。
ProGuard1
构建系统使您能够为每个构建变量指定不同的ProGuard规则文件。 构建系统可以运行ProGuard以在构建过程中压缩和混淆您的类。
APK拆分
构建系统使您能够自动构建不同的APK,每个只包含特定屏幕密度或应用程序二进制接口(ABI)所需的代码和资源。 有关详细信息,请参阅APK拆分配置。
构建配置文件
创建自定义构建配置需要对一个或多个构建配置文件或build.gradle
文件进行更改。 这些纯文本文件使用的是Domain Specific Language(DSL)来描述和操作使用Groovy的构建逻辑,Groovy是Java虚拟机(JVM)的动态语言。 你不需要知道Groovy开始配置你的构建,因为Gradle的Android插件介绍了你需要的大部分DSL元素。 要详细了解Android插件DSL,请阅读DSL参考文档。
当开始新项目时,Android Studio会自动为您创建一些这些文件,如图2所示,并根据合理的默认值填充它们。
当开始新项目时,Android Studio会自动为您创建一些这些文件,如图2所示,并根据合理的默认值填充它们。
图2. Android应用程序模块的默认项目结构。
有一些Gradle构建配置文件是Android应用程序的标准项目结构的一部分。 在开始配置构建之前,了解每个文件的范围和目的以及它们应定义的基本DSL元素很重要。
Gradle设置文件
settings.gradle
文件位于根项目目录中,告诉Gradle在构建应用程序时应包括哪些模块。 对于大多数项目,文件很简单,只包括以下内容:
include ':app'
然而,多模块项目需要指定每个应该进入最终构建的模块。
顶级构建文件
位于项目根目录中的顶级build.gradle文件定义了适用于项目中所有模块的构建配置。 默认情况下,顶级构建文件使用 buildscript
{}块来定义项目中所有模块所共有的Gradle存储库和依赖关系。 以下代码示例介绍了在创建新项目后,您可以在顶级build.gradle中找到的默认设置和DSL元素。
/**
* The buildscript {} block is where you configure the repositories and
* dependencies for Gradle itself--meaning, you should not include dependencies
* for your modules here. For example, this block includes the Android plugin for
* Gradle as a dependency because it provides the additional instructions Gradle
* needs to build Android app modules.
*/
buildscript {
/**
* The repositories {} block configures the repositories Gradle uses to
* search or download the dependencies. Gradle pre-configures support for remote
* repositories such as JCenter, Maven Central, and Ivy. You can also use local
* repositories or define your own remote repositories. The code below defines
* JCenter as the repository Gradle should use to look for its dependencies.
*/
repositories {
jcenter()
}
/**
* The dependencies {} block configures the dependencies Gradle needs to use
* to build your project. The following line adds Android Plugin for Gradle
* version 2.0.0 as a classpath dependency.
*/
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
}
}
/**
* The allprojects {} block is where you configure the repositories and
* dependencies used by all modules in your project, such as third-party plugins
* or libraries. Dependencies that are not required by all the modules in the
* project should be configured in module-level build.gradle files. For new
* projects, Android Studio configures JCenter as the default repository, but it
* does not configure any dependencies.
*/
allprojects {
repositories {
jcenter()
}
}
模块级构建文件
位于每个<project>/<module>/
目录中的模块级build.gradle
文件允许您为其所在的特定模块配置构建设置。配置这些构建设置允许您提供自定义打包选项,例如 作为其他构建类型和产品版本,并覆盖main/
应用程序清单或顶级build.gradle
文件中的设置。
你应该知道Android应用程序模块的build.gradle
文件概述了一些基本的DSL元素和设置。
/**
* The first line in the build configuration applies the Android plugin for
* Gradle to this build and makes the android {} block available to specify
* Android-specific build options.
*/
apply plugin: 'com.android.application'
/**
* The android {} block is where you configure all your Android-specific
* build options.
*/
android {
/**
* compileSdkVersion specifies the Android API level Gradle should use to
* compile your app. This means your app can use the API features included in
* this API level and lower.
*
* buildToolsVersion specifies the version of the SDK build tools, command-line
* utilities, and compiler that Gradle should use to build your app. You need to
* download the build tools using the SDK Manager.
*/
compileSdkVersion 23
buildToolsVersion "23.0.3"
/**
* The defaultConfig {} block encapsulates default settings and entries for all
* build variants, and can override some attributes in main/AndroidManifest.xml
* dynamically from the build system. You can configure product flavors to override
* these values for different versions of your app.
*/
defaultConfig {
/**
* applicationId uniquely identifies the package for publishing.
* However, your source code should still reference the package name
* defined by the package attribute in the main/AndroidManifest.xml file.
*/
applicationId 'com.example.myapp'
// Defines the minimum API level required to run the app.
minSdkVersion 14
// Specifies the API level used to test the app.
targetSdkVersion 23
// Defines the version number of your app.
versionCode 1
// Defines a user-friendly version name for your app.
versionName "1.0"
}
/**
* The buildTypes {} block is where you can configure multiple build types.
* By default, the build system defines two build types: debug and release. The
* debug build type is not explicitly shown in the default build configuration,
* but it includes debugging tools and is signed with the debug key. The release
* build type applies Proguard settings and is not signed by default.
*/
buildTypes {
/**
* By default, Android Studio configures the release build type to enable code
* shrinking, using minifyEnabled, and specifies the Proguard settings file.
*/
release {
minifyEnabled true // Enables code shrinking for the release build type.
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
/**
* The productFlavors {} block is where you can configure multiple product
* flavors. This allows you to create different versions of your app that can
* override defaultConfig {} with their own settings. Product flavors are
* optional, and the build system does not create them by default. This example
* creates a free and paid product flavor. Each product flavor then specifies
* its own application ID, so that they can exist on the Google Play Store, or
* an Android device, simultaneously.
*/
productFlavors {
free {
applicationId 'com.example.myapp.free'
}
paid {
applicationId 'com.example.myapp.paid'
}
}
/**
* The splits {} block is where you can configure different APK builds that
* each contain only code and resources for a supported screen density or
* ABI. You'll also need to configure your build so that each APK has a
* different versionCode.
*/
splits {
// Screen density split settings
density {
// Enable or disable the density split mechanism
enable false
// Exclude these densities from splits
exclude "ldpi", "tvdpi", "xxxhdpi", "400dpi", "560dpi"
}
}
}
/**
* The dependencies {} block in the module-level build configuration file
* only specifies dependencies required to build the module itself.
*/
dependencies {
compile project(":lib")
compile 'com.android.support:appcompat-v7:22.0.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Gradle属性文件
Gradle还包括两个属性文件,它们位于项目根目录中,您可以使用它们为Gradle构建工具包本身指定设置:
gradle.properties
在这里您可以配置项目范围的Gradle设置,例如Gradle守护程序的最大堆大小。 有关详细信息,请参阅构建环境。
local.properties
配置构建系统的本地环境属性,例如SDK安装的路径。 由于此文件的内容是由Android Studio自动生成的,并且特定于本地开发人员环境,因此您不应手动修改此文件或将其上传到版本控制系统。
与Gradle文件同步项目
当您更改项目中的构建配置文件时,Android Studio需要同步您的项目文件,并运行一些检查以便它确保您的配置不会由构建配置的更改而出现错误。
要同步项目文件,请在进行更改后显示的通知栏中单击 Sync Now
,或单击菜单栏中的 Sync Project
,如图3所示。 如果Android Studio发现您的配置出现任何错误,例如,您的源代码使用的API功能仅在高于compileSdkVersion的API级别可用,则会出现 Messages
窗口来描述该问题。
图3.在Android Studio中将项目与构建配置文件同步。
资源集合
Android Studio在逻辑上将每个模块的源代码和资源分组到源集中。 模块的 main/
源集包括其所有构建变量使用的代码和资源。 其他源集目录是可选的,并且当您配置新构建版本时,Android Studio不会为您自动创建它们。 但是,创建源集与 main/
类似,有助于组织Gradle在构建特定版本应用程序时应使用的文件和资源:
src/main/
此源集包括所有构建变量公用的代码和资源。
src/<buildType>/
创建此源集合以仅包含特定构建类型的代码和资源。
src/<productFlavor>/
创建此源集,以包含特定产品类型的代码和资源。
例如,要生成应用程序的“fullDebug”版本,构建系统会合并来自以下源集的代码,设置和资源:
src/fullDebug/
(构建变量源集)src/debug/
(构建类型源集)src/full/
(产品风格源集)src/main/
(主源集)
注意:在Android Studio中创建新文件或目录时,使用
File > New
菜单选项,它可以创建特定源集。 选择的源集是基于您的构建配置,并且如果它们不存在,Android Studio自动创建所需的目录。
如果不同的源集包含相同文件的不同版本,则Gradle在决定使用哪个文件时使用以下优先级顺序(左侧的源集会覆盖右侧源集的文件和设置):
build variant > build type > product flavor > main source set > library dependencies
构建不同应用版本时,允许Gradle使用特定的构建文件,同时重用与其他版本的应用程序通用的活动,应用程序逻辑和资源。 当合并多个清单时,Gradle使用相同的优先级顺序,因此每个构建变量可以在最终清单中定义不同的组件或权限。 要了解有关创建自定义源集的更多信息,请参考构建变量的源集创建。
- 压缩、优化和混淆Java字节码文件的免费工具 ↩