什么是ConnectedAndroidTest执行的任务列表? [英] What are list of tasks that ConnectedAndroidTest executes?

查看:193
本文介绍了什么是ConnectedAndroidTest执行的任务列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解更多有关ConnectedAndroidTest Gradle任务的信息.我看到它用于安装应用程序,测试apk和运行测试.

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"似乎生成了应用程序apk.什么任务生成测试apk?以及它如何(ConnectedAndroidTest)安装应用程序并测试apk?以及它如何开始测试?

"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?

非常感谢.

推荐答案

我的第一个SO答案,请保持平和;)

My first SO answer, please be gentle ;)

但是它分别执行哪些步骤? (对任务进行分级)

But what are the individual steps that it does? (gradle tasks if any)

因此,如果您想大致了解任务所依赖的内容,只需运行./gradlew connectedAndroidTest./gradlew cAT(不带-q选项)将输出每个任务的名称cAT依赖于它本身执行之前.该任务本身不能包含其他任务,但是可以依赖其他任务.

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.

答案来看,gradle build任务实际上与Java相关,并且与不是负责构建测试apk的原因.相反,正是assembleAndroidTest任务紧接在connectedAndroidTest之前.您对connectedAndroidTest的看法是正确的,它实际上会安装并运行测试apk.但是我会介绍一下.我剩下的答案是比有效使用该任务所需的更多细节,但是如果您想了解它是如何工作的,它会很有用.

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.

某些背景
像许多其他Android gradle插件任务一样,connectedAndroidTest实际上在执行阶段的某个时刻放在一起,这是因为构建版本不同(调试,发行版,flavor 1,flavor 2等).因此,connectedAndroidTest在配置阶段不可用(大多数构建脚本逻辑已执行时).取而代之的是,将其构建后,将其设置为android对象中testVariants属性的connectedInstrumentTest 属性(基本上是一个字段).

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.

作为澄清的示例,如果您想访问此任务以某种方式进行操作(也许在其末尾添加Action),则可以在build.gradle文件中执行以下操作:

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"       
    }
  }
}

然后运行./gradlew -q cAT

因此,在这里,我要在已构建并分配给connectedInstrumentTest属性的任何任务的末尾添加一个动作,该属性嵌套在android对象中相当深的位置.此任务可能是connectedDebugAndroidTest或类似的内容.

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.

正在做什么?
现在,从我在上一个println中输入的type属性,我们可以看到任务的类实际上是com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask_Decorated.老实说,我还不太确定_Decorated部分的来源,但是谷歌搜索其余的类名可以为我们提供

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.

任务的主要Action被称为runTests(),它或多或少地向您展示了任务如何完成其​​工作.如果稍微遵循源代码,最终您会发现adb pm install命令将用于安装apk.

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.

尽管我找不到它,但我怀疑在其他地方使用了adb命令adb shell am instrument -w com.package.name/android.support.test.runner.AndroidJUnitRunner命令来最终进行测试.

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.

所以我希望不要太困惑-我是最近才学到的,所以有些事情可能不是100%.我建议您阅读gradle文档,特别是如何创建自定义插件和自定义任务,并查看

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.

这篇关于什么是ConnectedAndroidTest执行的任务列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆