How can you get the Manifest Version number from the App's (Layout) XML variables?

android, android-manifest, android-xml

Solution

There is not a way to directly get the version out, but there are two work-arounds that could be done.

The version could be stored in a resource string, and placed into the manifest by:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.somepackage"
     android:versionName="@string/version" android:versionCode="20">

One could create a custom view, and place it into the XML. The view would use this to assign the name:

context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;

Either of these solutions would allow for placing the version name in XML. Unfortunately there isn't a nice simple solution, like `android.R.string.version` or something like that.

Problem

I would like to have a way to reference the project's manifest version number in the main part of the code. What I have been doing up until now is to link the version number in a String XML file to the manifest (@string/Version). What I would like to do is to do it the other way around, link a string XML variable to the version in the manifest. The reason? I'd like to only have to change the version number in one location, the manifest file. Is there any way to do this? Thanks!

Original source

Related problems