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

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

    Kotlin example:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_signin)
    
        packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES).apply {
            findViewById(R.id.text_version_name).text = versionName
            findViewById(R.id.text_version_code).text =
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) "$longVersionCode" else "$versionCode"
        }
    
        packageManager.getApplicationInfo(packageName, 0).apply{
            findViewById(R.id.text_build_date).text =
                SimpleDateFormat("yy-MM-dd hh:mm").format(java.io.File(sourceDir).lastModified())
        }
    }
    

    Don't thank :-)

提交回复
热议问题