在android studio中执行一个自定义的独立gradle任务 [英] Execute a custom independent gradle task in android studio

查看:46
本文介绍了在android studio中执行一个自定义的独立gradle任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个模块的 android 项目.我试图从其中一个模块运行自定义 gradle 任务,但每次运行该任务时,模块和其他模块中的所有其他 gradle 任务都会运行.我的任务不依赖于任何其他任务.任务:

I have an android project with multiple modules. I am trying to run a custom gradle task from one of the modules, but each time I run the task all the other gradle tasks in the module as well as in the other modules. My task is not dependent on any other tasks. Tasks :

task helloTask{
   println "Hello task"
}

我曾尝试通过 Studio 中的终端窗口以及命令行运行此任务.

I have tried running this task through the terminal window in studio as well as from command line.

推荐答案

Gradle 将在配置阶段执行所有未用 <<< 声明的任务.如果您想将任务的执行延迟到执行阶段,那么您只需添加 <<

Gradle will execute all the tasks not declared with << during the configuration phase. If you want to delay the execution of a task till the execution phase then you could just add the <<

在你的 build.gradle

task helloConfiguration { task ->
    println "Hello configuration phase task! $task.name"
}

/* Notice the `<<` this denotes to gradle to not execute
 * the closure during the configuration phase. Instead
 * delay closure's execution till the execution phase.
 */
task helloExecution << { task ->
    println "Hello execution phase task! $task.name"
}

helloExecution.dependsOn helloConfiguration

然后在执行 helloExecution 任务时,我们看到两者都在运行,顺序得到保证.接下来,如果我们只想运行配置构建的任务,我们可以单独执行,如果我们想要并且只运行单个任务.

Then when executing the helloExecution task we see both run, order ensured. Next if we only want to run the tasks that configure the build we can do that separately if we want and only run a single task.

$ gradle helloExecution
Hello configuration phase task! helloConfiguration
Hello execution phase task! helloExecution
:helloConfiguration UP-TO-DATE
:helloExecution UP-TO-DATE

BUILD SUCCESSFUL

Total time: 0.64 secs

$ gradle helloConfiguration
Hello configuration phase task! helloConfiguration
:helloConfiguration UP-TO-DATE

BUILD SUCCESSFUL

Total time: 0.784 secs

在配置阶段运行的任务将始终执行,即使没有提供任何任务,这是我希望您看到的行为.所以给出了上面的例子.注意配置任务运行但没有执行.

Tasks that run during the configuration phase will ALWAYS be executed even if no tasks are supplied, which is the behavior I expect your seeing. So given the example above. Notice the configuration task ran but not the execution.

$ gradle
Hello configuration phase task! helloConfiguration
:help

Welcome to Gradle 2.10.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL

Total time: 0.651 secs

因此,如果您有 5 个任务在配置阶段运行,那么无论命令行参数试图为执行阶段的目标调用哪个任务,您都会看到所有任务都执行了.

So if you have 5 tasks that run in the configuration phase then you would see all of them execute, regardless of the task the command line args attempted to invoke for the execution phase's target.

这篇关于在android studio中执行一个自定义的独立gradle任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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