How to Retrieve the Build/Version Number of Your Android Application

How to Retrieve the Build/Version Number of Your Android Application

22 May 2024 Stephan Petzl Leave a comment Tech-Help

Understanding how to retrieve the build and version number of your Android application is crucial for various purposes, such as displaying it in the UI or for debugging. This guide will walk you through the steps to correctly fetch these details using the most up-to-date and efficient methods.

Using Gradle and BuildConfig

If you’re using the Gradle plugin with Android Studio, version code and version name are available statically in the BuildConfig class. This approach doesn’t require a Context object, making it straightforward and efficient.

Step-by-Step Guide:

  1. Ensure you import your app’s package:
  2. import com.yourpackage.BuildConfig;
  3. Access the version code and version name directly from BuildConfig:
  4. int versionCode = BuildConfig.VERSION_CODE;
    String versionName = BuildConfig.VERSION_NAME;
  5. Specify the version details in your build.gradle file:
  6. defaultConfig {
        versionCode 1
        versionName "1.0"

Alternative Method Using PackageManager

If for any reason you prefer or need to use the PackageManager class, the following method is also effective. This approach is particularly useful if you are not using Gradle or if you need to fetch these details dynamically at runtime.

Step-by-Step Guide:

  1. Use the following code snippet to get the version name and version code:
  2. try {
        PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        String version = pInfo.versionName;
        int verCode = pInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {

Handling Deprecation in API 28 and Above

For applications targeting API 28 (Android 9 Pie) and above, the versionCode is deprecated. Instead, use longVersionCode as shown below:

val versionNumber = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
} else {


Retrieving the version code and version name of your Android application can be done efficiently using either the BuildConfig class or the PackageManager class. Choosing the right method depends on your specific needs and the structure of your project.

Streamline Your Testing with Repeato

While managing version numbers is crucial, ensuring your app functions correctly across versions is equally important. This is where Repeato comes in. Repeato is a no-code test automation tool for iOS and Android that allows you to create, run, and maintain automated tests for your apps effortlessly.

Using advanced computer vision and AI, Repeato enables you to focus on developing your app rather than spending time on test creation and maintenance. It’s particularly beneficial for mobile developers who want to delegate test automation tasks to non-technical colleagues or QA teams. Learn more about how Repeato can enhance your development process on our blog.

For more detailed documentation and guides on using Repeato, visit our documentation section.

Like this article? there’s more where that came from!