Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle task doesn't execute in Android Studio

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?

like image 336
1192805 Avatar asked Sep 27 '14 00:09

1192805


1 Answers

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.

like image 121
Mark Vieira Avatar answered Oct 08 '22 22:10

Mark Vieira