Gradle Task dependsOn multiple parameters - android

I have 3 gradle tasks:first,second,third.
task first {
doLast {
println "+++++first+++++"
}
}
task second {
doLast {
println "+++++second+++++"
}
}
task third(dependsOn :[second,first]) {
doLast {
println "+++++third+++++"
}
}
execute 'gradle third' in cmd,the result is
E:\android\GradleTest>gradle third
> Task :app:first
+++++first+++++
> Task :app:second
+++++second+++++
> Task :app:third
+++++third+++++
dependsOn task order is [second,first],But why execute task order is "+++++first+++++" " +++++second+++++"?

When declaring Task Dependencies using dependsOn like that you're not specifying an order; you're just saying third depends on both one and two. Gradle can then look at the dependency graph of all the tasks in the build and decide in which order to run them all.
If you also have a dependency between one and two you should declare it explicitly.
You can do more subtle things with task ordering using mustRunAfter and shouldRunAfter but these are intendend for special cases only, and are rarely what you want. Just focus on the dependencies and let Gradle worry about the ordering.

Related

Gradle .execute method showing error after gradle update

My project was running perfect with gradle plugin version 3.0.1 after updating to version 3.4.1 I am going through sync failure.
Previously I was using wrapper 4.10.1 which is now updated to 5.1.1.
afterEvaluate(new Action<Project>() {
#Override
void execute(Project project) {
tasks.getByName("assembleRelease").doLast {
tasks.copyReleaseBuild.execute() //error is here
tasks.copyReleaseBuildToXX.execute()
tasks.copyReleaseBuildToXXXX.execute()
}
}
copyReleaseBuild task is written something like below...
task copyReleaseBuild(type: Copy) {
def releaseDir = getProjectProperty('releaseDir')
if (releaseDir?.trim()) {
//if release folder is provided
def releaseAarFile =
getProjectProperty('sourceCodeDir') + "/android-corekit/kit/build/outputs/aar/kit-release.aar"
from releaseAarFile
into releaseDir
}
task copyReleaseBuildToXX(type: Copy) {
from "./build/outputs/aar/kit-release.aar"
into "../kitwrapper/libs"
}
task copyReleaseBuildToXXXX(type: Copy) {
from "./build/outputs/aar/kit-release.aar"
into "../kitwrapperapp/libs"
}
}
I have been trying to resolve from last two days but nothing is working as I lack knowledge of groovy.
Please check error log I am getting while building the project...
It happens because you are using an updated version of gradle (check the migration to gradle v5)
The following properties and methods of TaskInternal have been removed — use task dependencies, task rules, reusable utility methods, or the Worker API in place of executing a task directly.
execute()
executer
getValidators()
addValidator()
Now you can't call the execute method directly, but you have to use the tasks dependencies to obtain the same result.
Just define in your build.gradle:
task copyReleaseBuild {
dependsOn 'assembleRelease'
//...
}
and remove this:
afterEvaluate(new Action<Project>() {
#Override
void execute(Project project) {
tasks.getByName("assembleRelease").doLast {
tasks.copyReleaseBuild.execute() //error is here
}
}
Upgrading your build from Gradle 4.x to 5.0
The following properties and methods of TaskInternal have been removed — use task dependencies, task rules, reusable utility methods, or the Worker API in place of executing a task directly.
execute()
executer
getValidators()
addValidator()
in your case seems you have dependency between tasks: copyReleaseBuild must run after assembleRelease
so, this simple build.gradle represents this dependency:
task copyReleaseBuild {
dependsOn 'assembleRelease'
doLast {
println 'run copyReleaseBuild'
}
}
task assembleRelease {
doLast {
println 'run assembleRelease'
}
}
and the command gradle copyReleaseBuild
runs both tasks:
# gradle copyReleaseBuild
> Task :assembleRelease
run assembleRelease
> Task :copyReleaseBuild
run copyReleaseBuild
BUILD SUCCESSFUL in 2s
2 actionable tasks: 2 executed

Run gradle task of type Copy from gradle code

I have gradle task:
task copyDebugConfigJson(type: Copy) {
outputs.upToDateWhen { false }
from 'some/file/dir/file.json'
into 'some/other/file/dir/'
rename ('file.json', "config.json')
}
And I can invoke it from terminal like that:
./gradlew copyDebugConfigJson
and it works. But I would like to make it happen automatically during build so for example:
android {
buildTypes {
debug {
// somehow make copyDebugConfigJson task start there
}
}
}
Or anytime before app starts running I would like this copy to happen. In general I ask you how can start type:Copy task from gradle blocks.
You can make your task run before compilation:
afterEvaluate {
android.applicationVariants.all { variant ->
variant.javaCompiler.dependsOn(copyDebugConfigJson)
}
}

About order of task execution in gradle

I have defined two tasks in my app's gradle file for my Android application. There are two flavors of my application and I want these two tasks to run only for one flavor. To achieve this, I am using the following gradle configuration:
task A << { ... }
task B << { ... }
tasks.whenTaskAdded { task ->
if(task.name.contains('assembleFreeVersion')) {
task.dependsOn A
task.dependsOn B
}
}
My question is this: Will task A always be executed after task B has been executed? If not, what can I do to ensure that without tightly coupling tasks A and B (i.e. I should still be able to run one task without running the other)?
The short answer would be no. Dependency does not specify the order. To specify the order you need to use mustRunAfter:
task A << { ... }
task B << { ... }
tasks.whenTaskAdded { task ->
if(task.name.contains('assembleFreeVersion')) {
task.dependsOn A
task.dependsOn B
B.mustRunAfter A
}
}

use packageReleaseJar in depends on

I have a Gradle task that depends on other tasks. For instance:
//Dependent tasks will be executed first before executing requested task
makeJar.dependsOn(clearJar)
makeJar and clearJar are the task which I defined as follow:
task clearJar(type: Delete) {
delete 'build/outputs/myProject.jar'
}
task makeJar(type: Copy) {
def someString = 'build/intermediates/bundles/release/'
from(someString)
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'myProject.jar')
}
I want to add another dependency to makeJar task. Gradle has a task called packageReleaseJar which I want to use.
Following script fails:
makeJar.dependsOn(clearJar,packageReleaseJar)
Do you know how can I use packageReleaseJar using dependsOn ?
This worked for me:
makeJar.dependsOn(clearJar, ':ModuleName:packageReleaseJar')
We can also add dependsOn when we define the task such as :
task makeJar(type: Copy, dependsOn: ':ModuleName:packageReleaseJar') {
...
}

Android Jacoco Test with Gradle dependsOn order with compileDebugSources and compileDebugTestSources

Other places like This Other Question will have a jacocoTestReport task that depends on testDebug. Which works.
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
...
}
However, in my case, I need to also depend on the android tasks compileDebugSources and compileDebugTestSources in addition to the testDebug. So I was hoping that the following would work
task jacocoTestReport(type: JacocoReport, dependsOn: ["compileDebugSources", "compileDebugTestSources", "testDebug"]) {
...
}
However, when I use the dependsOn property or method, the order of these dependencies aren't guaranteed as stated by Gradle. So I wanted to do something like the following outside of the task
testDebug.mustRunAfter compileDebugSources
testDebug.mustRunAfter compileDebugTestSources
but I get a compilation problem with the above is that testDebug or compileDebugSources or compileDebugTestSources gets marked as being "Could not find property 'testDebug/compileDebugSources/compileDebugTestSources'" on project :app
I'm wondering what I need to do in order to add the order of these tasks for my existing jacocoTestReport task in gradle.
Try this:
task jacocoTestReport(type: JacocoReport, dependsOn: ["compileDebugSources", "compileDebugTestSources", "testDebug"]) { ... }
tasks.testDebug.dependsOn(compileDebugTestSources)
tasks.compileDebugTestSources.dependsOn(compileDebugSources)

Categories

Resources