我试图通过使用定制版本的gradle发布插件:https://github.com/townsfolk/gradle-release来模仿Android中的Maven发布插件。
令人感兴趣的步骤是:
但是,生成的APK总是具有以前的版本(即1.0.0-快照而不是1.0.0)。
版本号是在gradle.properties中存储和正确更新的,因此我假设我也需要更新数据模型中的版本,以使更改生效。
我的android插件配置:
defaultConfig {
versionCode versionCode as int // taken from gradle.properties
versionName versionName // taken from gradle.properties
minSdkVersion 10
targetSdkVersion 19
}
我试过的事情:
preBuild << {
android.applicationVariants.each { variant ->
variant.versionName = versionName
}
}
但是变体中没有versionName。
preBuild << {
android.buildTypes.each { type ->
type.versionName = versionName
}
}
但是在一个类型中没有versionName。
preBuild << {
android.productFlavors.each { flavor ->
flavor.versionName = versionName
}
}
但是在我的应用程序中没有风格(仅简单地调试和发布构建类型)。
我的替代方法是编写bash/bat脚本,在调用Gradle之前对版本进行分步,这与使用Groovy改进构建自定义的目的大相径庭。
如何在执行阶段动态更新Android插件中的版本?
发布于 2022-06-11 16:03:33
版本名称和版本代码都可以按以下方式动态生成。代码中的注释。
/**
* computedVersionCode()
* do not name this to getVersionCode. getVersionCode conflicts with the automatic getter of versionCode
* version code is an int a value between 0 and max int value 2147483647 is expected.
* This function returns at int in yyyMMddHH format
* For example, 2022061121 for 11 June 2022 between 21:00 to 21:59
* This gives a new versioncode for every different hour of day and same code within same hour of hour of day
* Max int value is 2147483647. So after year 2147 it will overflow to -ve values.
* max value in year 2147 will be 2147121223 so Lot of scope of manually incrementing up-to 2147483647 will be there.
* @return an int corresponding to current hour in yyyyMMddHH format
*/
static def computedVersionCode() {
def date = new Date()
def formattedDate = date.format('yyyyMMddHH')
int versionCodeInt = (int) Long.parseLong(formattedDate)
return versionCodeInt
}
/**
* computedVersionCode2()
* do not name this to getVersionCode. getVersionCode conflicts with automatic getter of versionCode
* version code is an int a value between 0 and Max int value 2147483647 is expected.
* This function returns total hours since epoch
* For example, it returns 459711 for 11 June 2022 at 21:21 IST
* This gives a new versioncode for every different hour
* Max int value is 2147483647. This format is good till 09-Oct-246953 12:30:00 PM
*
* @return hours since epoch which can be used as version code
*/
static def computedVersionCode2() {
long millisSinceEpoch = System.currentTimeMillis();
long hoursSinceEpoch = millisSinceEpoch/(3600*1000);
int hoursSinceEpochInt = (int)hoursSinceEpoch;
//Hours since epoch changes every hour automatically.
//If data type int remains of same size forever this value will be good till year 4419.
return hoursSinceEpochInt;
}
static def computedVersionSuffix() {
def date = new Date()
def formattedDate = date.format('yyyyMMdd.HH.mm')
return formattedDate
}
android {
compileSdkVersion 32
defaultConfig {
...
versionCode computedVersionCode()
versionName "1.0.8.".concat(computedVersionSuffix())
....
}
https://stackoverflow.com/questions/21414399
复制相似问题