一、多种创建方式任务

这个那么多,其实我们平时用的最多还是闭包的形式。记得太多容易混,别的看到能分别出来这个是创建就可以了。

二、多种方式访问任务

task 和 tasks
tasks 其实就是一个数组,根据 task 的名字可以查到。没啥意思。就是方便。用到在深究。
tasks[‘task1’]

三、分组和描述符

group 分类 分组都可以
description 描述符

四、<<

<< 相当于 doLast (新版本就没有了)

五、执行顺序

doFirst -> doSelf -> doLast
taskB.shouldRunAfter(taskA) 只是应该,可能也没有
taskB.MustRunAfter(taskA) 这个就必须了

六、任务的启用和禁用

taskA.enabled = true
taskA.enabled = false

七、断言

如果我们首发的渠道是应用宝和百度,直接执行 build 会编译出来所有的包,这个太慢了,所以就可以用这个断言 onlyIf 。

  1. final String BUILD_APPS_ALL = 'all'
  2. final String BUILD_APPS_SHOUFA = 'shoufa'
  3. final String BUILD_APPS_EXCLUDE_SHOUFA = 'exclude_shoufa'
  4. task exBaiduRelease {
  5. doLast{
  6. println '打包百度'
  7. }
  8. }
  9. task exHuaWeiRelease {
  10. doLast{
  11. println '打包华为'
  12. }
  13. }
  14. task exMeizuRelease {
  15. doLast{
  16. println '打包魅族'
  17. }
  18. }
  19. task exMiRelease {
  20. doLast{
  21. println '打包小米'
  22. }
  23. }
  24. exMeizuRelease.mustRunAfter exMiRelease
  25. task build2{
  26. group BasePlugin.BUILD_GROUP
  27. description '大渠道包'
  28. println description
  29. }
  30. build2.dependsOn exBaiduRelease,exHuaWeiRelease,exMeizuRelease,exMiRelease
  31. exBaiduRelease.onlyIf{
  32. def execute = false
  33. if(project.hasProperty('build_apps')){
  34. Object buildAppps = project.property('build_apps')
  35. if(buildAppps.equals(BUILD_APPS_SHOUFA)||buildAppps.equals(BUILD_APPS_ALL)){
  36. execute = true
  37. }
  38. }
  39. execute
  40. }
  41. exHuaWeiRelease.onlyIf{
  42. def execute = false
  43. if(project.hasProperty('build_apps')){
  44. Object buildAppps = project.property('build_apps')
  45. if(buildAppps.equals(BUILD_APPS_SHOUFA)||buildAppps.equals(BUILD_APPS_ALL)){
  46. execute = true
  47. }
  48. }
  49. execute
  50. }
  51. exMeizuRelease.onlyIf{
  52. def execute = false
  53. if(project.hasProperty('build_apps')){
  54. Object buildAppps = project.property('build_apps')
  55. if(buildAppps.equals(BUILD_APPS_EXCLUDE_SHOUFA)||buildAppps.equals(BUILD_APPS_ALL)){
  56. execute = true
  57. }
  58. }
  59. execute
  60. }
  61. exMiRelease.onlyIf{
  62. def execute = false
  63. if(project.hasProperty('build_apps')){
  64. Object buildAppps = project.property('build_apps')
  65. if(buildAppps.equals(BUILD_APPS_EXCLUDE_SHOUFA)||buildAppps.equals(BUILD_APPS_ALL)){
  66. execute = true
  67. }
  68. }
  69. execute
  70. }

不得不学之「 Gradle」 ④ Gradle 任务 - 图1

八、任务规则

我们创建的任务都是在 TaskContainer 里的,所有我们访问任务的时候都是通过 TaskContainer 进行访问。TaskContainer 又是一个 NamedDomainObjectCollection(继承它),也就是 NamedDomainObjectCollection 的规则。NamedDomainObjectCollection 是一个具有唯一不变名字的域对象的集合。它里面的所有元素都有一个唯一不变的名字,String 类型的。我们可以通过名字来获取元素。也就是可以通过任务名来获取任务。至于怎么加规则,我们简单写一下。等以后要用的时候再深究。

  1. tasks.addRule('增加规则,这是规则描述'){ String taskName ->
  2. task(taskName){
  3. doLast{
  4. println "该 task ${taskName} 不存在 "
  5. }
  6. }
  7. }
  8. ask exBaiduRelease {
  9. dependsOn exYingYongBao
  10. doLast{
  11. println '打包百度'
  12. }
  13. }
  14. exBaiduRelease.mustRunAfter exYingyongBao

不得不学之「 Gradle」 ④ Gradle 任务 - 图2

划重点: 本文是完全参考 《Android Gradle 权威指南》而写,作为学习笔记使用。

补充:直接用 ext.build_apps

  1. final String BUILD_APPS_ALL = 'all'
  2. final String BUILD_APPS_SHOUFA = 'shoufa'
  3. final String BUILD_APPS_EXCLUDE_SHOUFA = 'exclude_shoufa'
  4. ext.build_apps = "all"
  5. task exBaiduRelease {
  6. doLast{
  7. println '打包百度'
  8. }
  9. }
  10. task exHuaWeiRelease {
  11. doLast{
  12. println '打包华为'
  13. }
  14. }
  15. task exMeizuRelease {
  16. doLast{
  17. println '打包魅族'
  18. }
  19. }
  20. task exMiRelease {
  21. println "exMiRelease"
  22. doLast{
  23. println '打包小米'
  24. }
  25. }
  26. exMeizuRelease.mustRunAfter exMiRelease
  27. task build2{
  28. group BasePlugin.BUILD_GROUP
  29. description '打渠道包'
  30. println "build2"
  31. doLast{
  32. println description
  33. }
  34. }
  35. build2.dependsOn exBaiduRelease,exHuaWeiRelease,exMeizuRelease,exMiRelease
  36. exBaiduRelease.onlyIf{
  37. def execute = false
  38. if(project.hasProperty('build_apps')){
  39. Object buildAppps = project.property('build_apps')
  40. if(buildAppps.equals(BUILD_APPS_SHOUFA)||buildAppps.equals(BUILD_APPS_ALL)){
  41. execute = true
  42. }
  43. }
  44. execute
  45. }
  46. exHuaWeiRelease.onlyIf{
  47. def execute = false
  48. if(project.hasProperty('build_apps')){
  49. Object buildAppps = project.property('build_apps')
  50. if(buildAppps.equals(BUILD_APPS_SHOUFA)||buildAppps.equals(BUILD_APPS_ALL)){
  51. execute = true
  52. }
  53. }
  54. execute
  55. }
  56. exMeizuRelease.onlyIf{
  57. def execute = false
  58. if(project.hasProperty('build_apps')){
  59. Object buildAppps = project.property('build_apps')
  60. if(buildAppps.equals(BUILD_APPS_EXCLUDE_SHOUFA)||buildAppps.equals(BUILD_APPS_ALL)){
  61. execute = true
  62. }
  63. }
  64. execute
  65. }
  66. exMiRelease.onlyIf{
  67. def execute = false
  68. if(project.hasProperty('build_apps')){
  69. Object buildAppps = project.property('build_apps')
  70. if(buildAppps.equals(BUILD_APPS_EXCLUDE_SHOUFA)||buildAppps.equals(BUILD_APPS_ALL)){
  71. execute = true
  72. }
  73. }
  74. execute
  75. }