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

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

    Using Gradle and BuildConfig

    Getting the VERSION_NAME from BuildConfig

    BuildConfig.VERSION_NAME
    

    Yep, it's that easy now.

    Is It Returning an Empty String for VERSION_NAME?

    If you're getting a empty string for BuildConfig.VERSION_NAME then read on.

    I kept getting an empty string for BuildConfig.VERSION_NAME because I wasn't setting the versionName in my Grade build file (I migrated from ANT to Gradle). So, here are instructions for ensuring you're setting your VERSION_NAME via Gradle.

    build.gradle

    def versionMajor = 3
    def versionMinor = 0
    def versionPatch = 0
    def versionBuild = 0 // bump for dogfood builds, public betas, etc.
    
    android {
    
      defaultConfig {
        versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
    
        versionName "${versionMajor}.${versionMinor}.${versionPatch}"
      }
    
    }
    

    Note: This is from the masterful Jake Wharton.

    Removing versionName and versionCode from AndroidManifest.xml

    And since you've set the versionName and versionCode in the build.gradle file now, you can also remove them from your AndroidManifest.xml file, if they are there.

提交回复
热议问题