Skip to content

Instantly share code, notes, and snippets.

@AbreuY
Created August 15, 2020 19:40
Show Gist options
  • Select an option

  • Save AbreuY/33186b349e28e154fb9345aecb797cb6 to your computer and use it in GitHub Desktop.

Select an option

Save AbreuY/33186b349e28e154fb9345aecb797cb6 to your computer and use it in GitHub Desktop.
Get current version code in android app
static int getCurrentVersionCode(Activity activity) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return (int) activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0).getLongVersionCode();
} else {
//noinspection deprecation
return activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0).versionCode;
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, e.toString());
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment