How to set up a task that does something to APKs built by Gradle (For Android)?

I need to set up a custom task that I run after an APK is built when using Gradle for Android. Right now I actually have two tasks. One builds an XML file using the path to the APK as an input, and the other runs after the XML task (named packageConfig) and bundles the APK and the XML file into a ZIP.

Right now, I’m having trouble with my packageConfig task. It runs the python script for only the “fullRelease” product flavor, when I actually have multiple:

  • x86Debug
  • x86Release
  • armDebug
  • armRelease
  • fullDebug
  • fullRelease

My script is only executed once, and for the wrong APK. For example, I will invoke gradle like so:

$ gradlew :Applications:zApp:assemblex86debug packageConfig

I set up a print() call in my python script to output the APK path passed in, and it is:


The path I’m expecting (based on the product flavor I chose on the command line, as well as the APK I know that is there):


I’ll paste my gradle task below. Can anyone help me understand what I’m doing wrong? What corrections can I make to my script to get it to run my script on the correct APK?

task packageConfig(type: Exec) {
    def buildCommit = getBuildCommit()
    def buildBranch = getVcsBranchName()
    def buildDescription = buildBranch + '-' + buildCommit
    def buildDate = getBuildDate()

    workingDir buildscript.sourceFile.parent
    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def apk = output.outputFile
            def apkDir = apk.getParent();
            def inputConfig = '' + project.projectDir + File.separator +
            def outputConfig = apkDir + File.separator + 'PackageConfig.xml'

            commandLine 'python', '', versions.version, inputConfig,
                outputConfig, apk, buildDescription