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
}
}
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
After updating Android Studio to version 2.2 and the Gradle-plugin to 2.2.0, I get following error:
"Could not get unknown property 'assembleRelease' for project ':app' of type org.gradle.api.Project."
When I change the plugin version back to 2.1.3 the code still works, but that's no long-term option for me.
My code:
apply plugin: 'com.android.application'
dependencies {
...
}
android {
...
}
...
assembleRelease.doLast {
file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
}
Hint:
project.ext.androidVersionCode is a variable defined otherwhere and contains a build number. The code in assembleRelease.doLast shall just move/rename the generated apk file.
Thank you for advices!
tangens
tasks.whenTaskAdded { task ->
if (task.name == 'assembleRelease') {
task.finalizedBy 'yourRenameTasks'
}
}
You may rewrite your task a bit and try like this:
task renameBuildTask() << {
file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
dependsOn 'assembleRelease'
}
Also you can check this question to get better understanding.
EDIT
As #tangens said in a comment:
It works when I replace the call gradle assemble by e.g. gradle renameBuildTask. Thank you! The answer contains an error. Correct would be: task renameBuildTask() << { ... }
maybe wrap code in afterEvaluate{} will be work:
afterEvaluate {
assembleRelease.doLast {
file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
}
}
gradle-2.14.1 and android gradle plugin 2.2.0
details:
Could not get unknown property 'assembleDebug' (2.2-beta)
I had the same problem after upgrading Android Studio to 2.2 and Gradle to 2.2.
I have task copyApk that needs to be run at the end of building. For brevity, let me skip what was working before, and post only what is working right now:
tasks.create(name: 'copyApk', type: Copy) {
from 'build/outputs/apk/myapp-official-release.apk'
into '.../mobile'
rename('myapp-official-release.apk', 'myapp.apk')
}
tasks.whenTaskAdded { task ->
if (task.name == 'assembleRelease') {
task.dependsOn 'copyApk'
}
}
Gradle console shows copyApk was run near the end after packageOfficialRelease, assembleOfficialRelease, right before the last task assembleRelease. "Official" is a flavor of the app.
I got the workaround from this SO post. I essentially copied the answer here for your convenience. All credits go to the author of that post.
inside buildTypes {} method, I put this code : worked like a charm
task setEnvRelease << {
ant.propertyfile(
file: "src/main/assets/build.properties") {
entry(key: "EO_WS_DEPLOY_ADDR", value: "http://PRODUCTION IP")
}
}
task setEnvDebug << {
ant.propertyfile(
file: "src/main/assets/build.properties") {
entry(key: "EO_WS_DEPLOY_ADDR", value: "http://DEBUG IP TEST")
}
}
tasks.whenTaskAdded { task ->
if (task.name == 'assembleDebug') {
task.dependsOn 'setEnvDebug'
} else if (task.name == 'assembleRelease') {
task.dependsOn 'setEnvRelease'
}
}
you can do this:
task copyApk(dependsOn: "assembleRelease") << {
file('build/outputs/apk/app-release.apk').renameTo("AppName-1.0.0-${project.ext.androidVersionCode}.apk")
}
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)
}
}
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') {
...
}