Dim
Dim

Reputation: 4837

Run task once at the end in gradle

I need some task what will run at the end. Lets say some println. I seen multiple examples of how to run task at the end and they always depend on other task, so I don't really get how to do it and I don't have tasks. For now I have some piece of code:

if (releaseBol) 
{
 // Some of my code
 // Run twice, one for lab and other for prod
 println    fileName
}

That piece of code run twice, once for lab flavor and one for prod flavor in case I run:

MyApp:assembleRelease -PRELEASE=1

I need this peace of code to run only once, so for that I need to run it once at the end.

My full gradle:

import org.tmatesoft.svn.core.wc.*

apply plugin: 'com.android.application'

def BuildNumberFile = new File('./BuildNumber.txt')
def BuildNumber = 'BRND'
def _versionName = '1.0.1'
def _applicationId = 'com.myapp.android.pic'
def _versionCode = 17
def fileName = ''
def obfuscated = false
def releaseBol = false


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    lintOptions {
        abortOnError false
    }

    def lines = BuildNumberFile.readLines()
    BuildNumber = 'S'+lines.get(0)

    defaultConfig {
        applicationId _applicationId
        minSdkVersion 15
        targetSdkVersion 23
        versionCode _versionCode
        versionName _versionName
        multiDexEnabled true
        resValue 'string', 'BUILD_NUMBER_RES', BuildNumber


    }

    if (project.hasProperty('RELEASE') && project.ext.RELEASE == '1')
        releaseBol = true

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(
                    output.outputFile.parent,
                    output.outputFile.name.replace(".apk", "-${variant.versionName}." + BuildNumber + "-" + getSvnRevision() + ".apk"))
            fileName=output.outputFile.name

        }


        variant.assemble.doLast {
            variant.outputs.each { output ->
                println "aligned " + output.outputFile
                println "unaligned " + output.packageApplication.outputFile

                File unaligned = output.packageApplication.outputFile;
                File aligned = output.outputFile
                if (!unaligned.getName().equalsIgnoreCase(aligned.getName())) {
                    println "deleting " + unaligned.getName()
                    unaligned.delete()
                }

            }

            if (releaseBol) {

                // Some of my code
                // Run twice, one for lab and other for prod
                println fileName
            }
        }
    }
    signingConfigs {
        release {
            storeFile file('myapp')
            storePassword "myapp123"
            keyAlias 'myapp'
            keyPassword "myappmobile"
        }
    }

    productFlavors {
        prod {


        }

        lab {

        }

    }

    buildTypes {
        release {
            minifyEnabled obfuscated
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-android.txt'
            signingConfig signingConfigs.release
        }


    }


    dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "4g"
        jumboMode true
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':trunk')
}

It just help me if you can add some println to this gradle that will run once at the end.

Upvotes: 2

Views: 2197

Answers (1)

Opal
Opal

Reputation: 84874

You need to add an instance of BuildListener:

class BA extends BuildAdapter {
    void buildFinished(BuildResult result) {
       println "finished"
    }
}

gradle.addListener(new BA())

EDIT

When it comes to releaseBol it can be done in the following way:

project.ext.releaseBol = false

class BA extends BuildAdapter {

    Project project

    BA(Project project) {
      this.project = project
    }

    void buildFinished(BuildResult result) {
       println "finished $project.releaseBol"
    }
}

gradle.addListener(new BA(project))

EDIT 2

You can also utilize gradle's TaskExecutionGraph. With whenReady closure you can get info that the graph is ready and filled with tasks. At this time there's no option to modify the graph (add/remove task, change order) but you can (via getAllTasks) get the last one and... add an action. Here's how it looks like:

task someTask << {
  println "run"
}

gradle.taskGraph.whenReady {
  gradle.taskGraph.allTasks[-1] << {
    println 'finished'
  }
}

Upvotes: 4

Related Questions