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

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

    Useful for build systems: there is a file generated with your apk called output.json which contains an array of information for each generated APK, including the versionName and versionCode.

    eg

    [
        {
            "apkInfo": {
                "baseName": "x86-release",
                "enabled": true,
                "filterName": "x86",
                "fullName": "86Release",
                "outputFile": "x86-release-1.0.apk",
                "splits": [
                    {
                        "filterType": "ABI",
                        "value": "x86"
                    }
                ],
                "type": "FULL_SPLIT",
                "versionCode": 42,
                "versionName": "1.0"
            },
            "outputType": {
                "type": "APK"
            },
            "path": "app-x86-release-1.0.apk",
            "properties": {}
        }
    ]
    
    0 讨论(0)
  • 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"
    }
    
    0 讨论(0)
  • 2020-11-22 11:36

    Always do it with try catch block:

    String versionName = "Version not found";
    
    try {
        versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        Log.i(TAG, "Version Name: " + versionName);
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        Log.e(TAG, "Exception Version Name: " + e.getLocalizedMessage());
    }
    
    0 讨论(0)
  • 2020-11-22 11:37

    Kotlin one-liners

    val versionCode = BuildConfig.VERSION_CODE
    val versionName = BuildConfig.VERSION_NAME
    

    Make sure to import BuildConfig into your class.

    Java one-liners

    String versionCode = String.valueOf(BuildConfig.VERSION_CODE);
    String versionName = String.valueOf(BuildConfig.VERSION_NAME);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 11:40

    for Api 28 the PackageInfo.versionCode is deprecated so use this code below:

        Context context = getApplicationContext();
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            myversionName = info.versionName;
            versionCode = (int) PackageInfoCompat.getLongVersionCode(info);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            myversionName = "Unknown-01";
        }
    
    0 讨论(0)
提交回复
热议问题