Copying Android gradle to dolast does not work

I'm trying to do a very simple thing. Since gradle will delete all the files in the build directory when cleaning up, I want to move APK elsewhere when creating the release. Therefore, I added the copy task to the chain and set it as the last one. Any method I tried is invalid. So I simplified it, And added some log records to illustrate this. I don't think it will work

Using two variables, I can check whether the input and output paths are valid during task definition and execution. I can also check whether the task has been executed. I put more files in the input directory to ensure that some files exist 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
    }
}

This is the output. Here you can see that the paths are correct during definition and execution (they exist and are valid). You can also see that the task has been executed:

What's up?

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

resolvent:

First, you must know that just adding the task name to your closure (copyapks in your case) does not really mean that this task should be performed. It is the same as the specified variable, but does not perform any operation

Also, please note that the two variant paths are the same, which means that you try to copy the same file twice. In fact, this is not the only reason. You must understand that when you try to call the copy task at the execution stage, you also need to configure it at the configuration stage, so you can't change between parameters, and this task will always behave the same

If you want to call some tasks one by one, there are many options, such as task dependency, task completion or task sorting. You can read it in the official user guide. There is a method that can call some tasks like method call, but this is a very bad solution, and you must avoid using it

Therefore, if you want to invoke the replication task, you can try such a solution

assembleRelease.finalizedBy copyApks

This always calls the copy task after each assembly

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>