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 应用。

相关推荐
nc_kai17 分钟前
Flutter 之 每日翻译 PreferredSizeWidget
java·前端·flutter
littlegnal1 小时前
Flutter Add-to-app profiling
flutter
0wioiw08 小时前
Flutter基础(FFI)
flutter
Georgewu9 天前
【HarmonyOS 5】鸿蒙跨平台开发方案详解(一)
flutter·harmonyos
爱吃鱼的锅包肉9 天前
Flutter开发中记录一个非常好用的图片缓存清理的插件
flutter
张风捷特烈10 天前
每日一题 Flutter#13 | build 回调的 BuildContext 是什么
android·flutter·面试
恋猫de小郭10 天前
Flutter 又双叒叕可以在 iOS 26 的真机上 hotload 运行了,来看看又是什么黑科技
android·前端·flutter
QC七哥10 天前
跨平台开发flutter初体验
android·flutter·安卓·桌面开发
小喷友11 天前
Flutter 从入门到精通(水)
前端·flutter·app