Adding datestamp to output apk files in Gradle

In build.gradle, start off with:

import java.text.SimpleDateFormat
def buildTime() {
    def df = new SimpleDateFormat("yyyy-MM-dd'-'HH:mm") //you can change it
    df.setTimeZone(TimeZone.getTimeZone("IST"))
    return df.format(new Date())
}

Now add the following to modify the APK name:

applicationVariants.all { variant ->

        def apk = variant.outputFile;
        def newName;

        def time = buildTime()
        def versionNameSuffix = "-beta-build-${time}"

        newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-" + variant.buildType.name.toUpperCase() + versionNameSuffix + ".apk");

        newName = newName
                .replace("-" + variant.buildType.name, "")
                .replace(project.name, "droidzone");

        variant.outputFile = new File(apk.parentFile, newName);
        if (variant.zipAlign) {
            variant.outputFile = new File(apk.parentFile, newName.replace("-unaligned", ""));
        }

        logger.info('INFO: Set outputFile to ' + variant.outputFile + " for [" + variant.name + "]");
    }