Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User versionName value of AndroidManifest.xml in code

The AndroidManifest.xml contains the version name of the application, something like

android:versionName="1.0" 

Now the question - is it somehow possible to access this version name in the source code, so that I can display it for example in an About Dialog?

like image 310
DonGru Avatar asked Sep 03 '10 16:09

DonGru


People also ask

What does AndroidManifest XML contain?

Every app project must have an AndroidManifest. xml file (with precisely that name) at the root of the project source set. The manifest file describes essential information about your app to the Android build tools, the Android operating system, and Google Play.

What is the root element of AndroidManifest XML?

manifest is the root element of the AndroidManifest. xml file. It has package attribute that describes the package name of the activity class.

Can decimal code version?

android:versionCodeThe value must be set as an integer, such as "100".

How do I open AndroidManifest XML?

Just open your APK and in treeview select "AndroidManifest. xml". It will be readable just like that.


2 Answers

If you use ADT and Eclipse:

String version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName; 

If you use Gradle, there is an easier way, since it puts the data into BuildConfig for you:

String version = BuildConfig.VERSION_NAME; 
like image 119
Konstantin Burov Avatar answered Sep 29 '22 06:09

Konstantin Burov


Konstantin's answer (above) is correct, but for what it's worth I found that I got a compiler error if I did not catch a NameNotFoundException , as follows:

import android.content.pm.PackageManager.NameNotFoundException; try {     String version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName; } catch (NameNotFoundException e) {     Log.e("tag", e.getMessage()); } 
like image 36
DMH Avatar answered Sep 29 '22 06:09

DMH