How to get the build/version number of your Androi

2018-12-31 19:45发布

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 AndroidManifest.xml?

28条回答
只若初见
2楼-- · 2018-12-31 19:54
BuildConfig.VERSION_NAME //version name from your build file
BuildConfig.VERSION_CODE //version code from your build file

I don't see the need to get it from package manager

查看更多
骚的不知所云
3楼-- · 2018-12-31 19:55

There are some ways to get versionCode and versionName programmatically.

  1. Get version from PackageManager. This is the best way for most cases.
try {
    String versionName = packageManager.getPackageInfo(packageName, 0).versionName;
    int versionCode = packageManager.getPackageInfo(packageName, 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}
  1. Get it from generated BuildConfig.java. But notice, that if you'll access this values in library it will return library version, not apps one, that uses this library. So use only in non-library projects!
String versionName = BuildConfig.VERSION_NAME;
int versionCode = BuildConfig.VERSION_CODE;

There are some details, except of using second way in library project. In new android gradle plugin (3.0.0+) some functionalities removed. So, for now, i.e. setting different version for different flavors not working correct.

Incorrect way:

applicationVariants.all { variant ->
    println('variantApp: ' + variant.getName())

    def versionCode = {SOME_GENERATED_VALUE_IE_TIMESTAMP}
    def versionName = {SOME_GENERATED_VALUE_IE_TIMESTAMP}

    variant.mergedFlavor.versionCode = versionCode
    variant.mergedFlavor.versionName = versionName
}

Code above will correctly set values in BuildConfig, but from PackageManager you'll receive 0 and null if you didn't set version in default configuration. So your app will have 0 version code on device.

There is a workaround - set version for output apk file manually:

applicationVariants.all { variant ->
    println('variantApp: ' + variant.getName())

    def versionCode = {SOME_GENERATED_VALUE_IE_TIMESTAMP}
    def versionName = {SOME_GENERATED_VALUE_IE_TIMESTAMP}

    variant.outputs.all { output ->
        output.versionCodeOverride = versionCode
        output.versionNameOverride = versionName
    }
}
查看更多
孤独总比滥情好
4楼-- · 2018-12-31 19:55

In Activity file

final TextView text_1 = (TextView) findViewById(R.id.text_1);

String version= BuildConfig.VERSION_NAME;
int code = BuildConfig.VERSION_CODE;
text_1.setText("version Name :   "+version+ "Version Code : "+code);

I hope it will help you...!

查看更多
路过你的时光
5楼-- · 2018-12-31 19:57

Here is the method for getting the version code:

public String getAppVersion() {
    String versionCode = "1.0";
    try {
        versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return versionCode;
}
查看更多
有味是清欢
6楼-- · 2018-12-31 19:57

Someone who does’t need BuildConfig info for application's UI however wants to use these info for setting a CI job configuration or others, like me.

There is a automatically generated file, BuildConfig.java, under your project directory as long as you build your project successfully.

{WORKSPACE}/build/generated/source/buildConfig/{debug|release}/{PACKAGE}/BuildConfig.java

/**
* Automatically generated file. DO NOT MODIFY
*/
package com.XXX.Project;

public final class BuildConfig {
    public static final boolean DEBUG = Boolean.parseBoolean("true");
    public static final String APPLICATION_ID = "com.XXX.Project";
    public static final String BUILD_TYPE = "debug";
    public static final String FLAVOR = "";
    public static final int VERSION_CODE = 1;
    public static final String VERSION_NAME = "1.0.0";
}

Split information you need by python script or other tools. Here’s an example:

import subprocess
#find your BuildConfig.java
_BuildConfig = subprocess.check_output('find {WORKSPACE} -name BuildConfig.java', shell=True).rstrip()
#get the version name
_Android_version = subprocess.check_output('grep -n "VERSION_NAME" '+_BuildConfig, shell=True).split('"')[1]
print('Android version :’+_Android_version)

Please excuse my limited English ability, but hope this helps.

查看更多
其实,你不懂
7楼-- · 2018-12-31 19:58

A very simple way is :

private String appVersion = BuildConfig.VERSION_NAME;
查看更多
登录 后发表回答