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 ?


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;

Konstantin的答案(上面)是正确的,但是如果我没有发现NameNotFoundException ,我发现我收到了一个编译器错误,如下所示:

import android.content.pm.PackageManager.NameNotFoundException;
try {
    String version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
} catch (NameNotFoundException e) {
    Log.e("tag", e.getMessage());
}
链接地址: http://www.djcxy.com/p/90742.html

上一篇: 如何将空格保留在字符串的末尾和/或开头?

下一篇: 代码中AndroidManifest.xml的用户版本名称值