androidgradleandroid-gradle-plugin

Android: add date/time to gradle output apk filename


How to add date/time stamp to Gradle Android output file name?

Should be like project_v0.5.0_201503110212_public.apk

Already looked at


Solution

  • I'm assuming that you want it in the format you specified, so here's one possible solution.

    In your gradle file you can define a new function to get the date time string like you desire:

    import java.text.DateFormat
    import java.text.SimpleDateFormat
    
    def getDateTime() {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
    
        return df.format(new Date());
    }
    

    Then for all variants you can simply run this:

    android {
        //...
      buildTypes {
        //...
        android.applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def file = output.outputFile
                output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + getDateTime() + ".apk"))
            }
        }
      }
    }
    

    Note that this doesn't really output the apk name like you posted, but I guess it's enough to help you out.