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

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

    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"
    }
    

提交回复
热议问题