Android Development
Application Version
Programming
Android Applications
Mobile Development

Get application version programmatically in android

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Android development, retrieving the application version programmatically is essential for many scenarios, such as displaying it on a settings or about page, checking compatibility with APIs, or managing updates. Android stores version information in the AndroidManifest.xml file, and this data can be retrieved at runtime using Android’s PackageManager class.

Understanding Version Information in AndroidManifest.xml

Android applications use the AndroidManifest.xml file to define system-related data. Among these are the versionCode and versionName. The versionCode is an integer that generally increases with each release and is used internally to determine newer versions. In contrast, versionName is a string that represents the version which is displayed to the user. These might typically be set as follows in AndroidManifest.xml:

xml
1<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2    package="com.example.myapp">
3    <application
4        ...
5        android:icon="@drawable/icon"
6        android:label="@string/app_name">
7        ...
8    </application>
9    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="29" />
10    android:versionCode="5"
11    android:versionName="1.4.3"
12</manifest>

Retrieving Version Information Programmatically

To retrieve these version details programmatically, you can utilize the PackageManager class in Android. Here’s a quick guide on how they can be accessed:

  1. Get PackageManager Instance - This class is used to retrieve various kinds of information related to the application packages that are currently installed on the device.
  2. Get PackageInfo - PackageInfo holds overall information about the contents of a package. This object is where the app can retrieve the version information.

Here’s how you can do it:

java
1try {
2    PackageManager pm = this.getPackageManager();
3    PackageInfo pInfo = pm.getPackageInfo(this.getPackageName(), 0);
4    String versionName = pInfo.versionName;
5    int versionCode;
6    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
7        versionCode = (int) pInfo.getLongVersionCode(); // for API level 28+
8    } else {
9        versionCode = pInfo.versionCode; // deprecated in API level 28
10    }
11
12    // Log or display the version information
13    Log.d("AppVersionInfo", "Version Name: " + versionName + ", Version Code: " + versionCode);
14} catch (PackageManager.NameNotFoundException e) {
15    e.printStackTrace();
16}

Table Summary of Key Methods and Classes

ElementPurposeAPI Level
PackageManagerRetrieve various kinds of information about installed appsAvailable from API Level 1
getPackageInfoRetrieves overall information about the app packageAvailable from API Level 1
versionName(String) Human readable version nameSpecified in AndroidManifest.xml
versionCode(Integer) Internal version codeSpecified in AndroidManifest.xml
getLongVersionCode(Long) Retrieves the version code as a long (API 28+)Available from API Level 28

Practical Uses and Considerations

Knowing how to programmatically access application version information is particularly useful in cases such as:

  • Feature flagging: Where certain features can be toggled based on the version of the application.
  • Analytics or debugging: Understanding which version of the app is in use while analyzing usage data or reports.
  • User support: Automatically include version information when users send feedback or report issues.

Conclusion

Mastering how to retrieve an app's version programmatically in Android helps in fine-graining control over app features and support, establishing a smoother user experience and easier maintenance. It’s a basic yet critical aspect of Android development that ensures better management of the app lifecycle and version control.


Course illustration
Course illustration

All Rights Reserved.