Search code examples
androidgradlebuild.gradledynamically-generatedbuild-time

How to access apk generated time in Android Programmatically?


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.


Solution

  • 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);