Skip to content

Instantly share code, notes, and snippets.

@nethergrim
Created June 29, 2021 15:55
Show Gist options
  • Save nethergrim/e8f4f1718b0c7165ac33ffdb0d7b5c00 to your computer and use it in GitHub Desktop.
Save nethergrim/e8f4f1718b0c7165ac33ffdb0d7b5c00 to your computer and use it in GitHub Desktop.
Generate versionCode for Android using current time, incremented each minute.
/**
* Constructs a version code as: {(now).minutes - (1 Jan 2021).minutes}* Which means that version code will be incremented each minute.
* The greatest value Google Play allows for versionCode is 2100000000.
* There are ~525600 minutes in a year, so greatest value will be reached after 3995 years.
* */
static def versionCodeDate() {
def millisInMinute = 60 * 1000
def startDate = new GregorianCalendar(2021, Calendar.JUNE, 1)
def startDateMinutePrecision = startDate.getTimeInMillis() / millisInMinute
def now = new GregorianCalendar()
def nowDateMinutePrecision = now.getTimeInMillis() / millisInMinute
def difference = (nowDateMinutePrecision - startDateMinutePrecision).toInteger()
println("Generated versionCode is: " + difference)
return difference
}
android {
defaultConfig {
versionCode versionCodeDate()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment