I want to understand more about ConnectedAndroidTest Gradle task. I see that it is used to install the application and test apks and run the tests.
But what are the individual steps that it does? (gradle tasks if any)
"gradle build" seems to generate the Application apk. What task generates the test apk? And how does it(ConnectedAndroidTest) install the application and test apk? And how does it start the tests?
Thanks very much.
To answer the more general question "what are the list of tasks that task
<taskName>
executes?", there are two simple ways to find that out for any given task.The first is:
where
<taskName>
should be replaced with whatever task you care about. E.g.,./gradlew tasks --all | grep connectedDebugAndroidTest
. Note that I'm piping throughgrep
to save myself the trouble of manually sifting through the list of all tasks.The second is:
Use the task-tree plugin. Once applied, usage looks like this:
Or, as I usually prefer it:
The latter option makes the output a bit less cluttered.
My first SO answer, please be gentle ;)
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 thatcAT
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 theassembleAndroidTest
task that comes right beforeconnectedAndroidTest
that does it. You are right about theconnectedAndroidTest
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 theconnectedInstrumentTest
property (basically, a field) of thetestVariants
property in theandroid
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 yourbuild.gradle
file: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 theandroid
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 calledrunTests()
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 theadb 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.