Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

android - What are list of tasks that ConnectedAndroidTest executes?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...