本文介绍了android dexDebug或dexRelease任务后如何执行gradle任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android构建过程中,在dexRelease或dexDebug任务之后运行Gradle任务来执行一些shell脚本.

I want to run a Gradle task to execute some shell scripts after dexRelease or dexDebug task during the android build.

我尝试了以下方法

task taskAfterDex(type:Exec) {

    workingDir '.'
    executable 'sh'
    args "-c", "source scriptskAfterDex.sh"
    ignoreExitValue true
    doLast {
        println "taskAfterDex completed"
    }
}


tasks.whenTaskAdded { task ->
    if (task.name == 'dexRelease') {
        task.dependsOn taskAfterDex
    }
}

但是没有获得执行阶段日志,或者在dexRelease/Debug之后它没有执行.

but am not getting the execution phase log or it's not executing after dexRelease/Debug.

>  Gradle 4.8
>
>
> Build time:   2018-06-04 10:39:58 UTC Revision:
> 9e1261240e412cbf61a5e3a5ab734f232b2f887d
>
> Groovy:       2.4.12 Ant:          Apache Ant(TM) version 1.9.11
> compiled on March 23 2018 JVM:          1.8.0_151 (Oracle Corporation
> 25.151-b12) OS:           Mac OS X 10.15.2 x86_64
>

推荐答案

您真正想要的是完成任务.另外,whenTaskAdded对于之前已添加的任务不起作用;使用all可能更安全:

What you actually want is a finalizer task. Also, whenTaskAdded doesn’t work for tasks that have already been added before; using all may be safer:

tasks.all { task ->
    if (task.name == 'dexRelease') {
        task.finalizedBy 'taskAfterDex'
    }
}

正如@Blaz在问题注释中指出的那样,您使用dependsOn的方法则相反:它将taskAfterDex作为dexRelease的依赖项添加,即taskAfterDex之前运行 dexRelease.

As @Blaz has noted in the question comments, your approach with dependsOn does the opposite: it adds taskAfterDex as a dependency of dexRelease, i.e., taskAfterDex is run before dexRelease.

这篇关于android dexDebug或dexRelease任务后如何执行gradle任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 00:18