Detect my app's own android:versionCode at run time

Is there a way for my app to know the android:versionCode from AndroidManifest.xml or do I have to create a separate constant in one of my classes?

  • How does getContentResolver() work?
  • Create Free/Paid versions of Application from same code
  • Size of file which we get through AssetManager function getAssets in Android
  • Flash video not displaying in WebView for Android 3.0.1
  • Integer.parseInt and NumberFormatException on Android
  • How to implement first launch tutorial like Android Lollipop apps: Like Sheets, Slides app?
  • Difference between build.gradle(Project) and build.gradle(Module)
  • How to get the AlertDialog title?
  • How to move an image from left to right in android
  • how to use adb command to push a file on device without sd card
  • Robolectric ContentProvider testing
  • How to resize a bitmap eficiently and with out losing quality in android
  • 3 Solutions collect form web for “Detect my app's own android:versionCode at run time”

    I put this in my subclassed but you can use it anywhere you have a context. Just change getPackageManager() to context.getPackageManager().

    public int getVersion() {
        int v = 0;
        try {
            v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
        } catch (NameNotFoundException e) {
            // Huh? Really?
        return v;

    Yes, you can access this information via PackageManager.getPackageInfo

    Also, see details about versioning in the documentation

    You can get version code simply from <package_name>.BuildConfig#VERSION_CODE
    reference. It is generated by ADT plugin/Intellij.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.