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 have two version identifiers: versionName (a human-readable string like "2.1.0") and versionCode (an integer used by the Play Store to determine updates). You can read both programmatically using PackageManager.getPackageInfo(). In modern Android development (API 28+), use PackageInfo.getLongVersionCode() instead of the deprecated versionCode field for apps with version codes exceeding Integer.MAX_VALUE.
Getting Version in Java
Getting Version in Kotlin
Using BuildConfig
The simplest approach — access version info directly without PackageManager:
BuildConfig is generated at compile time from your build.gradle values. It does not require try-catch and is available without a Context.
Note: In multi-module projects, each module has its own BuildConfig. Import the correct one from your app module.
Setting Version in build.gradle
Auto-Incrementing Version Code
Displaying Version in the UI
In Jetpack Compose
Getting Version of Another App
Using PackageInfoCompat (AndroidX)
PackageInfoCompat handles the API 28 deprecation automatically, using longVersionCode on API 28+ and versionCode on older versions.
Version Comparison
Common Pitfalls
- Using deprecated
versionCodeon API 28+:PackageInfo.versionCodeis deprecated. UsePackageInfo.getLongVersionCode()on API 28+ orPackageInfoCompat.getLongVersionCode()from AndroidX for all API levels. - Wrong BuildConfig import in multi-module projects: Each module generates its own
BuildConfigclass. ImportingBuildConfigfrom a library module gives that module's version, not the app's version. Always import from the app module's package. - NameNotFoundException in release builds: This should never happen for
getPackageName(), but ProGuard or R8 obfuscation can sometimes interfere withBuildConfigconstants. If usingBuildConfig, ensure the class is not stripped by adding keep rules if necessary. - versionName is null:
versionNamecan be null if not set inbuild.gradle. Always handle null:pInfo.versionName ?: "unknown". - versionCode overflow:
versionCodeis anint(max ~2.1 billion). Google Play requires each update to have a higherversionCode. If using timestamps or large numbers, uselongVersionCode(API 28+) which supports values up toLong.MAX_VALUE.
Summary
- Use
BuildConfig.VERSION_NAMEandBuildConfig.VERSION_CODEfor the simplest access - Use
PackageManager.getPackageInfo()when you need version info from aContextor for other apps - Use
PackageInfoCompat.getLongVersionCode()from AndroidX for backward-compatible long version codes - Set
versionCodeandversionNameinbuild.gradle versionCodeis for the Play Store (must increment);versionNameis for users (any string format)
Related reading
- How can you get the build/version number of your Android application?
- 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
.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.