Gradle script to autoversion and include the commit hash in Android
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Android build metadata is much easier to trust when it is generated from Git instead of edited by hand. A common setup is to derive versionCode from commit count or a CI build number, then include the short commit hash in versionName and BuildConfig so every build can be traced back to source.
Generate version information from Git
In a Groovy-based app/build.gradle, you can run small Git commands to obtain a short SHA and a commit count:
Then wire those values into the Android configuration:
That gives each build a numeric code for installs and a human-readable label that includes the source revision.
Why versionCode and commit hash should not be the same thing
versionCode exists for Android package upgrade ordering. It must be numeric and it must increase for every release you publish. A Git hash is great for traceability, but it is not ordered and should not be used as the raw versionCode.
The practical split is:
- use commit count or CI build number for
versionCode - use semantic version plus short hash for
versionName - expose the hash through
BuildConfigfor runtime diagnostics
That approach keeps Android installation rules and debugging needs separate.
Expose the hash inside the app
Once the build creates BuildConfig.GIT_SHA, app code can log or display it:
This is especially useful in QA builds because bug reports can include the exact source revision without guesswork.
CI build numbers are often safer than raw commit count
git rev-list --count HEAD is convenient, but it has limitations. It can change unexpectedly if repository history is rewritten, and shallow CI checkouts can produce misleading counts.
For production release pipelines, many teams prefer:
- CI-provided monotonically increasing build number for
versionCode - Git short SHA for traceability
In that setup, Gradle becomes the place where build metadata is assembled, not the place where release ordering is invented.
Add fallbacks when Git is unavailable
Gradle scripts that call Git assume the build is running from a Git checkout. That may fail in source archives or unusual CI environments, so a fallback helps:
You can use the same pattern for commit count if necessary.
Keep release policy predictable
If the app is distributed through Google Play, versionCode must never move backward for published builds. Auto-versioning is only useful if it respects that rule every time. For that reason, it is often better to keep a simple release policy for production and a slightly different scheme for local debug builds.
Common Pitfalls
- Using the Git hash directly as
versionCodeeven thoughversionCodemust be numeric and increasing. - Relying on commit count without considering rebases or shallow clones.
- Assuming Git commands always work in every build environment.
- Generating commit metadata but never exposing it in the running app.
- Forgetting that Play Store releases require monotonically increasing
versionCodevalues.
Summary
- Auto-versioning reduces manual mistakes and makes Android builds traceable.
- Use a numeric source such as commit count or CI build number for
versionCode. - Put the short Git SHA in
versionNameandBuildConfigfor debugging visibility. - Add fallbacks for environments where Git metadata is unavailable.
- Keep the release strategy compatible with Play Store upgrade rules.
Related reading

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.