Android - Only execute Gradle task on release build variant

Add doFirst or doLast for the build type you are interested in.

android.applicationVariants.all {  variant ->
    if ( variant.buildType.name == "release"){
        variant.assemble.doLast { // Can also use doFirst here to run at the start.
            logger.lifecycle("we have successfully built $v.name and can post a messaage to remote server")
        }
    }
}

I had to do something like this to check build version:

buildTypes {
    applicationVariants.all { variant ->
        variant.outputs.each {output ->
            def project = "AppName"
            def separator = "_"
            /*def flavor = variant.productFlavors[0].name*/
            def buildType = variant.variantData.variantConfiguration.buildType.name
            def versionName = variant.versionName
            def versionCode = variant.versionCode
            def date = new Date();
            def formattedDate = date.format('yyyyMMdd_HHmm')
            if (variant.buildType.name == "release"){
                def newApkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + formattedDate + ".apk"
                output.outputFile = new File(output.outputFile.parent, newApkName)
            }
            if (variant.buildType.name == "debug"){
                def newApkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + ".apk"
                output.outputFile = new File(output.outputFile.parent, newApkName)
            }
        }
    } }