1

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 that assembleDebug does not have any output. zipalignDebug does however, but the task does not exist in the evaluation phase so writing from zipalignDebug does not compile. Any way to work around this? Can I define tasks in the afterEvalate-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

Community
  • 1
  • 1
Nilzor
  • 18,082
  • 22
  • 100
  • 167

1 Answers1

1

Seems like you could use external properties:

ext.output = "Initial Value"

task firstTask() << {
    output = "Output";
}

task secondTask(dependsOn: firstTask) << {
    println output;
}

Will yield: Output

If you have multiple assemble-type tasks and each one sets 'output' accordingly, then your duplication is no longer multiple tasks but multiple set statements.

Jov
  • 89
  • 4