``` android { checkVerAdd() compileSdkVersion 30 buildToolsVersion "30.0.2" defaultConfig { applicationId "com.aland.isolationgate" minSdkVersion 19 targetSdkVersion 30 versionCode = getProperty("version.properties", "versionCode").toInteger() versionName getProperty("version.properties", "versionName") testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } multiDexEnabled true ndk { abiFilters 'armeabi-v7a' //只生成armv7的so } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release } debug { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release } android.applicationVariants.all { variant -> variant.outputs.all { if (variant.buildType.name.equals('release')) { // 自定义输出路径 variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath + File.separator + "apk") outputFileName = "app_${variant.versionName}.apk" } else if (variant.buildType.name.equals('debug')) { // variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath + File.separator + "apk") // outputFileName = "${variant.applicationId}_${variant.name}_${variant.versionName}.apk" } } } } }
/** * 校验进程进行编号自增 * @return */ def checkVerAdd() { if (checkRelease().toBoolean()) { propertyVerNameAdd() propertyVerCodeAdd() } } def checkRelease() { def runTasks = gradle.startParameter.taskNames print("task " + runTasks.toString()) return ('assembleRelease' in runTasks) } /** * @param filename 要打开的文件名字 * @param propName 文件里面的键名字 * @param isRootProject 是否用rootProject 文件打开,true 的话,在编译时候可以得到值,打包时候无法得到值。false的话在打包时候可以得到值,编译时候 无法得到值 * @return 所选择的文件里面的propName对应的值 */ def getProperty(String filename, String propName) { return getProperty(filename, propName, false) } def getProperty(String filename, String propName, boolean isRootProject) { //在这里 必须是在rootProject.file 如果 file的话 ,编译时候会出错 print("getProperty " + propName + "\n") def propsFile if (isRootProject) { propsFile = rootProject.file(filename) } else { propsFile = file(filename) } print(propsFile.getAbsolutePath() + "\n") if (propsFile.exists()) { def props = new Properties() props.load(new FileInputStream(propsFile)) if (props[propName] != null) { //下面这个输出在android studio3.1里面 在Build窗口 右边一个锤子下面 toggle哪里,就是原来的message哪里 print(propName + " : " + props[propName]) return props[propName].toString() } else { print("No such property " + propName + " in file " + filename); } } else { print(filename + " does not exist!") } } def propertyVerCodeAdd() { propertyVerCodeAdd("version.properties", "versionCode", false) } def propertyVerNameAdd() { propertyVerNameAdd("version.properties", "versionName", false) } //version.properties 里面的vesioncode自增 def propertyVerCodeAdd(String filename, String propName, boolean isRootProject) { print("PropertyAdd " + filename + "-" + propName) //这里必须用file 如果用rootProject的话 则找不到file为null def propsFile if (isRootProject) { propsFile = rootProject.file(filename) } else { propsFile = file(filename) } //只有在打正式版的情况下才继续运行,否则退出方法,这样是为了防止 编译时候versionCode 也自增 if里面的参数可以在Build窗口里面 message 最上边[]里面就是 if (propsFile.exists()) { def props = new Properties() props.load(new FileInputStream(propsFile)) //这里要注意 props[propName] 是String 类型 if (props[propName] != null) { def code = (props[propName]).toInteger() //转成Integer props[propName] = (++code).toString() //自增 且转成String } else { print("No such property " + propName + " in file " + filename); } //在这里提交 对version.properties的更改 props.store(propsFile.newWriter(), null) } else { print(filename + " does not exist!") } } def propertyVerNameAdd(String filename, String propName, boolean isRootProject) { print("PropertyAdd " + filename + "-" + propName) //这里必须用file 如果用rootProject的话 则找不到file为null def propsFile if (isRootProject) { propsFile = rootProject.file(filename) } else { propsFile = file(filename) } //只有在打正式版的情况下才继续运行,否则退出方法,这样是为了防止 编译时候versionCode 也自增 if里面的参数可以在Build窗口里面 message 最上边[]里面就是 // def runTasks = gradle.startParameter.taskNames // if (!(':app:assembleRelease' in runTasks)) { // return // } if (propsFile.exists()) { def props = new Properties() props.load(new FileInputStream(propsFile)) //这里要注意 props[propName] 是String 类型 if (props[propName] != null) { // def code = (props[propName]).toInteger() //转成Integer // props[propName] = (++code).toString() //自增 且转成String def vp = props[propName].toString() print(vp) if (vp.contains(".")) { props[propName] = updateVerN(vp) } else { props[propName] = vp + ".01" } } else { print("No such property " + propName + " in file " + filename); } //在这里提交 对version.properties的更改 props.store(propsFile.newWriter(), null) } else { print(filename + " does not exist!") } } def updateVerN(String str) { if (str.contains(".")) { String[] splitS = str.split("\\."); boolean up = fineLstInt(splitS, splitS.length - 1); if (!up) { str += ".001"; } else { str = appendDotStr(splitS); } } else { str += ".001"; } return str; } def fineLstInt(String[] splitS, int i) { boolean up = false; if (i < 0) return up; String split = splitS[i]; int length = split.length(); try { int parseInt = Integer.parseInt(split); int i1 = (int) Math.pow(10, (length)); int i2 = i1 - 2; if (parseInt > i2) { up = fineLstInt(splitS, i - 1); if (up) { splitS[i] = appendZeroStr(0, length); } } else { parseInt += 1; splitS[i] = appendZeroStr(parseInt, length); up = true; } } catch (Exception e) { return fineLstInt(splitS, i - 1); } return up; } def appendDotStr(String[] splitS) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < splitS.length; i++) { sb.append(splitS[i]); if (i < splitS.length - 1) { sb.append("."); } } return sb.toString(); } def appendZeroStr(int parseInt, int length) { String s = parseInt + ""; int i = length - s.length(); for (int i1 = 0; i1 < i; i1++) { s = "0" + s; } return s; }
```
版权声明:本文为sinat_34426699原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。