52

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?

120

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;
| improve this answer | |
  • 26
    That's too many "getPackage"s for a single line of code... O_O – Cephron Apr 12 '11 at 20:06
  • 4
    Great! But is there a way to get the value from an xml file (especially to use it as android:summary of a Preference node)? – Ethan Leroy Jul 31 '12 at 20:31
38

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());
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.