Amazing! I wish I had found this post a few weeks ago… I actually worked my way into a very similar solution for merging unit and instrumentation test coverage. I really like the way you leverage the android jacoco task classpath instead of explicitly specifying a jacoco version in the custom task.
We also extracted the task into a separate .gradle file that contained other common tasks and can be included with the `apply from: “filename”` syntax. We had to conditionally include the instrumentation test coverage data based on the value of android.buildTypes.debug.testCoverageEnabled, in case our common script was applied to projects with unit tests but no instrumentation tests (we develop a lot of independent frameworks).
For a bit of extra flexibility, check out Robert Gouline’s custom task solution for unit test coverage (which is essentially the same, but build type and product flavor aware, much like the built-in android plugin tasks). I’m still working on our solution to follow this pattern.
I can’t help but wonder what it would take to get the android gradle plugin (or another sister plugin) to do all this out of the box…