5

I've tried to get a gradle task to execute for a lib module 'lib1' in an Android Studio project. It should run with command 'gradlew assembleDebug' or 'gradlew assemble' but it never runs.

task copy(type: Copy, dependsOn: ':lib1:assembleDebug') << {
  println "copying"
}

I tried a simpler task with no dependency and it never seems to run either.

task hello << {
  println 'hello world'
}

This runs but it's only in the configuration phase.

task hello {
  println 'hello world'
}

I need to get a copy to work in the execution phase after the library module assembled. Any clues what to do?

1 Answer 1

7

You need to add your task to the task dependency graph somehow. Typically, by making an existing task depend on it. In this case, copy depends on assembleDebug, which simply means, if you run the copy task, assembleDebug must run first. This does not mean that running assembleDebug will cause copy to run. Add this to your build.

assemble.dependsOn copy

Now running gradlew assemble will cause the copy task to execute.

Your second task is correctly defined but again, no other task depends on it so it will only execute if you run it explicitly via gradlew hello or by adding a dependency as mentioned above.

Your third task prints a line during the configuration phase because that closure is evaluated only during that phase. It is the << operator that adds a doLast action which is run at execution time.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to make the copy task always execute after assemble is finished? I simply am trying to copy the library to another location after a debug or release build.
You could do assemble.finalizedBy copy to ensure it is always run after the assemble task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.