gradle复制为doLast不起作用 [英] gradle copy as doLast not working

查看:802
本文介绍了gradle复制为doLast不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一件非常简单的事情。由于gradle在清理时会删除构建目录中的所有文件,因此我想在创建发行版本时将apk移到其他地方。因此,我将复制任务添加到了链中,并将其设置为最后一个。我尝试过的所有方法都无效。因此,我对其进行了简化,并添加了一些日志记录来说明这一点。我认为这是行不通的。

I am trying to do a very simple thing. As gradle removes all files in the build dir when cleaning I want to move the apks somewhere else when creating release versions. So I added a copy task into the chain and I set it to be the last. Anything I tried did't work. So I simplified it and added some logging to make a point. I think it just doesn't work.

使用两个变量,我可以在任务定义时间和执行时间检查输入和输出路径是否有效。我还可以检查任务是否已执行。我在输入目录中放置了更多文件,以确保在任何情况下都存在一些文件。这是脚本:

Using two variables, I can check that at task definition time and execution time the input and output paths are valid. I can also check that the task is executed. I put some more files in the input directory to make sure there is also something there in any case. This is the script:

def buildPath
def outPath
task copyApks(type: Copy) {

    buildPath = "$buildDir\\outputs\\apk"
    outPath ="$buildDir\\outputs\\apk2"

    logger.error("Source Folder is $buildPath")
    logger.error("Destination Folder is $outPath")

    from buildPath
    into outPath
}


assembleRelease.doLast {
    android.applicationVariants.all { variant ->
        println "Variant  $variant.name"
        logger.error("Source Folder is $buildPath")
        logger.error("Destination Folder is $outPath")
        copyApks
    }
}

这是输出,可以看到路径在定义和执行时都是正确的(它们存在并且有效)。也可以看到任务已执行:

And this is the output, where one can see that the paths are correct (they exist and are valid) both at definition and execution time. Also one can see that the task is executed:

出什么问题了?

Executing external task 'assembleRelease'...
Parallel execution with configuration on demand is an incubating feature.
Source Folder is C:\Users\Administrator\Projects\Gradle\MB6\app\build\outputs\apk
Destination Folder is C:\Users\Administrator\Projects\Gradle\MB6\app\build\outputs\apk2
................
some other gradle logs
................
:app:assembleRelease
Variant  debug
Source Folder is C:\Users\Administrator\Projects\Gradle\MB6\app\build\outputs\apk
Destination Folder is C:\Users\Administrator\Projects\Gradle\MB6\app\build\outputs\apk2
Variant  release
Source Folder is C:\Users\Administrator\Projects\Gradle\MB6\app\build\outputs\apk
Destination Folder is C:\Users\Administrator\Projects\Gradle\MB6\app\build\outputs\apk2

BUILD SUCCESSFUL


推荐答案

首先,您必须知道,只需将任务名称添加到您的闭包,在您的情况下是 copyApks ,并不真正意味着应该执行此任务。就像指定变量一样,但是却无济于事。

First of all, you have to know, that just adding the task name into your closure, in your case it's copyApks, doesn't really mean that this task should be executed. It's just the same, as you specified a variable, but do nothing with it.

还有一点,请注意,两个变量路径都相同,这意味着您试图复制两次相同的文件。实际上,这不是唯一的原因,您必须了解,在执行阶段尝试调用复制任务时,仍在配置阶段对其进行了配置,因此无法在参数之间进行更改,并且

And one more, note, the both variants paths are the same, that means that you are trying to copy tha same files twice. Actually, that not the only reason, you have to understand, that your copy task is configured yet in the configuration phase, when you are trying to call it during the execution phase, so you can't change it's from and into parameters, and this task will always behave the same.

如果您要一个接一个地调用某些任务,则有很多选择,例如任务依赖性,任务完成或任务排序。 。您可以在官方用户指南中进行阅读。有一种方法可以像方法调用那样调用某些任务,但这是一个非常差的解决方案,您必须避免使用它。

If you want to call some tasks one after another, you have a number of choices, like task dependencies, task finalization or task ordering. You can read about it in the official user guide. There is a way to call some task like a method call, but this is a very poor solution and you have to avoid using it.

因此,如果要调用复制任务,那么您可以尝试这样的解决方案

So, if you want to call a copy task, then you may try solution like this

assembleRelease.finalizedBy copyApks

每次组装完成后,它将始终调用复制任务。

This will call a copy task always every time assembling is done.

这篇关于gradle复制为doLast不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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