Gradle Build Path

I had a script tat used to copy the apps from given output folder to a server. With update Of gradle 4.1, the script has broken because the Output path has changed. Now I am trying to write the gradle script to copy the build from new directory to older path and delete the generated one

applicationVariants.all { variant ->

    
                variant.outputs.all {
                    //add here your logic to customize the name of the apk
                    outputFileName = "demoName-" + variant.buildType.name + ".apk"
                }
                variant.assemble.doLast { //assemble ->
                    //copy the apk in another directory, add here your
                    //logic to customize the destination folder
                    println "***********" + variant.outputs*.outputFile + "***************"
                    copy {
                        from variant.outputs*.outputFile
                        into "$project.buildDir/apk/"
                    }
                    //if you don't want to delete the file after copying it comment the line below
                    delete variant.outputs*.outputFile
                }
            }

When executing ./gradlewAssemble, it works fine for debug version but doesnt work for release. The error is mentioned below :-

Could not find method copy() for arguments [build_59frnzsit9pioe5hqxmxzw19v$_run_closure2$_closure7$_closure13$_closure15$_closure16@3fdf4ccd] on task ā€˜:app:assembleReleaseā€™ of type org.gradle.api.DefaultTask.

Where am i going wrong?

Does anything change if you explicitly call out project.copy { instead of just copy {?

1 Like

Thanks it works. Can i know what it changed, because earlier one was copying 1 version to the folder after that it will stop

The error indicated it was looking for the copy method on the task and failing before searching upward to the project. Since the copy method you want is on project, explicitly stating it forced it to look in the correct location and avoid whatever changed that introduced this conflict.

I have 3 variantsā€¦ it works for the 1st variant with no issues but during generation of 2nd it acts like that

./gradlew assemble is the command i run