Gradle - Attempt to calculate the execution time of a Gradle task

Viewed 12

I'm trying to get the execution time of a specific Gradle task in order to write it into a file. I'm trying with the "connectedAndroidTest" task.

When I execute the command "gradle connectedAndroidTest xyz" and the build is successful, in the console there's written "Build successful in X seconds".

I would get the value of that "X".

I've searched in StackOverflow and on other websites and, in the end, I've written this custom task:

 task xyz {
      gradle.buildFinished { buildResult ->
      if (buildResult.failure) {
         println "Build Failed!"
      } else {
         println "Build Succeeded!"
      }
      var task = "connectedAndroidTest"

      long startTime
      def timings = []

      startTime = System.currentTimeMillis()
      logger.warn("time1=" + startTime)

      def ms = System.currentTimeMillis() - startTime
      logger.warn("time2=" + System.currentTimeMillis())

      timings.add([ms, "app:connectedAndroidTest"])

      logger.warn("timings=" + timings)
      logger.warn("${task} took ${ms}ms")
   }
}

The problem is that when I print the "ms" variable, it seems like "startTime" and System.currentTimeMillis() have the same value, and the value of "ms" will be 0, but when I build Gradle, the time used to build it isn't 0 but, for example, 2 minutes.

In particular, if I run other tasks such as "gradle connectedAndroidTest assemble" I want to get only the time used to execute the "connectedAndroidTest" task.

Besides this, is it also possible, for example, to get the information about how many tests have been executed?

The other option would be to reach the ".pb" file which is generated after the test task has been completed, but then I should get that file and automatically move it somewhere else in my project to make some actions on it. The problem is that this file is put inside the folder corresponding to the last variant of my app in alphabetical order (that's why I'm doing all this). As I know that in the future I could add other variants which maybe will become the last variants in alphabetical order, I should find a way to read dynamically that file and not using an absolute path.

Could you help me?

0 Answers
Related