问题
I want to run a Gradle task to execute some shell scripts after dexRelease or dexDebug task during the android build.
I have tried the below approach
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
}
}
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
>
回答1:
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'
}
}
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
.
来源:https://stackoverflow.com/questions/60612752/how-to-execute-a-gradle-task-after-android-dexdebug-or-dexrelease-task