So I want to create a post-build hook which copies the compiled APK file to a new folder and give it a name which includes the version code. This is what I've got so far:
task copyToPublishFolderDebug(type: Copy, dependsOn: assembleDebug ) {
def targetFile = "MyApp-debug-" + android.defaultConfig.versionCode + ".apk"
from ('build/outputs/apk/MyApp-debug.apk') {
rename 'MyApp-debug.apk', targetFileName
}
into '../build/publish/'
}
assembleDebug.finalizedBy copyToPublishFolderDebug
This works but I think it can improved
The ideal solution would not be to hard-code the input file name but to pipe the output from
assembleDebug
to this task. (Related: https://stackoverflow.com/a/30637190/507339) The problem is thatassembleDebug
does not have any output.zipalignDebug
does however, but the task does not exist in the evaluation phase so writingfrom zipalignDebug
does not compile. Any way to work around this? Can I define tasks in theafterEvalate
-phase?I have to copy-paste this task to a 90% identical one for the release build (and any other build types I've created). Any way to in a simple way DRY that thing up?
edit: Misunderstood problem #1, thought it was a file not a directory. Copying is done correctly except it creates a folder and does not rename
edit2: Found out how to rename so that's no longer an issue