My first SO answer, please be gentle ;)
But what are the individual steps that it does? (gradle tasks if any)
So if you want a high-level overview of what tasks ConnectedAndroidTest depends on, just running ./gradlew connectedAndroidTest
or ./gradlew cAT
(without the -q
option) will output the name of each task that cAT
depends on before it itself is executed. The task itself can't have other tasks inside it, but can depend on others coming before it.
From this answer, the gradle build
task is actually something java related, and isn't what's responsible for building the test apk. Instead, it's the assembleAndroidTest
task that comes right before connectedAndroidTest
that does it. You are right about the connectedAndroidTest
though, it actually installs and runs the test apk. But I'll come to how in a bit. The rest of my answer is goes into more detail than is necessary to use the task effectively, but is useful if you want to understand how it works.
Some background
Like many other Android gradle plug-in tasks, connectedAndroidTest is actually put together at some point in the execution phase because of the different build variants (debug, release, flavour 1, flavor 2 etc.). So connectedAndroidTest
isn't available to you in the configuration phase (when most of your build script logic is executed). Instead, once it's built, it's set as the connectedInstrumentTest
property (basically, a field) of the testVariants
property in the android
object.
As an example for clarification, if you want to access this task to manipulate it somehow (maybe add an Action
to the end of it), you can do something like this in your build.gradle
file:
android {
testVariants.all { variant ->
variant.connectedInstrumentTest.doLast {
println "This will be executed right after our connectedInstrumentTest!"
println "The name of the test type: $connectedInstrumentTest.name"
println "The type of test $connectedInstrumentTest.class"
}
}
}
And then run ./gradlew -q cAT
So here, I'm adding an action to the end of whatever task has been built and assigned to the connectedInstrumentTest
property, which is nested fairly deep in the android
object. This task will be likely beconnectedDebugAndroidTest
or something similar.
What's the task doing?
Now, from the type property I put in the last println, we can see that the class of the task is actually com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask_Decorated
. To be honest, I'm not too sure yet where that _Decorated
part comes from, but a google search for the rest of the class name provides us with the source code for the base class of the task.
The main Action
of the task is called runTests()
and shows you more or less how the task accomplishes what it does. If you follow the source code around a bit, you eventually find that the adb pm install
command will be used to install the apk.
Although I couldn't quite find it, I suspect that somewhere else the adb command adb shell am instrument -w com.package.name/android.support.test.runner.AndroidJUnitRunner
command is being used to finally drive the tests.
So I hope that wasn't too confusing - I learnt most of this very recently so some things might not be 100%. I would suggest working through the gradle docs, in particular how to create a custom plugin and a custom task, and also check out the Android gradle plug-in tools documentation.