Android Studio: Automatic copying of the build apk file to the desktop

I am trying to automatically copy the apk file generated by Android Studio to my PC desktop as well. To do this, I add the following code to build.gradle (module):

android {
    ...
    buildTypes {
        ...
        debug {
            android.applicationVariants.all { variant ->
                variant.outputs.all {output ->
                    outputFileName = new File(parent.name + ".apk")
                    def taskSuffix = variant.name.capitalize()
                    def assembleTaskName = "assemble$taskSuffix"
                    if (tasks.findByName(assembleTaskName)) {
                        def copyAPKFolderTask = tasks.create(name: "archive$taskSuffix", type: org.gradle.api.tasks.Copy) {
                            from "$buildDir/outputs/apk/debug/"
                            into "C:/Users/Bernhard/Desktop/"
                            include parent.name + ".apk"
                        }
                        tasks[assembleTaskName].finalizedBy = [copyAPKFolderTask]
                    }
                }
            }
        }
    }
}

This works, BUT the very first time it takes up to 10 minutes for the code to be processed. From then on it only takes seconds for a revised apk file to be copied to the desktop. I am grateful for any help.

Thanks, Bernhard

I have found a solution to my listed issue. The problem was that the app-debug.apk file did not even exist at the time the script was executed. This blocks the script for minutes (!). The following code copies app-debug.apk under the project name to the desktop without delay.

    debug {
        afterEvaluate {
            def apkFile = file("$buildDir/outputs/apk/debug/app-debug.apk")
            def destFile = file("C:/Users/.../Desktop/" + parent.name + ".apk")
            tasks.register("copyDebugApkToDesktop", DefaultTask) {
                dependsOn tasks.named("assembleDebug")
                doLast {
                    if (apkFile.exists()) {
                        destFile.bytes = apkFile.bytes
                        println ".apk successfully copied to " + destFile.absolutePath
                    } else {
                        println ".apk NOT copied to desktop!"
                    }
                }
            }
            tasks.named("assembleDebug").configure{
                finalizedBy("copyDebugApkToDesktop")
            }
        }
    }