applyplugin:'com.android.application'
android {
compileSdkVersion26
buildToolsVersion"26.0.2"
defaultConfig {
applicationId"com.study.gradle.gradledemo"
minSdkVersion15
targetSdkVersion26
versionCode1
versionName"1.0"
testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabledfalse
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir:'libs',include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
excludegroup:'com.android.support',module:'support-annotations'
})
compile'com.android.support:appcompat-v7:26.0.0-alpha1'
testCompile'junit:junit:4.12'
}
/*Task对象 Extension对象 Project对象 Gradle对象 闭包 方法*/
/*--------------------------------------------------------Plugin 插件------------------------------------------------------*/
//自定义插件,Extension --》插件(Project)---》Task
applyplugin: GreetingPlugin
class GreetingPluginimplements Plugin {
void apply(Project project) {
//通过插件在Project中添加任务Task对象,在任务执行的完毕后执行闭包中的代码。
project.task('testpluging') {
doLast {
println"helloworld from the GreetingPlugin"
def android = project.extensions.getByType(com.android.build.gradle.AppExtension)
//可以看出就是遍历集合把对象传递给闭包。
android.applicationVariants.all { variant ->
def appID = variant.generateBuildConfig.appPackageName
def variantData = variant.variantData
def scope = variantData.scope
String generateBuildConfigTaskName = variant.getVariantData().getScope().getGenerateBuildConfigTask().name
println"appID:" + appID
println"scope:" + scope
println"generateBuildConfigTaskName:" + generateBuildConfigTaskName
}
}
}
//在创建任务对象的时候就执行闭包中的代码。
project.task("testpluging1", {
def android = project.extensions.getByType(com.android.build.gradle.AppExtension)
//可以看出就是遍历集合把对象传递给闭包。
android.applicationVariants.all { variant ->
def appID = variant.generateBuildConfig.appPackageName
def variantData = variant.variantData
def scope = variantData.scope
String generateBuildConfigTaskName = variant.getVariantData().getScope().getGenerateBuildConfigTask().name
println"appID1:" + appID
println"scope1:" + scope
println"generateBuildConfigTaskName1:" + generateBuildConfigTaskName
}
})
//通过Extesion来获取额外的对象。
project.extensions.add("extensionTest", ExtensionTest)
project.task("testpluging2", {
doLast {
//获取额外的对象并在task中使用
ExtensionTest extensionTest = project.extensionTest;
println"extensionTest:" + extensionTest
}
})
//通过NamedDomainObjectContainer获取多个额外对象 ,提供一个存放ExtensionTest对象的容器
NamedDomainObjectContainer extensionTests = project.container(ExtensionTest);
//把存放ExtensionTest对象的容器添加project的extensions属性中。(*****)
project.extensions.add("extensionTests", extensionTests)
project.task("testpluging3", {
doLast {
def extensionTestss = project.extensionTests;
println"extensionTestss:" + extensionTestss
}
})
//获取额外的混合对象,通过project创建一个容器对象
NamedDomainObjectContainer extensionTests1 = project.container(ExtensionTest);
Team team =new Team(extensionTests1);
project.extensions.add("team", team)
project.task("testpluging4", {
doLast {
def team1 = project.team;
println"team1:" + team1
}
})
}
}
/*--------------------------------------------------------Extension 延伸------------------------------------------------------*/
//在Project添加简单的扩展对象extensions,在对象中可以定义构造方法和普通的方法。就是java对象。
class ExtensionTest {
Stringname;
int age;//默认为0
public ExtensionTest(name) {
this.name = name;
}
public ExtensionTest() {
}
@Override
String toString() {
return "name:" +name +" age:" +age;
}
}
//1:单个Extentsion
extensionTest {
name'testext'
}
//2:列表式Extension
extensionTests {
//先给name赋值为ExtensionTest,然后在赋值为test。
ExtensionTest {
name ='test'
age =6
}
//使用的是ExtensionTest带参数的构造方法
testext {
age =1
}
testext1 {
age =4
}
extensionTest {
name ='test9'
age =5
}
extension {
age =5
}
}
//3:单个对象包含列表
class Team {
Stringname;
int count;
NamedDomainObjectContainerlist;
public Team(NamedDomainObjectContainer list) {
this.list = list;
}
public void setName(name) {
this.name = name;
}
//定义一个方法,通过闭包配置list属性。(****)
def list(Closure closure) {
list.configure(closure)
}
@Override
String toString() {
return "name:" +name +"count:" +count +list.toListString()
}
}
team {
//setName就是team中定义的setName()方法,通过该方法给Team的name属性赋值。(*****)
setName"team"
//不通过方法直接赋值
count =2
//list就是team中定义的list方法,传递的参数是闭包类型
list {
testext {
}
testext1 {
}
}
}
/*-------------------------------------------------------- 监听------------------------------------------------------*/
tasks.whenTaskAdded { task ->
task.ext.srcDir ='src/main/java'
}
Action action =new Action() {
@Override
void execute(Task task) {
task.ext.test ='test'
println'taskName:' + task.name;
println'test:' + task.ext.test;
}
}
tasks.whenTaskAdded(action)
gradle.afterProject { project, projectState ->
if (projectState.failure) {
println"Evaluation of $projectFAILED"
}else {
println"Evaluation of $projectsucceeded"
}
}
/*--------------------------------------------------------Task 任务------------------------------------------------------*/
/*
ContextAwareTaskAction extends Action
每个Task对象里面都有一个private final List actions = new ArrayList();
存放Action的集合对象。Closure也会通过适配器模式,封装Closure,实现ContextAwareTaskAction接口。private static class ClosureTaskAction implements ContextAwareTaskAction {
private final Closure closure;
把Closure转化为Action对象。*/
ext.srcDir =""
//在Terminal中执行命令:gradlew sayGreeting1
//创建一个名字为sayGreeting1的Task对象,并调用其<<方法向Task尾部添加Action,表示该Task执行到最后的时候执行闭包里面的代码。
//重点在执行时(*****)
task sayGreeting1 <<{
//监听一定要在task定义前定义好,然后就可以访问到在监听中为每个task设置的额外属性了(ext.test)
println'sayGreeting1'+ext.test
println rootProject.ext.springVersion;
}
//在Terminal中执行命令:gradlew sayGreeting2
//创建一个名字为sayGreeting2的Task对象,该Task创建的时候就执行闭包里面的代码。
//重点在创建时(*****)
task sayGreeting2 {
println'sayGreeting2' +srcDir
println rootProject.ext.springVersion;
}
sayGreeting2.dependsOn sayGreeting1
//在Terminal中执行命令:gradlew sayGreeting3
/*
日志分析::app:sayGreeting1
sayGreeting1
3.1.0.RELEASE
:app:sayGreeting3
Hello!
可以看出先执行:app:sayGreeting1再执行:app:sayGreeting3(*****)*
* */
task sayGreeting3(dependsOn: sayGreeting1) {
doLast {
println file(greetingFile).text
}
}
//在Terminal中执行命令:gradlew byTasks
Task byTasks = tasks.create("byTasks");
byTasks.doFirst {
println'Create Task by tasks!'
}
ext.greetingFile ="$buildDir/hello.txt"
class CreateFileTaskextends DefaultTask {
def destination
File getDestination() {
//使用project创建一个文件对象
project.file(destination)
}
@TaskAction
def create() {
def file = getDestination()
file.parentFile.mkdirs()
file.write"Hello!"
}
}
task createTxt(type: CreateFileTask) {
destination = { project.greetingFile }
}
task distribution {
doLast {
println"We build the zip with version=$version"
}
}
task release(dependsOn:'distribution') {
doLast {
println'We release now'
}
}
gradle.taskGraph.whenReady {taskGraph ->
if (taskGraph.hasTask(release)) {
version ='1.0'
}else {
version ='1.0-SNAPSHOT'
}
}
TaskExecutionGraphListener taskExecutionGraphListener =new TaskExecutionGraphListener() {
@Override
void graphPopulated(TaskExecutionGraph taskExecutionGraph) {
List list = taskExecutionGraph.allTasks;
for(Task task :list){
println'taskName:'+task.name;
}
}
}
gradle.taskGraph.addTaskExecutionGraphListener(taskExecutionGraphListener)
task ok
task broken(dependsOn: ok) {
doLast {
throw new RuntimeException('broken')
}
}
gradle.taskGraph.beforeTask { Task task ->
println"executing $task..."
}
gradle.taskGraph.afterTask { Task task, TaskState state ->
if (state.failure) {
println"FAILED"
}
else {
println"done"
}
}