How to get the build/version number of your Android application?

I need to figure out how to get or make a build number for my Android application. I need the build number to display in the UI.

Do I have to do something with AndroidManifest.xml ?


Use:

try {
    PackageInfo pInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
    String version = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}

And you can get the version code by using this

int verCode = pInfo.versionCode;

If you're using the Gradle plugin/Android Studio, as of version 0.7.0, version code and version name are available statically in BuildConfig . Make sure you import your app's package, and not another BuildConfig :

import com.yourpackage.BuildConfig;
...
int versionCode = BuildConfig.VERSION_CODE;
String versionName = BuildConfig.VERSION_NAME;

No Context object needed!

Also make sure to specify them in your build.gradle file instead of the AndroidManifest.xml .

defaultConfig {
    versionCode 1
    versionName "1.0"
}

如果您只想要版本名称,稍微缩短版本。

String versionName = context.getPackageManager()
    .getPackageInfo(context.getPackageName(), 0).versionName;
链接地址: http://www.djcxy.com/p/2640.html

上一篇: 我的Android应用程序工作非常缓慢

下一篇: 如何获取Android应用程序的构建版本号?