I want to know how to parse the AndroidManifest.xml file in order to get the Application Version Number through code. android:versionName
No need to parse the AndroidManifest.xml
file for this.
You can get this by using:
try { 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:
android:versionName="@string/app_versionName"
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With