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
  • 732,580
  • 175
  • 1,330
  • 1,459
Eloy Fernández Franco
  • 1,350
  • 1
  • 24
  • 47
  • 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

29

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

BuildConfig.VERSION_CODE
Simas
  • 43,548
  • 10
  • 88
  • 116
3

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,951
  • 2
  • 25
  • 39
1

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

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

try this

BuildConfig.VERSION_CODE

and import

import com.<your_pakcage_name>.BuildConfig;
Sandeep Pareek
  • 1,636
  • 19
  • 21