Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 408
jennifer Avatar asked Feb 10 '11 11:02

jennifer


2 Answers

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.

like image 83
Vikas Patidar Avatar answered Sep 20 '22 06:09

Vikas Patidar


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.

like image 28
ForeverWintr Avatar answered Sep 21 '22 06:09

ForeverWintr