How to Programmatically Get Application Version in Android?

LinkedIn
Hacker News

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";
    }
Tagged in : Android

Stacktips

Stacktips is a weblog that provides collection of programming tutorials, how-to guides and code snippets. It is the home to range of articles that aims to help developers improve their programming skills.

We have published over 500+ programming tutorials and over 300 code snippets covering various technologies.

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.