3

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:

What is wrong?

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
Gogu CelMare
  • 699
  • 6
  • 15

1 Answers1

3

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.

cmcginty
  • 113,384
  • 42
  • 163
  • 163
Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • Gradle sync failed: Gradle DSL method not found: 'finalisedBy()' – Gogu CelMare Jun 28 '16 at 06:37
  • Yep, had misspelled too, it has to be finalizedBy – Stanislav Jun 29 '16 at 02:25
  • AH now I understand u don't answer questions you're here to let everybody know you read a book... lol. Some people are smart and some people just show off.. I'll read the book and get back to you... without spelling mistakes – Gogu CelMare Aug 09 '16 at 01:11
  • Here in Australia we have a bit of a habit of not using those words as in American English. I apologize for that... we don't really speak as good English as you do in Russia.. – Gogu CelMare Aug 10 '16 at 11:12