首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android :在构建时动态更改versionName

Android :在构建时动态更改versionName
EN

Stack Overflow用户
提问于 2014-01-28 18:57:10
回答 6查看 39.5K关注 0票数 37

我试图通过使用定制版本的gradle发布插件:https://github.com/townsfolk/gradle-release来模仿Android中的Maven发布插件。

令人感兴趣的步骤是:

  • 检查未提交的更改
  • 执行版本代码步骤,并从版本名称中删除-SNAPSHOT后缀
  • 构建
  • 步骤版本号并为下一个开发版本添加-SNAPSHOT后缀

但是,生成的APK总是具有以前的版本(即1.0.0-快照而不是1.0.0)。

版本号是在gradle.properties中存储和正确更新的,因此我假设我也需要更新数据模型中的版本,以使更改生效。

我的android插件配置:

代码语言:javascript
运行
复制
defaultConfig {
    versionCode versionCode as int  // taken from gradle.properties
    versionName versionName // taken from gradle.properties
    minSdkVersion 10
    targetSdkVersion 19
}

我试过的事情:

代码语言:javascript
运行
复制
preBuild << {
    android.applicationVariants.each { variant ->
        variant.versionName = versionName
    }
}

但是变体中没有versionName。

代码语言:javascript
运行
复制
preBuild << {
    android.buildTypes.each { type ->
        type.versionName = versionName
    }
}

但是在一个类型中没有versionName。

代码语言:javascript
运行
复制
preBuild << {
    android.productFlavors.each { flavor ->
        flavor.versionName = versionName
    }
}

但是在我的应用程序中没有风格(仅简单地调试和发布构建类型)。

我的替代方法是编写bash/bat脚本,在调用Gradle之前对版本进行分步,这与使用Groovy改进构建自定义的目的大相径庭。

如何在执行阶段动态更新Android插件中的版本?

EN

Stack Overflow用户

发布于 2022-06-11 16:03:33

版本名称和版本代码都可以按以下方式动态生成。代码中的注释。

代码语言:javascript
运行
复制
/**
 * 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())
       ....
    }
票数 0
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21414399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档