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') {
...
}
Related
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.
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
I want to build dependency.
I have gcm-src.jar file.and i want to show compile files('libs/gcm-src.jar') in app build.gradle.
Use this in your gradle file
// For making jar
task clearJar(type: Delete) {
delete 'build/libs/nameOfYourJar.jar'
}
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
rename('classes.jar', 'nameOfYourJar.jar')
}
makeJar.dependsOn(clearJar, build)
I have gradle task which unbacks lib-debug.aar
need execute this task after crate this lib-debug.aar file
task unzip<<{
copy {
def aarFile = file("${buildDir}/outputs/aar/lib-debug.aar")
def outputDir = file("${buildDir}/outputs/eclipse")
from zipTree(aarFile)
into outputDir
}
You can just do:
unzip.dependsOn assemble
That will make it so that whenever you run the unzip task, the assemble task has to have run before.
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)