From b57c490a6f6c074e7be5b59aba2e53ec083c1c3e Mon Sep 17 00:00:00 2001 From: xinfengkun Date: Wed, 1 Mar 2023 14:24:55 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=B7=A5=E6=8E=A7=E6=9C=BA=E7=9B=91=E6=8E=A7]?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=93=E5=8C=85=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_ipc_monitoring/build.gradle | 167 ++++++++++++++++-- app_ipc_monitoring/gradle.properties | 2 + .../adas/client/ui/VersionFragment.java | 3 + 3 files changed, 158 insertions(+), 14 deletions(-) create mode 100644 app_ipc_monitoring/gradle.properties diff --git a/app_ipc_monitoring/build.gradle b/app_ipc_monitoring/build.gradle index 789192a600..164272dd71 100644 --- a/app_ipc_monitoring/build.gradle +++ b/app_ipc_monitoring/build.gradle @@ -2,6 +2,18 @@ import java.text.SimpleDateFormat apply plugin: 'com.android.application' +// 定义当前gradle文件的全局变量 +ext { + //主版本号 + MAJOR_VERSION = 1 + //次版本号 + MINOR_VERSION = 0 + createFile() + buildTimes = getBuildTime() + isRelease = checkRelease() + gitBranchName = getWorkingBranchName() +} + android { compileSdkVersion rootProject.ext.android.compileSdkVersion defaultConfig { @@ -9,8 +21,8 @@ android { minSdkVersion rootProject.ext.android.minSdkVersion targetSdkVersion 25 multiDexEnabled true - versionCode 1 - versionName "1.0" + versionCode increasedVersionCode() + versionName increasedVersionName() testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" flavorDimensions "multi_device" @@ -42,14 +54,46 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } +//配置自定义打包名称 + applicationVariants.all { variant -> + def buildTypeName = variant.buildType.name + //println("buildTypeName-------->" + buildTypeName) + variant.outputs.all { + StringBuffer buffer = new StringBuffer() + buffer.append(getProject().name) + buffer.append("_") + buffer.append(defaultConfig.versionName) + buffer.append(".") + buffer.append(buildTimes) + buffer.append("_") + buffer.append(gitBranchName) + buffer.append("_") + //获取渠道编号 + String flavor = variant.flavorName + if (flavor != null && flavor != "") { + buffer.append(flavor) + buffer.append("_") + } + //获取打包时间 + def fileName = "${buffer.toString()}${buildTypeName}.apk" + //测试文件名称 + buildConfigField 'String', 'APK_NAME', "\"${fileName}\"" + //println("fileName-------->" + fileName) + //这里修改apk文件名 + outputFileName = fileName + } + } + //插入构建时间 buildTypes.each { - //设置时间格式 - SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd.HHmmss", Locale.getDefault()) - //获取当前时间 - Date curDate = new Date(System.currentTimeMillis()) - String buildTime = formatter.format(curDate) - it.buildConfigField 'String', 'BUILD_TIME', "\"${buildTime}\"" + it.buildConfigField 'String', 'BUILD_TIME', "\"${buildTimes}\"" + it.buildConfigField 'String', 'GIT_BRANCH_NAME', "\"${gitBranchName}\"" + it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_TAXI', 'new java.util.HashSet(){{add(chassis.Chassis.GearPosition.GEAR_P);add(chassis.Chassis.GearPosition.GEAR_R);}}' + it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_BUS', 'new java.util.HashSet(){{add(chassis.Chassis.GearPosition.GEAR_N);add(chassis.Chassis.GearPosition.GEAR_R);}}' + it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_M1', 'null' + it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_M2', 'new java.util.HashSet(){{add(chassis.Chassis.GearPosition.GEAR_N);add(chassis.Chassis.GearPosition.GEAR_P);add(chassis.Chassis.GearPosition.GEAR_R);}}' + it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_SWEEPER', 'new java.util.HashSet(){{add(chassis.Chassis.GearPosition.GEAR_N);}}' + it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_VAN', 'null' } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 @@ -64,12 +108,7 @@ android { driver { dimension "multi_device" buildConfigField 'boolean', 'IS_CLIENT', 'false' - buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_TAXI', 'new java.util.HashSet(){{add(chassis.Chassis.GearPosition.GEAR_P);add(chassis.Chassis.GearPosition.GEAR_R);}}' - buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_BUS', 'new java.util.HashSet(){{add(chassis.Chassis.GearPosition.GEAR_N);add(chassis.Chassis.GearPosition.GEAR_R);}}' - buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_M1', 'null' - buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_M2', 'new java.util.HashSet(){{add(chassis.Chassis.GearPosition.GEAR_N);add(chassis.Chassis.GearPosition.GEAR_P);add(chassis.Chassis.GearPosition.GEAR_R);}}' - buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_SWEEPER', 'new java.util.HashSet(){{add(chassis.Chassis.GearPosition.GEAR_N);}}' - buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_VAN', 'null' + } } @@ -103,3 +142,103 @@ dependencies { // implementation 'com.mogo.cloud:telematic:1.3.59'//注释掉司机端 乘客端 implementation 'com.jcraft:jsch:0.1.55' } + +// 删除老的APK +def checkRelease() { + def runTasks = gradle.startParameter.taskNames + for (String task : runTasks) { + // 我这里认为执行“assembleRelease”和非“debug”的任务就是执行“release”的任务 + if (task.contains("assembleRelease") || task.contains("Release")) { + deleteOldAPK("release") + return true + } else if (task.contains("Debug")) { + deleteOldAPK("debug") + return false + } + } + return false +} +//在Moudle 根目录中创建gradle.properties文件存储VersionCode +def createFile() { + def propFile = file('./gradle.properties') + Properties props = new Properties() + if (!propFile.exists()) { + propFile.createNewFile() + props.load(new FileInputStream(propFile)) + props['VERSION_CODE'] = '1' + props.store(propFile.newWriter(), null) + } else { + props.load(new FileInputStream(propFile)) + def code = props['VERSION_CODE'] + if (code == null || code == "0") { + props['VERSION_CODE'] = '1' + props.store(propFile.newWriter(), null) + } + } +} +//读取VersionCode +def readVersionCode() { + def propFile = file('./gradle.properties') + Properties props = new Properties() + props.load(new FileInputStream(propFile)) + // 读取gradle.properties文件中定义的VERSION_CODE属性 + def code = props['VERSION_CODE'].toInteger() + //def code = props.getProperty('VERSION_CODE').toInteger() + return code +} +//当打包的是Release版本时自动更新VersionCode +def increasedVersionCode() { + def code = readVersionCode() + //println("isRelease====" + isRelease) + if (isRelease) { + code++ + def propFile = file('./gradle.properties') + Properties props = new Properties() + props.load(new FileInputStream(propFile)) + props['VERSION_CODE'] = code.toString() + // 将最新的versionCode写入gradle.properties文件中 + props.store(propFile.newWriter(), null) + } + return code +} + +//删除无用apk +def deleteOldAPK(String buildTypeName) { + + // 使用map创建一个树 + def tree = fileTree(dir: './build/outputs/apk', include: "${buildTypeName}/*.apk") + //println("文件个数:" + tree.size()) + // 遍历文件树 + tree.each { File file -> + //println("删除文件名称:" + file) + file.delete() + } +} + +def increasedVersionName() { + def versionName = "${MAJOR_VERSION}.${MINOR_VERSION}.${readVersionCode()}" + return versionName +} + +def getBuildTime() { + //设置时间格式 + SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd.HHmmss", Locale.getDefault()) + //获取当前时间 + Date curDate = new Date(System.currentTimeMillis()) + return formatter.format(curDate) +} + + +/** + * @return 获取当前分支名称 + */ +def getWorkingBranchName() { + def workingBranchName = "" + def proc = "git rev-parse --abbrev-ref HEAD".execute() + proc.in.eachLine { line -> workingBranchName = line } + proc.err.eachLine { line -> println line } + proc.waitFor() + workingBranchName = "${workingBranchName}" + println "Working branch name: " + workingBranchName + return workingBranchName +} \ No newline at end of file diff --git a/app_ipc_monitoring/gradle.properties b/app_ipc_monitoring/gradle.properties new file mode 100644 index 0000000000..82f55b7698 --- /dev/null +++ b/app_ipc_monitoring/gradle.properties @@ -0,0 +1,2 @@ +#Wed Mar 01 12:23:40 CST 2023 +VERSION_CODE=1 diff --git a/app_ipc_monitoring/src/main/java/com/zhidao/adas/client/ui/VersionFragment.java b/app_ipc_monitoring/src/main/java/com/zhidao/adas/client/ui/VersionFragment.java index 6bbb4b3825..2a19084ab1 100644 --- a/app_ipc_monitoring/src/main/java/com/zhidao/adas/client/ui/VersionFragment.java +++ b/app_ipc_monitoring/src/main/java/com/zhidao/adas/client/ui/VersionFragment.java @@ -260,7 +260,10 @@ public class VersionFragment extends BaseFragment { } list.add(new Config("ADAS LIB版本:", AdasManager.getInstance().getAdasVersion())); + list.add(new Config("APP版本:", BuildConfig.VERSION_NAME)); list.add(new Config("APP构建时间:", BuildConfig.BUILD_TIME)); + list.add(new Config("GIT分支:", BuildConfig.GIT_BRANCH_NAME)); + list.add(new Config("APK名称:", BuildConfig.APK_NAME)); adapter.setData(list); }