Sid*_*rth 5 groovy build task gradle
我想从 1.0.0 增加我的项目的版本号。每当通过 bash 命令进行新构建时,都会自动升级到 1.0.1。我只需要增加路径号,其他我将在手动构建期间手动增加。
我想改变
这个 :
version=1.0.0
到 这个:
version=1.0.1
Run Code Online (Sandbox Code Playgroud)
使用 gradle 任务。任何帮助我该怎么做。有什么方法可以使用正则表达式或使用子字符串函数来更新它。
这是 Gradle 中版本升级的自定义任务(Android 项目):
\n\nclass Version {\n\n private int major\n private int minor\n private int patch\n private int code\n\n Version(int code, String version) {\n this.code = code\n\n def (major, minor, patch) = version.tokenize(\'.\')\n this.major = major.toInteger()\n this.minor = minor.toInteger()\n this.patch = patch.toInteger()\n }\n\n @SuppressWarnings("unused")\n void bumpMajor() {\n major += 1\n minor = 0\n patch = 0\n\n code += 1\n }\n\n @SuppressWarnings("unused")\n void bumpMinor() {\n minor += 1\n patch = 0\n\n code += 1\n }\n\n @SuppressWarnings("unused")\n void bumpPatch() {\n patch += 1\n code += 1\n }\n\n String getName() { "$major.$minor.$patch" }\n\n int getCode() { code }\n}\n\ntasks.addRule("Pattern: bump<TYPE>Version") { String taskName ->\n if (taskName.matches("bump(Major|Minor|Patch)Version")) {\n task(taskName) {\n doLast {\n String type = (taskName - \'bump\' - \'Version\')\n\n println "Bumping ${type.toLowerCase()} version\xe2\x80\xa6"\n\n int oldVersionCode = android.defaultConfig.versionCode\n String oldVersionName = android.defaultConfig.versionName\n\n version = new Version(oldVersionCode, oldVersionName)\n version."bump$type"()\n\n String newVersionName = version.getName()\n String newVersionCode = version.getCode()\n\n println "$oldVersionName ($oldVersionCode) \xe2\x86\x92 $newVersionName ($newVersionCode)"\n\n def updated = buildFile.getText()\n updated = updated.replaceFirst("versionName \'$oldVersionName\'", "versionName \'$newVersionName\'")\n updated = updated.replaceFirst("versionCode $oldVersionCode", "versionCode $newVersionCode")\n\n buildFile.setText(updated)\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n请参阅此汉字学习 Android 应用程序以了解完整性。
\n\n需要以下格式(注意单引号):
\n\nandroid {\n defaultConfig {\n versionCode 3\n versionName \'0.3.13\'\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n$ ./gradlew bumpPatchVersion\n\n> Task :app:bumpPatchVersion\nBumping patch version\xe2\x80\xa6\n0.3.13 (3) \xe2\x86\x92 0.3.14 (4)\n\nBUILD SUCCESSFUL in 0s\n1 actionable task: 1 executed\n\n$ ./gradlew bumpMinorVersion\n\n> Task :app:bumpMinorVersion\nBumping minor version\xe2\x80\xa6\n0.3.14 (4) \xe2\x86\x92 0.4.0 (5)\n\nBUILD SUCCESSFUL in 0s\n1 actionable task: 1 executed\n\n$ ./gradlew bumpMajorVersion \n\n> Task :app:bumpMajorVersion\nBumping major version\xe2\x80\xa6\n0.4.0 (5) \xe2\x86\x92 1.0.0 (6)\n\nBUILD SUCCESSFUL in 0s\n1 actionable task: 1 executed\nRun Code Online (Sandbox Code Playgroud)\n
这是一个示例任务:
version='1.0.0' //version we need to change
task increment<<{
def v=buildFile.getText().find(version) //get this build file's text and extract the version value
String minor=v.substring(v.lastIndexOf('.')+1) //get last digit
int m=minor.toInteger()+1 //increment
String major=v.substring(0,v.length()-1) //get the beginning
//println m
String s=buildFile.getText().replaceFirst("version='$version'","version='"+major+m+"'")
//println s
buildFile.setText(s) //replace the build file's text
}
Run Code Online (Sandbox Code Playgroud)
多次运行此任务,您应该会看到版本更改。
一个变体:
version='1.0.0'
task incrementVersion<<{
String minor=version.substring(version.lastIndexOf('.')+1)
int m=minor.toInteger()+1
String major=version.substring(0,version.length()-1)
String s=buildFile.getText().replaceFirst("version='$version'","version='"+major+m+"'")
buildFile.setText(s)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14228 次 |
| 最近记录: |