'How can I display the build time in my Android application?

When I develop my application and correct it once and again, I would like to see if the one running now is the last I built. Hence, I would like to add a field, say a TextView, that will display the build time-of-day (e.g., 19:26). How can I take the build time and embed it in activity_main.xml? BTW - build version, a counter that progresses with every build, is also good. Anything that will indicate the last build. Thanks, Eyal



Solution 1:[1]

Assuming that you're building with Gradle, you can add a BuildConfig field with the desired information in your app/build.gradle:

android {
    defaultConfig {
        def buildTime = new Date()
        buildConfigField "String", "BUILD_TIME", "\"${buildTime.format('yyyy-MM-dd HH:mm:ss')}\""
        ... other stuff ...
    }
    ...other stuff ...
}

And then in your Kotlin/Java code:

myTextView.text = BuildConfig.BUILD_TIME

Another alternative is to replace the buildConfigField line in the above example with:

resValue "string", "build_time", "${buildTime.format('yyyy-MM-dd HH:mm:ss')}"

Which creates a string resource that you can use in your layout XML file, e.g.:

android:text="@string/build_time"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1