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

查看:29
本文介绍了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.

但是它执行的各个步骤是什么?(如果有的话,gradle 任务)

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 ;)

但是它执行的各个步骤是什么?(如果有的话,gradle 任务)

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

因此,如果您想对 tasks ConnectedAndroidTest 所依赖的内容进行高级概述,只需运行 ./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.

this 答案来看,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 实际上是在执行阶段的某个时刻组合在一起的,因为不同的构建变体(调试、发布、风格 1、风格 2 等).所以 connectedAndroidTest 在配置阶段(当你的大部分构建脚本逻辑被执行时)对你不可用.相反,一旦它被构建,它就会被设置为 android 对象.

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 文档,特别是如何创建自定义插件和自定义任务,并查看 Android 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天全站免登陆