7

Is there any way to know dynamically and programmatically the versionCode of an Android application??

I don´t know... maybe something like

 getApplicationContext.getVersionCode();

Thank you very much.

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
  • possible duplicate http://stackoverflow.com/questions/4616095/how-to-get-the-build-version-number-of-your-android-application – K Guru Feb 10 '15 at 07:13

4 Answers4

27

If you're using gradle (default in AndroidStudio) you can:

BuildConfig.VERSION_CODE
Simas
  • 42,722
  • 10
  • 86
  • 116
4

You find the following from using this code

 public int getVersionCode() {
    int v = 0;
    try {
        v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
    } catch (NameNotFoundException e) {

    }
    return v;
}

Class Required

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
MDMalik
  • 3,927
  • 2
  • 24
  • 39
2

This should be surrounded by a try...catch.

PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
version = pInfo.versionName;
versionCode = pInfo.versionCode;
Reenactor Rob
  • 1,490
  • 1
  • 11
  • 20
0

try this

BuildConfig.VERSION_CODE

and import

import com.<your_pakcage_name>.BuildConfig;
Sandeep Pareek
  • 1,068
  • 13
  • 17