You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android构建脚本问题:build.gradle自定义版本递增任务报错

问题分析与解决方案

报错原因

从version.properties文件中读取的build值默认是字符串类型,而非整数。当直接执行build = build + 1或build++时,Groovy无法对字符串执行数值递增操作,会触发类型转换异常。

修正方案

需要在读取build值时显式转为整数,递增后再转回字符串写入文件。以下是修正后的incrementBuildNumber函数示例:

def incrementBuildNumber() {
    def versionPropsFile = file('version.properties')
    def versionProps = new Properties()
    versionProps.load(new FileInputStream(versionPropsFile))

    // 读取时将字符串转为整数
    def build = versionProps['build'].toInteger()
    build += 1 // 或使用 build++

    // 存回文件时转为字符串
    versionProps['build'] = build.toString()
    versionProps.store(new FileOutputStream(versionPropsFile), null)
}

额外注意事项

  • 确保version.properties文件格式规范,键值对的空格不影响读取,但要避免混入特殊字符
  • 执行Gradle任务时,确认当前用户对文件有读写权限,避免因权限问题导致写入失败

内容的提问来源于stack exchange,提问作者Seb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 23:45:37