I want to know how to parse the AndroidManifest.xml file in order to get the Application Version Number through code. android:versionName
-
1You can get your application's `versionName` with `BuildConfig.VERSION_NAME` if you're using Gradle. See the answer here: http://stackoverflow.com/a/28352130/293280 – Joshua Pinter Feb 05 '15 at 19:18
4 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.

- 60,022
- 51
- 208
- 332

- 42,865
- 22
- 93
- 106
-
i need to compare my application android:versionName with the app in the downloads. my app is not in android market.. i need to compare the versionName in my mobile with that available in site to obatin the new version and show an alert to update it.. how is it posiible to obtain the new version number of the app in the website – jennifer Feb 10 '11 at 12:28
-
@jebbifer: Simply create a `Http Get` request from your application and send this value in parameters of this request. And on your server you need to handle that request and compare that application version values. If greater than the client app then in response send him/her a link to download new version of that application. – Vikas Patidar Feb 10 '11 at 12:34
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.

- 5,492
- 2
- 36
- 65
-
1cool solution ! Instead of editting string directly from the manifest, use a string value and edit it, your code wont change then ! i like ! +2 :P – S.Thiongane Jun 24 '14 at 20:24
-
4Notice this **lint warning message** : ***android:versionName can not be a resource url, it must be a literal string*** – S.Thiongane Jul 07 '14 at 09:05
-
Shame. That lint message must've showed up recently; I don't remember seeing it back when I was working on this. – ForeverWintr Sep 25 '14 at 22:11
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.

- 1,975
- 20
- 14
-
As from https://developer.android.com/studio/publish/versioning the versionName should be in the Gradle, not the manifest. This method reads from the Gradle. – Ben Jul 05 '22 at 08:40
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;
-
Please expand your answer, as link-only answers are not considered good on SO. – Mike Szyndel Jan 08 '14 at 14:23