A common use case is that you want to include the git hash of the last commit and build time into your project, so that you can use their values in your crash reporting tool (for example).
The easiest way to do this is to generate them into your BuildConfig by adding these lines to your build.gradle:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This will break incremental compilation | |
def gitSha = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim() | |
def buildTime = new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")) | |
android { | |
defaultConfig { | |
buildConfigField "String", "GIT_SHA", "\"${gitSha}\"" | |
buildConfigField "String", "BUILD_TIME", "\"${buildTime}\"" | |
} | |
} |
A solution could be to write information into an asset file instead of BuildConfig, and then read that file during runtime, which is exactly what Paperwork does:
https://github.com/zsoltk/paperwork
An added benefit is that you can now include any other information as well (output of a script maybe) that would result in a full recompile otherwise.