Wednesday 21 December 2016

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



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!


Answer



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




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




         package="com.somepackage"
    android:versionName="@string/version" android:versionCode="20">

  2. 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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...