Skip to content

Instantly share code, notes, and snippets.

@SeanZoR
Created February 22, 2014 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SeanZoR/9152066 to your computer and use it in GitHub Desktop.
Save SeanZoR/9152066 to your computer and use it in GitHub Desktop.
Android - Get application version code
public static int getVersionCode(Context ctx) {
int versionCode = 0;
try {
PackageInfo pInfo = ctx.getPackageManager().getPackageInfo(
ctx.getPackageName(), 0);
versionCode = pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
@fadsel
Copy link

fadsel commented Nov 8, 2016

I was looking for this one and ended up here , Its been 3 years 🚶‍♂️

Let me update this for future references ,

If you're using the Gradle plugin/Android Studio, as of version 0.7.0 ( Right now it is 2.1.0 ), version code and version name are available statically in BuildConfig:

int versionCode = BuildConfig.VERSION_CODE;
String versionName = BuildConfig.VERSION_NAME;

No Context object needed!

Just make sure to specify them in your build.gradle file instead of the AndroidManifest.xml.


defaultConfig {
    ...
    versionCode 1
    versionName "1.0"
}

@bev623
Copy link

bev623 commented Jun 18, 2019

Correct me if I'm wrong. Is it applicable here the Payroll system?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment