该文档适合初入Android的小白,帮助理解Android studio和gradle是怎样配合编译源码、资源文件生成apk!
配置搭建
Android Studio 使用Gradle 这一高级构建工具包来自动化执行和管理构建流程,同时也支持自定义构建配置。每个构建配置均可自行定义一组代码和资源,同时对所有应用版本共有的部分加以重复利用。Android Plugin for Gradle 与这个构建工具包协作,共同提供用于构建和测试 Android 应用的流程和设置。
Gradle 和 Android 插件独立于 Android Studio 运行。这表示可以在 Android Studio 内、使用计算机上的命令行工具或在未安装 Android Studio 的计算机(例如持续性集成服务器)上构建 Android 应用。如果您不使用 Android Studio,可以学习如何从命令行构建和运行您的应用。无论您是从命令行、在远程计算机上还是使用 Android Studio 构建项目,构建的输出都相同。
注:由于 Gradle 和 Android 插件独立于 Android Studio 运行,您需要单独更新构建工具。请阅读版本说明,了解如何更新 Gradle 和 Android 插件。
接下来进入正题!
构建流程
如图 1 所示,典型 Android 应用模块的构建流程通常依循下列步骤:
编译器将您的源代码转换成 DEX(Dalvik Executable) 文件(其中包括运行在 Android 设备上的字节码),将所有其他内容转换成已编译资源。
APK 打包器将 DEX 文件和已编译资源合并成单个 APK。不过,必须先签署 APK,才能将应用安装并部署到 Android 设备上。
-
APK 打包器使用调试或发布密钥库签署您的 APK:
a. 如果构建的是调试版本的应用(即专用于测试和分析的应用),打包器会使用调试密钥库签署您的应用。Android Studio 自动使用调试密钥库配置新项目。
b. 如果构建的是打算向外发布的发布版本应用,打包器会使用发布密钥库签署您的应用。要创建发布密钥库,请阅读在 Android Studio 中签署您的应用。
在生成最终 APK 之前,打包器会使用 zipalign 工具对应用进行优化,减少其在设备上运行时的内存占用。
构建流程结束时,您将获得可用来进行部署、测试的调试 APK,或者可用来发布给外部用户的发布 APK。
构建配置文件
创建自定义构建配置需要您对一个或多个构建配置文件(或 build.gradle 文件)进行更改。这些纯文本文件使用域特定语言 (DSL) 以 Groovy 语言描述和操作构建逻辑,后者是一种适用于 Java 虚拟机 (JVM) 的动态语言。您无需了解 Groovy 便可开始配置构建,因为 Android Plugin for Gradle 引入了您需要的大多数 DSL 元素。如需了解有关 Android 插件 DSL 的更多信息,请阅读DSL参考文档。
开始新项目时,Android Studio 会自动为您创建其中的部分文件(如图 2 所示),并为它们填充合理的默认值。
有几个 Gradle 构建配置文件是 Android 应用标准项目结构的组成部分。了解其中每一个文件的范围和用途及其应定义的基本 DSL 元素,才能着手配置构建。
Gradle 设置文件
settings.gradle 文件位于项目根目录,用于指示 Gradle 在构建应用时应将哪些模块包括在内。对大多数项目而言,该文件很简单,只包括以下内容:
include ‘:app’
不过,多模块项目需要指定应包括在最终构建之中的每个模块。
顶级构建文件
顶级 build.gradle 文件位于项目根目录,用于定义适用于项目中所有模块的构建配置。默认情况下,这个顶级构建文件使用 buildscript {} 代码块来定义项目中所有模块共用的 Gradle 存储区和依赖项。以下代码示例描述的默认设置和 DSL 元素可在新建项目后的顶级 build.gradle 文件中找到。
/**
* 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.3.3 as a classpath dependency.
*/
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
}
/**
* 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()
}
}
模块级构建文件
模块级 build.gradle 文件位于每个<project><module>
目录,用于配置适用于其所在模块的构建设置。您可以通过配置这些构建设置来提供自定义打包选项(例如附加构建类型和产品风味),以及替换 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 26
buildToolsVersion "26.0.0"
/**
* 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 15
// Specifies the API level used to test the app.
targetSdkVersion 26
// 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:25.4.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Gradle 属性文件
Gradle 还包括两个属性文件,位于项目根目录,可用于指定适用于 Gradle 构建工具包本身的设置:
gradle.properties
可以在其中配置项目范围 Gradle 设置,例如 Gradle 后台进程的最大堆大小。
local.properties
为构建系统配置本地环境属性,例如 SDK 安装路径。由于该文件的内容由 Android Studio 自动生成并且专用于本地开发者环境。