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

前端 未结 30 2434
刺人心
刺人心 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:34

    As in 2020 : API 28 "versionCode" is deprecated so we can use "longVersionCode"

    Sample code in kotlin

      val manager = context?.packageManager
            val info = manager?.getPackageInfo(
                context?.packageName, 0
            )
    
            val versionName = info?.versionName
            val versionNumber = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                info?.longVersionCode
            } else {
                info?.versionCode
            }
    

提交回复
热议问题