How do I get my application Version

Can anyone tell me how to get applcation version in Android?


Solution 1:

This page has a tips on how to do it from java:

PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(
    context.getPackageName(), 0);
String version = info.versionName;

Also, this link has official information on how to properly set up your application versioning.

Solution 2:

I am using Android Studio, I realized I could use one line code to get this.

/*version name*/
BuildConfig.VERSION_NAME

/*version code*/
BuildConfig.VERSION_CODE

Edited:

If you are using other android libraries, make sure you import BuildConfig from your application package. This is similar to the automatically generated R class for identifying resources.

Solution 3:

public int getVersion(Context context) {
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            return pInfo.versionCode;
        } catch (NameNotFoundException e) {
            return 0;
        }
    }
}

More info on this link