本文介绍了在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个任务在运行,那么无论命令行args尝试为执行阶段的目标调用哪个任务,您都会看到它们全部执行.

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任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:20