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

前端 未结 30 1809
刺人心
刺人心 2020-11-22 11:00

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

30条回答
  •  遇见更好的自我
    2020-11-22 11:30

    There are two parts you need: android:versionCode android:versionName

    versionCode is a number, and every version of the app you submit to the Market needs to have a higher number then the last.

    VersionName is a string, and can be anything you want it to be. This is where you define your app as "1.0" or "2.5" or "2 Alpha EXTREME!" or whatever.

    Example:

    Kotlin:

    val manager = this.packageManager
    val info = manager.getPackageInfo(this.packageName, PackageManager.GET_ACTIVITIES)
    toast("PackageName = " + info.packageName + "\nVersionCode = "
                + info.versionCode + "\nVersionName = "
                + info.versionName + "\nPermissions = " + info.permissions)
    

    Java:

    PackageManager manager = this.getPackageManager();
    PackageInfo info = manager.getPackageInfo(this.getPackageName(), PackageManager.GET_ACTIVITIES);
    Toast.makeText(this,
         "PackageName = " + info.packageName + "\nVersionCode = "
           + info.versionCode + "\nVersionName = "
           + info.versionName + "\nPermissions = " + info.permissions, Toast.LENGTH_SHORT).show();
    

提交回复
热议问题