When releasing an Android app, it can be useful to show your users the current version name (
BuildConfig.VERSION_NAME) or code (
BuildConfig.VERSION_CODE). Another nice feature is showing when this version of the app was built, to reassure users that features / bug fixes are being released frequently.
Luckily, this can be done in 2 super simple steps:
BUILD_TIME (of type
Date) into your app-level
build.gradle, with a value defined using the
Date constructor with the current time. So, inside
buildConfigField "java.util.Date", "BUILD_TIME", "new java.util.Date(" + System.currentTimeMillis() + "L)"
Next, just use
BuildConfig.BUILD_TIME whenever you want to access this date object. Just like a normal
Date object, it can be formatted (e.g. to “18 Mar 2019”):
SimpleDateFormat("dd MMM yyy", Locale.US).format(BuildConfig.BUILD_TIME)