0

Is it possible to get generated time of APK programmatically?

I want to display that on the build time for build identification purposes.

I know that I could hard code the time string in the gradle file and access them in my Kotlin code, as this answer suggests, But that would mean every time I've to change the string when I re-generate the build.

Any help would be appreciated.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
  • call something like System.currentTime (pseudocode) in your build.gradle so you don't manually have to change the value – Zun Jul 16 '18 at 08:58
  • Refer to: https://stackoverflow.com/questions/19172565/how-append-date-build-to-versionnamesuffix-on-gradle – Zun Jul 16 '18 at 08:59

2 Answers2

1

I wrote the following code in build.gradle file to achieve this

android {
    buildTypes {
        debug {
            buildConfigField "String", "BUILT_ON", "\"${getBuildDateTime()}\""
        }
    }
}

static def getBuildDateTime() {
    return "Build Generated On: ${new Date().format("dd MMMM, 2018 @ h:mma")}"
}

Now I can access this variable in my Activity

Log.d(BuildConfig.BUILT_ON);
Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
0

You can use this code in defaultConfig of your project to set up your application build time.

setProperty("archivesBaseName", "Application_Name_$versionName.$versionCode" + "_" + getDate())

static def getDate() {
def date = new Date()
def formattedDate = date.format('yyyy-MMM-dd_EEE_HH.mm')
return formattedDate
}
Prince Dholakiya
  • 3,255
  • 27
  • 43
VIISHRUT MAVANII
  • 11,410
  • 7
  • 34
  • 49