The Gradle DSL supports a task block for defining your own custom tasks. The API
includes a wide range of existing tasks (like Copy , Wrapper , and Exec ) that you can use
simply by setting properties.
大概原意:Gradle DSL支持使用task作用块来自定义任务。API包含了一系列已存在的task,例如:Copy/Wrapper/Exec等,你可以通过设置属性来个性化使用
// Copy APKs to another folder
task copyApks(type: Copy) {
from("$buildDir/outputs/apk") {
exclude '**/*unsigned.apk', '**/*unaligned.apk'
}
into '../apks'
}
During the
configuration phase, Gradle builds a DAG based on their dependencies. It then exe‐
cutes the desired task, along with its dependencies. All tasks are configured before
any are executed.
大概原意:在配置阶段,Gradle会根据所有任务的依赖关系构建一个DAG关系图。然后延着它们的依赖关系执行特定的任务。因此,在任何任务执行之前都需要完成所有任务的配置工作。
// A custom task to print available variants
task printVariantNames() {
doLast {
android.applicationVariants.all {
variant ->println variant.name
}
}
}
// Install all the debug flavors on a single device
task installDebugFlavors() {
android.applicationVariants.all { v ->
if (v.name.endsWith('Debug')) {
String name = v.name.capitalize()
dependsOn "install$name"
}
}
}
During the initialization phase, Gradle assembles the tasks into a sequence according
to their dependencies. The result is a DAG. The “directed” term means each dependency arrow goes in one direction. “Acyclic”
means that there are no loops in the graph.
大概原意:在初始化阶段,Gradle根据各任务之间的依赖关系将任他们分解成一个序列,该序列就是一个DAG。“定向”意味着每个依赖箭头在同一个方向上。“无环”表示图中没有循环。
// Updated copy task to generate them first
task copyApks(type: Copy, dependsOn: assembleDebug) {
from("$buildDir/outputs/apk") {
exclude '**/*unsigned.apk', '**/*unaligned.apk'
}
into '../apks'
}
// Modified clean task to remove the apks directory
task clean(type: Delete) {
delete rootProject.buildDir, 'apks'
}
// Disabling all tasks that start with the word lint
gradle.taskGraph.whenReady { graph ->
graph.allTasks.findAll { it.name ==~ /lint.*/ }*.enabled = false
}
The allTasks property of the task graph invokes the getAllTasks method, using
the normal Groovy idiom. That returns a java.util.List of tasks. Groovy adds a
findAll method to List that returns only the tasks that satisfy the supplied closure.
In this case, the closure says access the name property of each task and check whether
or not it exactly matches the regular expression. Applying the “spread-dot” operator
to the resulting list disables each task in the list.
The result is that all tasks that have a name that starts with the letters lint have their
enabled property set to false , so none of them will run.
大概原意:使用Groovy语法调用getAllTasks方法获取所有的task,通过findAll方法把所有以lint开头的任务的enabled属性设置为false,这样这些任务就不会执行
// A settings.gradle file with an added module
include ':app', ':icndb'
From a Gradle perspective, Android libraries are subprojects from the root. That
means they are like Android applications, but in a subdirectory. The name of the
added module (Android Studio calls them modules) is therefore added to the set‐
tings.gradle file
大概原意:从Gradle的角度来看,Android lib库是根目录的一个子项目。这意味着他们就像一个在子目录的项目。因此,该lib库会被添加到settings.gradle文件中去
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
packagingOptions {
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'LICENSE.txt'
}
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
}
apply plug-in: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
// ... all the regular settings ...
}
dependencies {
compile project(':icndb')
}