我正在尝试在我正在使用的 CI 上自动化构建过程。我可以调用 curl
并为其分配一些变量,例如版本代码和名称。然后 CI(在我的例子中是 Bitrise CI)捕获它并开始发布构建。
但是,在此之前,我想根据 curl
传递给 build.gradle
文件的内容设置版本代码和版本名称,然后开始构建过程。
所以,我想我可以编写一个插件/任务,从命令行获取版本代码/名称,然后将其注入(inject) build.gradle
文件。像 ./gradlew setVersion 1 1.0
这样的命令。
因此,通过从我将要编写的脚本运行此命令,我将能够运行此 gradle 任务,并且 glob 中任何地方的每个人都能够通过 curl
创建发布版本.很有趣:)
我能够编写类似于以下代码的任务并将其放入我的主 build.gradle
文件中。
task setVersion << {
println versionCode
println versionName
}
并通过命令行传递一些参数:
./gradlew -PversionCode=483 -PversionName=v4.0.3 setVersion
这是我的输出:
:setVersion
483
v4.0.3
BUILD SUCCESSFUL
Total time: 6.346 secs
到目前为止一切顺利。我的问题是如何在 build.gradle
文件中设置它?
最佳答案
您可以创建从命令行更新 versionCode 和 versionName 的方法:
def getMyVersionCode = { ->
def code = project.hasProperty('versionCode') ? versionCode.toInteger() : -1
println "VersionCode is set to $code"
return code
}
def getMyVersionName = { ->
def name = project.hasProperty('versionName') ? versionName : "1.0"
println "VersionName is set to $name"
return name
}
然后在android block 中:
defaultConfig {
applicationId "your.app.id"
minSdkVersion 15
targetSdkVersion 23
versionCode getMyVersionCode()
versionName getMyVersionName()
archivesBaseName = "YourApp-${android.defaultConfig.versionName}"
}
然后你真的可以调用任何任务:
./gradlew assembleDebug -PversionCode=483 -PversionName=4.0.3
关于android - 是否可以通过 Gradle 任务设置 android 版本代码和名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36213977/