打包分支是 Mater 分支,所以 APP 的版本号就可以设置为当前分支的提交次数,并且为了方便,需要将分支名显示在设置页面上,如下图所示:
build
设置 APP 版本号为 git 提交的次数
- git 命令
git rev-list --count HEAD
- java 实现 git 命令的执行
public static int getGitReVersion() {
int gitReversion = -1;
try {
StringBuffer buffer = new StringBuffer();
Process exec = Runtime.getRuntime().exec("git rev-list --count HEAD");
InputStream inputStream = exec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
char[] chars = new char[1024];
int len;
while ((len = reader.read(chars)) != -1) {
buffer.append(chars, 0, len);
}
reader.close();
inputStream.close();
//注意:这里读取出来的数据需要 trim() 不然会有一个 \n 的换行符
gitReversion = Integer.valueOf(buffer.toString().trim());
} catch (IOException e) {
e.printStackTrace();
}
return gitReversion;
}
- groovy 脚本
给 versionCode 设置为 git 的提交次数
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.git"
minSdkVersion 21
targetSdkVersion 28
//设置版本号为 getReversion()
versionCode getReversion()
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
def getReversion() {
def buildnum = -1
try {
//底层使用的是 Runtime.getRuntime().exec()去执行
def getGit = 'git rev-list --count HEAD'.execute()
def gitVersion = getGit.text.trim().toInteger()
buildnum = buildnum + gitVersion
} catch (e) {
e.printStackTrace()
}
return buildnum
}
获取当前打包分支的分支名
- git 命令
git symbolic-ref --short -q HEAD
- java 实现
public static String getBranchName() {
String branchName = null;
try {
StringBuffer buffer = new StringBuffer();
Process exec = Runtime.getRuntime().exec("git symbolic-ref --short -q HEAD")
InputStream inputStream = exec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
char[] chars = new char[1024];
int len;
while ((len = reader.read(chars)) != -1) {
buffer.append(chars, 0, len);
}
reader.close();
inputStream.close();
branchName = buffer.toString();
} catch (IOException e) {
e.printStackTrace();
}
return branchName;
}
- groovy 脚本
String getBranchName() {
def branchName = null;
try {
/**
* .execute()
* .getText()
* 以上两个方法都是扩展方法
*/
def inputstream = "git symbolic-ref --short -q HEAD".execute()
//这里记得 trim() 不然会有换行符
branchName = inputstream.getText().trim()
} catch (IOException e) {
e.printStackTrace();
}
println "getBranchName():" + branchName
return branchName;
}
给 BuildConfig 设置一个属性参数获取分支名
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
buildConfigField "String", "BUILD_BRANCE", "\"" + getBranchName() + "\""
}
debug {
//如果不单独加上引号会出现这种问题,到时运行失败
//public static final String BUILD_BRANCE = master;
//注意这里必须要单独加上双引号
//public static final String BUILD_BRANCE = "master";
buildConfigField "String", "BUILD_BRANCE", "\"" + getBranchName() + "\""
}
}
设置之后就可以在 BuildConfig 中看到 BUILD_BRANCE
属性置了。
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.example.git";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 37;
public static final String VERSION_NAME = "1.0.0";
// Fields from build type: debug
public static final String BUILD_BRANCE = "master";
}