Hello Gradle 任務

  • 多種方式創(chuàng)建任務
//第一種是直接已一個任務名字創(chuàng)建任務

def Task exCreateTask=task(exCreateTask)
exCreateTask.doLast{
    println "創(chuàng)建方法原型為:Task task(String name) thorows InvalidUserDataException"
}


// 第二種以一個任務名字+一個對應該任務配置的Map對象來創(chuàng)建任務

def Task exCreateTask=task(exCreateTask,group:BasePlugin.BUILD_GROUP)
exCreateTask.doLast{
    println "創(chuàng)建方法原型為:Task task(String name,Map<String,?> args) thorows InvalidUserDataException"

    println "任務分組:${exCreateTask.group}"
}
// 第三種以一個任務名字+閉包配置的方式

task exCreateTask{
    description '演示任務創(chuàng)建'

    doLast{
        println "創(chuàng)建原型為:Task task(String name,Closure configureClosure)"
        println "任務描述:${description}"
    }
}


tasks.create('exCreateTask2'){
    description '演示任務創(chuàng)建'

    doLast{
        println "創(chuàng)建原型為:Task task(String name,Closure configureClosure)"
        println "任務描述:${description}"
    }
}

  • 多種方式訪問任務
task exCreateTask{
    description '演示任務創(chuàng)建'

    doLast{
        println "創(chuàng)建原型為:Task task(String name,Closure configureClosure)"
        println "任務描述:${description}"
    }
}



tasks['exCreateTask'].doLast{
    println tasks.findByPath(':gradle-test:exCreateTask')
    // println tasks.getByPath(':gradle-test:exCreateTask')

    println tasks.findByName('exCreateTask')
    println tasks.getByName('exCreateTask')
}


任務分組和描述

def  Task myTask=task exGroupTask
myTask.group=BasePlugin.BUILD_GROUP
myTask.description='這是一個構(gòu)建的引導任務'
myTask.doLast{
    println "group:${group} description:${description}"
}

任務排序

task task1{
    doLast{
    println 'task1 run...'
    }
}

task task2{
   doLast{
    println 'task2 run...'
   }
}

//task1必須在task2執(zhí)行之后執(zhí)行
task1.mustRunAfter(task2)

//task1應該在task2執(zhí)行之后執(zhí)行
//task1.shouldRunAfter task2

任務的啟用和禁用

task myTask{
    doLast{
        println 'disenabledTask'
    }
}

myTask.enabled=false

任務的onlyIf斷言

斷言就是一個條件表達式,Task有一個onlyIf方法,它接受一個閉包作為參數(shù),如果該閉包返回true則該任務執(zhí)行,否則跳過。

final String BUILD_APP_ALL="all"
final String BUILD_APPS_SHOUFA="shoufa"
final String BUILD_APPS_EXCLUDE_SHOUFA="exclude_shoufa"


task exQQRelease{
    doLast{
        println "打應用寶的包"
    }
}

task exBaiduRelease{
    doLast{
        println "打百度的包"
    }
}


task exHuaweiRelease{
    doLast{
        println "打華為的包"
    }
}


task exXiaomiRelease{
    doLast{
        println "打小米的包"
    }
}

task build{
    group BasePlugin.BUILD_GROUP
    description "打渠道包"
}

build.dependsOn exQQRelease,exBaiduRelease,exHuaweiRelease,exXiaomiRelease


exQQRelease.onlyIf{
    def execute=false

    if(project.hasProperty("build_apps")){
        Object buildApps=project.property("build_apps")
        if(BUILD_APPS_SHOUFA.equals(buildApps)){
            execute=true
        }
    }else{
        execute=true
    }

    execute
}


exBaiduRelease.onlyIf{
    def execute=false

    if(project.hasProperty("build_apps")){
        Object buildApps=project.property("build_apps")
        if(BUILD_APPS_SHOUFA.equals(buildApps)){
            execute=true
        }
    }else{
        execute=true
    }

    execute
}


exHuaweiRelease.onlyIf{
    def execute=false

    if(project.hasProperty("build_apps")){
        Object buildApps=project.property("build_apps")
        if(BUILD_APPS_EXCLUDE_SHOUFA.equals(buildApps)){
            execute=true
        }
    }else{
        execute=true
    }

    execute
}

exXiaomiRelease.onlyIf{
    def execute=false

    if(project.hasProperty("build_apps")){
        Object buildApps=project.property("build_apps")
        if(BUILD_APPS_EXCLUDE_SHOUFA.equals(buildApps)){
            execute=true
        }
    }else{
        execute=true
    }

    execute
}



  • 打所有渠道包

命令行中-P的意思為Project指定K-V格式的屬性鍵值對,格式為-PK=V


  • 打首發(fā)包


  • 打非首發(fā)包


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.多種方式創(chuàng)建任務: 第一種:直接以一個任務名字創(chuàng)建任務的方式 def Task ex6=task(ex6) e...
    明明_白_白閱讀 1,934評論 0 1
  • 個人博客:http://www.milovetingting.cn Gradle任務 多種方式創(chuàng)建任務 1、直接以...
    milovetingting閱讀 531評論 0 1
  • 上一章我們已經(jīng)介紹了Gradle腳本的基礎(chǔ),在其中我們也強調(diào)了Gradle中最要的Projects和Tasks這兩...
    acc8226閱讀 2,058評論 4 4
  • 在 Android Studio 構(gòu)建的項目中,基于 Gradle 進行項目的構(gòu)建,同時使用 Android DS...
    Ant_way閱讀 7,587評論 0 16
  • 這是16年5月份編輯的一份比較雜亂適合自己觀看的學習記錄文檔,今天18年5月份再次想寫文章,發(fā)現(xiàn)簡書還為我保存起的...
    Jenaral閱讀 3,142評論 2 9

友情鏈接更多精彩內(nèi)容