概览
Gradle
2013年5月,谷歌发布Android Studio(基于JetBrains的IntelliJ IDEA),并对Gradle进行了支持。Gradle构建脚本的书写没有基于传统的XML文件,而是基于Groovy
的领域专用语言(DSL
)。
- Gradle构建Android项目时,需要创建一个构建脚本,通常称为
build.gradle
。 - Gradle有约定优于配置的原则,即为设置和属性提供默认值。
You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or by editing the Gradle distribution reference in the gradle/wrapper/gradle-wrapper.properties file.
...
distributionUrl = https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
...
Groovy
Groovy
Groovy是一种适用于Java虚拟机的动态语言。
类和成员变量
def:声名变量
方法
在Groovy中,方法的最后一行通常默认返回,即使没有使用return关键字。
Closures
Closures是匿名代码块,可以接受参数和返回值。
集合
List和Map
List list = [1, 2, 3, 4]
list.each() { element ->
println element
}
Map prices = [apple:10, car:100]
apply plugin: 'com.android.application'
// apply()是Project类的一个方法。参数是key为plugin,value为com.android.application的Map。
project.apply([plugin: 'com.android.application'])
项目和任务
每一次构建都包括至少一个项目,每一个项目又包括一个或多个任务。每个build.gradle文件都代表着一个项目,任务定义在构建脚本里。当初始化构建过程时,Gradle会基于build文件组装项目和任务对象。一个任务对象包含一系列动作对象,这些动作对象之后会按顺序执行。一个单独的动作对象就是一个待执行的代码块,它和Java中的方法类似。
构建生命周期
- 初始化
- 配置
- 执行
Gradle Wrapper
项目根目录,在terminal上运行gradlew -v
或在命令行上运行gradlew.bat -v
检查项目中的Gradle Wrapper是否可用。
https://gradle.org/releases/
gradle文件
settings.gradle
:位于项目的根目录
include ':app'
build.gradle
:顶级构建文件,位于项目的根目录
buildscript {
repositories {
jcenter()
}
dependencies {
// Gradle的Android插件
classpath 'com.android.tools.build:gradle:2.3.0'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
build.gradle
:模块级构建文件,位于Module的根目录,可以覆盖顶层build.gradle中的任何属性。
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "com.test"
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0.0"
}
signingConfigs {
debug {
storeFile file("./doc/debug/debug.keystore")
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
}
buildTypes {
debug {
minifyEnabled false
signingConfig signingConfigs.debug
}
release {
minifyEnabled true
signingConfig signingConfigs.debug
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
}
**applicationId:**定义在manifest文件中的package,继续用在你的源代码和R类中,而之前被用作设备和Google Play唯一标识的package name,现在则被称之为applicationId。
ext:
Gradle允许在Project对象上添加额外属性。这意味着任何build.gradle文件都能定义额外的属性,添加额外属性需要通过ext代码块。
顶层构建文件添加如下代码块:
ext {
compileSdkVersion = 23
buildToolsVersion = '23.0.3'
}
模块层的构建文件使用rootProject获取属性:
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
BuildConfig
SDK工具版本升级到17之后,构建工具都会生成一个叫作BuildConfig的类,该类包含一个按照构建类型设置值得DEBUG常量。
android {
buildTypes {
debug {
buildConfigField "boolean","IS_LOG","true"
buildConfigField "String","URL","\"http://test.example.com\""
}
release {
buildConfigField "boolean","IS_LOG","false"
}
}
}
字符串值必须用转义双引号括起来,这样才会生成实际意义上的字符串。Java代码中可以这样用BuildConfig.IS_LOG和BuildConfig.URL
- buildConfigField
- resValue
依赖管理
本地依赖
https://developer.android.google.cn/studio/build/dependencies.html
dependencies {
// 依赖单个JAR文件
compile files('libs/test.jar')
// 依赖所有文件
compile fileTree('libs')
// 依赖指定文件格式,如JAR
compile fileTree(dir: 'libs', include: ['*.jar'])
// 远程依赖
implementation 'com.example.android:app-magic:12.3'
// 本地库模块依赖项
implementation project(':mylibrary')
}
**原生依赖库.so文件:**在模块的main目录下新建jniLibs文件夹,然后为每个平台(armeabi、mips、x86)创建子文件夹。如果此约定不生效,可以在构建文件中设置相关设置:
android {
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
}
}
.aar文件:
- 添加依赖仓库文件夹
- 添加依赖,如下
dependencies {
compile(name: 'aarName', ext: 'aar')
}
语义化版本
将依赖添加到JCenter等依赖仓库时,约定遵循了一套版本化规则,称为语义化版本。版本数字的格式一般为major.minor.patch
- 当做不兼容的API变化时,major版本增加。
- 当以向后兼容的方式添加功能时,minor版本增加。
- 当修复一些bug时,patch版本增加。
基于以上规则,可以添加动态化版本。+
远程仓库
如果您的依赖项并非本地库或文件树,Gradle会在您的build.gradle文件repositories程序块中指定的任何一个在线代码库中寻找文件。列出各代码库的顺序决定了Gradle在这些代码库中搜索各项目依赖项的顺序。
默认情况下,Android Studio新项目会在项目的顶级build.gradle文件中指定Google的Maven代码库和 JCenter作为代码库位置,如下所示:
allprojects {
repositories {
google()
jcenter()
}
}
如果您需要的内容来自Maven中央代码库,则添加**mavenCentral()**如果来自本地代码库,则使用 mavenLocal()
allprojects {
repositories {
google()
jcenter()
mavenCentral()
mavenLocal()
}
}
或者,也可像下面这样声明特定Maven或Ivy代码库:
allprojects {
repositories {
maven {
url "https://repo.example.com/maven2"
}
maven {
url "file://local/repo/"
}
ivy {
url "https://repo.example.com/ivy"
}
}
}
本地仓库
通过使用flatDirs添加一个常用文件夹作为仓库。
repositories {
flatDir {
dirs 'libs', '../Framework/libs'
}
}
构建variant
https://developer.android.google.cn/studio/build/build-variants.html
构建类型
https://developer.android.google.cn/studio/build/build-variants.html#build-types
android {
buildTypes {
debug {
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
// 用已有的构建类型属性初始化新的构建类型
staging.initWith(buildTypes.debug)
staging {
applicationIdSuffix ".staging"
versionNameSuffix "-staging"
}
}
}
可以根据构建类型来添加依赖。如debugCompile ‘com.android.support:***:1.0.0’
product flavor
https://developer.android.google.cn/studio/build/build-variants.html#product-flavors
android {
productFlavors {
free {
}
paid {
}
}
}
签名配置
android {
signingConfigs {
staging.initWith(signingConfigs.debug)
release {
storeFile file("../key/debug.keystore")
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
}
}
加速构建
Jack和Jill
Jack(Java Android Compiler Kit)是一个新的Android构建工具链,其可以直接编译Java源码为Android Dalvik的可执行格式。它有自己的.jack依赖库格式,也采用了打包和缩减。
Jill(Jack Intermediate Library Linker)是一个可以将.aar和.jar文件转换成.jack依赖库的工具。
useJack = true