Get android:versionName manifest element in code

I want to know how to parse the AndroidManifest.xml file in order to get the Application Version Number through code. android:versionName

  • Multiple TextViews update very slowly
  • Can you set “tab order” in XML Layout?
  • Android Activity.getParent() always returning null
  • Blend two textures with different coordinates and sizes in the same shader
  • Android: MediaPlayer setVolume function
  • android local permission Androidmanifest SMS READ
  • Related posts:

    'App not Installed' Error on Android
    Maven with Android - file generation (Eclipse)
    jQuery's ajax function does not work in Android PhoneGap application
    PackageManager's is always null
    Android Studio ERROR: Cannot resolve symbol 'View'
    How make spacing between letters in Android TextView?
  • SSL Proxy with Genymotion and Charles?
  • How do I add elements dynamically to a view created with XML
  • Intellij IDEA - view saved password
  • onCheckedChanged called automatically
  • Android Canvas Redo and Undo Operation
  • An established connection was aborted by the software in your host machine
  • 4 Solutions collect form web for “Get android:versionName manifest element in code”

    No need to parse the AndroidManifest.xml file for this.

    You can get this by using:

        String app_ver = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
    catch (NameNotFoundException e)
        Log.e(tag, e.getMessage());

    Use this inside your onCreate() method.

    In case this is helpful to anyone else, you can also access @string resources from AndroidManifest.xml like you can from other xml files.

    So for example, in AndroidManifest.xml:


    and in code:

    string versionName = getResources().getString(R.string.app_versionName);

    This way you don’t need the (annoying, imo) try/catch statement. I’m not sure if this is an approved way of doing things, but it makes sense to me.

    If you’re using the Android Studio, the version name is available in the class BuildConfig. This class in generated at compile time:

    String versionName = BuildConfig.VERSION_NAME;

    This is the cleanest way retrieving the version name.

    So far, i used the below code snippet from this article and successfully got the version code and version name from AndroidManifest.xml

    /* Get android:versionName */
    String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
    /* Get android:versionCode */
    int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.