How can you get the build/version number of your Android application?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Android applications usually expose two version values: a user-facing version name and an internal build version code. You can read them either from generated build constants or from package metadata at runtime, and the right choice depends on whether you need your own app's version or the installed package information from the system.
Understand versionName and versionCode
In Android builds, the common pair is:
- '
versionName: the string shown to users, such as2.4.1' - version code: the monotonically increasing build number used for upgrade ordering
Modern Android still relies on the same idea, but newer APIs expose the code as a long-capable value through longVersionCode.
Example Gradle configuration:
Once the app is built, those values are available to your code.
Use BuildConfig for Your Own App
If you just need the current app's build information, BuildConfig is the simplest path.
This is fast and convenient because the constants are generated at build time.
For Java:
This works well for about screens, logging, and feature diagnostics tied to your own package.
Use PackageManager When You Need Installed Package Info
If you need runtime package metadata, or you want to inspect another installed package, use PackageManager.
Usage inside an activity:
The compatibility helper is useful because older and newer Android API levels expose the code slightly differently.
Show the Version in UI Safely
Displaying version information in a settings screen is a common use case.
That keeps the text tied directly to the built artifact, which is better than hardcoding it in a resource string that can drift out of sync.
If you use product flavors or build variants, remember that each variant can have different version metadata. Logging version info during startup is often helpful in QA builds.
Prefer the Right Source for the Job
A practical rule:
- '
BuildConfigfor your own app's compile-time version constants' - '
PackageManagerfor installed-package queries and runtime package metadata'
If you only need one line in an about screen, BuildConfig is enough. If you are writing support diagnostics, crash reports, or version-aware tooling, package metadata may be more appropriate.
Also be aware that library modules do not always expose the same application-level BuildConfig values you expect. Read the app module's constants, not a random library module's generated class.
Common Pitfalls
- Using a library module's
BuildConfiginstead of the app module's version constants gives the wrong value. - Assuming
versionCodeis always enough without considering newer long version-code handling. - Hardcoding version strings in UI resources creates drift from the actual built artifact.
- Catching
NameNotFoundExceptionfor your own package and ignoring it can hide setup problems that should never happen. - Forgetting that different build variants may produce different version values during testing.
Summary
- Android exposes a user-facing version name and an internal version code.
- Use
BuildConfigwhen you need your own app's generated version constants. - Use
PackageManagerandPackageInfoCompatwhen you need installed package metadata. - Show version text from the actual build values instead of hardcoded strings.
- Be careful with build variants and module-specific
BuildConfigclasses.
Related reading
- How could I create a function with a completion handler in Swift?
- How disable Copy, Cut, Select, Select All in UITextView
- How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?
- How do I access the host machine itself from the iPhone simulator
- How do I add 1 day to an NSDate?
- How do I add a bullet symbol in TextView?
- How do I add a Fragment to an Activity with a programmatically created content view
- How do I add a library project to Android Studio?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.