flutter 发版的时候设置版本号

在 android/app/build.gradle 中设置版本号和版本昵称

接下来,需要在 Android 的 build.gradle 文件中配置 versionCode 和 versionName,这个配置通常在 defaultConfig 部分:

c 复制代码
android {
    ...
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 21
        targetSdkVersion 34
        versionCode 1  // 这里是 Android 的版本代码,必须是整数
        versionName "1.0.0"  // 这是 Android 的版本名称,通常是你发布给用户的版本号
        ...
    }
}

versionCode 和 versionName 的意义:

versionCode: 这个值在 Android 中用于区分不同版本的 APK。它应该是一个递增的整数,每次提交到 Google Play 时,versionCode 必须比之前的版本号大。

versionName: 这是用户可见的版本号,通常是 1.0.0、2.1.0 等格式。它不需要递增(不过大多数项目会递增),且它是展示给用户的版本信息。

自动化版本号(通过 git 获取)

如果你想自动化版本号和版本昵称(如 getAppVersionCode() 和 getAppVersionName() ),你可以通过 git 命令来动态获取 Git 提交的数量或者标签。

在 android/app/build.gradle 文件中,你可以利用 Groovy 脚本来获取 Git 版本号:

c 复制代码
def getAppVersionCode() {
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'rev-list', '--first-parent', '--count', 'HEAD'
            standardOutput = stdout
        }
        return Integer.parseInt(stdout.toString().trim()) + 100
    } catch (ignored) {
        println "===================error code!"
        return 100
    }
}

def getAppVersionName() {
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'describe', '--tags'
            standardOutput = stdout
        }
        return stdout.toString().trim()
    } catch (ignored) {
        println "===================error name!"
        return "1.0.0"
    }
}

android {
    defaultConfig {
        versionCode getAppVersionCode()
        versionName getAppVersionName()
    }
}

在上述示例中:

getAppVersionCode() 通过 git rev-list --count HEAD 获取提交的数量并加 100,生成版本代码。

getAppVersionName() 通过 git describe --tags 获取最近的 Git 标签作为版本名称。

5. 使用 Flutter 命令行发布

当你准备好版本号和版本昵称时,可以使用以下命令进行打包和发布:

打包 APK:

c 复制代码
flutter build apk --release

打包 AppBundle(推荐方式,尤其是当你需要通过 Google Play 进行发布时):

c 复制代码
flutter build appbundle --release

总结

pubspec.yaml:设置 version: 1.0.0+1,versionName 和 versionCode。

build.gradle:在 defaultConfig 中设置 versionName 和 versionCode。

自动化获取:通过 git 脚本动态获取版本号和版本昵称。

通过这两个文件的配置,你就可以顺利地修改版本号和版本昵称,并准备发布 Flutter 应用。

相关推荐
MonkeyKing6 小时前
Flutter约束模型(BoxConstraints)与布局体系完全解析
flutter
IntMainJhy7 小时前
Flutter 三方库 ImageCropper 图片裁剪鸿蒙化适配与实战指南(正方形+自定义比例全覆盖)
flutter·华为·harmonyos
IntMainJhy8 小时前
Flutter for OpenHarmony 第三方库六大核心模块整合实战全解|从图片处理、消息通知到加密存储、设备推送 一站式鸿蒙适配开发总结
flutter·华为·harmonyos
张风捷特烈8 小时前
状态管理大乱斗#02 | Bloc 源码全面评析
android·前端·flutter
IntMainJhy8 小时前
【fluttter for open harmony】Flutter 三方库适配实战:在 OpenHarmony 上实现图片压缩功能(附超详细踩坑记录)
flutter·华为·harmonyos
jiejiejiejie_8 小时前
Flutter for OpenHarmony 多语言国际化超简单实现指南
flutter·华为·harmonyos
里欧跑得慢9 小时前
12. CSS滤镜效果详解:为页面注入艺术灵魂
前端·css·flutter·web
里欧跑得慢9 小时前
CSS 级联层:控制样式优先级的新方式
前端·css·flutter·web
恋猫de小郭11 小时前
Jetpack Compose 1.11 正式版发布,下一代的全新控件和样式 API,你必须知道
android·前端·flutter
IntMainJhy12 小时前
Flutter 三方库 ImagePicker 的鸿蒙化适配与实战指南(相机/相册/多图选择全实现)
数码相机·flutter·harmonyos