Android Studio的APP开发即便使用VERSION_CODE来记录版本信息,依旧觉得麻烦,希望找到一种在编译时,自动将版本信息无误的加入到APK中的方法,毫无疑问,这需要修改编译脚本来辅助实现,而在AS中,自然是通过gradle脚本来实现。本文及详细介绍实现的方法和步骤,以及最终的演示效果。
文件名: tvass\gradle.properties
添加Buildconfig成员变量
GIT_COMMIT_ID=0
GIT_BRANCH_NAME=master
GIT_USER=empty
tvass\app\build.gradle
添加方法,用于给Buildconfig变量赋值
apply plugin: 'com.android.application'
//获取short版本的 git commit id
def getGitRevision() {
return "git rev-parse --short HEAD".execute().text.trim()
}
//获取分支名称
def getGitBranch(){
def branchName
def branch = "git branch --contains HEAD".execute().text.trim()
def index = branch.indexOf('*')
if(index == -1){
branchName = "master"
}else{
branchName = branch.substring(index+2)
}
println "branchName "+branchName
return branchName
}
//获取编译人员 user.name
def getGitUser(){
return "git config --global --get user.name".execute().text.trim()
}
tvass\app\build.gradle
android的BuildTypes内,添加
buildTypes {
release {
//在Build.gradle执行期间,给新增的Buildconfig的成员变量赋值
GIT_COMMIT_ID = getGitRevision()
GIT_BRANCH_NAME = getGitBranch()
GIT_USER = getGitUser()
//定义给JAVA使用的接口名称、参数类型
buildConfigField "String", "gitCommitId", "\"${GIT_COMMIT_ID}\""
buildConfigField "String", "gitBranchName", "\"${GIT_BRANCH_NAME}\""
buildConfigField "String", "getGitUser", "\"${GIT_USER}\""
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
//同楼上 release 说明
GIT_COMMIT_ID = getGitRevision()
GIT_BRANCH_NAME = getGitBranch()
GIT_USER = getGitUser()
buildConfigField "String", "gitCommitId", "\"${GIT_COMMIT_ID}\""
buildConfigField "String", "gitBranchName", "\"${GIT_BRANCH_NAME}\""
buildConfigField "String", "getGitUser", "\"${GIT_USER}\""
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}
在自己的工具类中,添加Java函数,获取Git版本信息
文件:tvass\app\src\main\java\com\xxx\tvass\tool\Tools.java
import com.xxx.tvass.BuildConfig;
public class Tools {
//调用 2.3.1 导出到java的接口
public static String getGitVersion(){
return String.format(Locale.CHINA, "APP版本信息:VERSION_CODE=%d, VERSION_NAME=%s, " +
" Git CommitID=CL#%s,Branch:%s, Producted By:%s",
BuildConfig.VERSION_CODE, BuildConfig.VERSION_NAME,
BuildConfig.gitCommitId, BuildConfig.gitBranchName, BuildConfig.getGitUser);
}
}
public class BootBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = BootBroadcastReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {//18006
RLog.i(TAG,"tvass: "+ Tools.getGitVersion());
if (Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(intent.getAction())) {
Windows PowerShell
版权所有(C) Microsoft Corporation。保留所有权利。
安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows
PS C:\Users\66176\Documents\works\mt9652\all_app\tvass> git branch -a
master_combiecode
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/master_xxx
PS C:\Users\66176\Documents\works\mt9652\all_app\tvass>
PS C:\Users\66176\Documents\works\mt9652\all_app\tvass> git lg
* 3516551 - (HEAD -> master, origin/master, origin/HEAD) 合并master与master_combiecode分支差异,添加APP版本打印 (16 hours ago) <zhoushang>
…… 省略 ……
* 501341c - init 20220713 (3 months ago) <zhoushang>
PS C:\Users\66176\Documents\works\mt9652\all_app\tvass>
11-18 09:40:38.373 5216 5216 I szhou : \n
BootBroadcastReceiver-----tvass: \n
APP版本信息:VERSION_CODE=1, VERSION_NAME=1.0, Git CommitID=CL#3516551,Branch:master, Producted By:zhoushang