StackTips
 1 minutes

How to Programmatically Get Application Version in Android?

By Editorial @stacktips, On Sep 17, 2023 Android 2.53K Views

Use the following code snippet to, get Android application version from code. The PackageInfo class provides overall information about the contents of a package. This corresponds to all of the information collected from AndroidManifest.xml.

public String getAppVersion() {
        PackageInfo pInfo;
        try {
            pInfo = MyApp.getInstance().getPackageManager().getPackageInfo(MyApp.getInstance().getPackageName(), 0);
            return new StringBuilder("v").append(pInfo.versionName)
                    .append(" (").append(pInfo.versionCode).append(")").toString();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "1.0.0";
    }
stacktips avtar

Editorial

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.