解决Flutter上架Google Play提示Version code 1 has already been used. Try another version code.

本文介绍了在使用Flutter打包APP Bundle时遇到版本号冲突的问题及解决方案。当尝试上传版本1.0.0+1到GooglePlay时,由于版本号已被使用,故提示错误。解决办法是修改`pubspec.yaml`文件中的versionCode,将其值增加,例如改为1.0.0+2,然后重新打包上传即可成功。

出现场景

使用flutter build appbundle打包aab时
因为上个版本是第一个默认版本1.0.0。所以这次打包我们在版本名后面加了1。

name: flutter_demo
description: A new Flutter application for Flutter demo

# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

上传到Google Play,提示以下错误信息。

Version code 1 has already been used. Try another version code.

在这里插入图片描述

原因分析

首先我们得搞清楚Flutter中的版本是如何管理的。

Flutter打包的版本名和版本号默认是从pubspec.yaml中的version读取的。我们只需要更新它的值就可以了。

格式为

version:A.B.C+X

对于Android的版本来说,版本号的管理在app/build.gradle中配置。

android{
    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.exmaple.demo"
        minSdkVersion 21
        targetSdkVersion 31
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }
}

A.B.C代表了verisonName,比如默认初始值为1.0.0.
X代表versionCode,默认值从1开始,依次累加。
默认App的versionName为1.0.0,versionCode为1。

解决方案

因为对于默认的versionName来说,后面不加上versionCode,versionCode就会默认为1.所以1.0.01.0.0+1是没有任何区别的。

我们只需要在versionCode后面加上更大的数字即可。比如+2.

name: flutter_demo
description: A new Flutter application for Flutter demo

# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# 1更改为2
version: 1.0.0+2

重新打包后上传,就能成功上传到Google Play了。

### 解决方案 在最新版本的 Android Studio 中,确实发生了变化,原本捆绑的 JRE 被替换为了 `jbr` 目录[^2]。这导致了当运行 `flutter doctor` 命令时会报告 “Unable to find bundled Java version.” 的错误消息。 #### 方法一:创建软链接 可以通过为新的 `jbr` 文件夹创建一个指向旧名称 `jre` 的软链接来解决问题。以下是具体操作: 1. **定位路径**: 首先找到你的 Android Studio 安装位置下的 `Contents` 文件夹。 对于 macOS 用户来说,默认安装路径可能是 `/Applications/Android Studio.app/Contents`. 2. **执行命令**: 使用终端工具,在上述路径下输入以下命令以创建软连接: ```bash ln -s jbr jre ``` 此命令的作用是在当前目录中建立名为 `jre` 的符号链接至实际存在的 `jbr` 文件夹[^3]。 #### 方法二:手动指定 JAVA_HOME 环境变量 另一种方式是通过设置系统的环境变量 `JAVA_HOME` 来显式定义使用的 JDK 版本。这样可以绕过寻找默认嵌入式 JDK 的过程。 - Windows 平台上的步骤如下: 1. 右键点击“计算机”,选择属性 -> 高级系统设置 -> 环境变量; 2. 新建或者编辑现有的 `JAVA_HOME` ,将其值设为你本地已安装的一个兼容 JDK 的完整路径(比如 C:\Program Files\Java\jdk-xx.x.x_x\) ; 3. 同样确认 PATH 包含 `%JAVA_HOME%\bin`. 对于 Linux 或 Mac OS X, 则需修改 `.bashrc`,`.zshrc` 等配置文件加入相应声明语句如 export JAVA_HOME=/path/to/jdk 和 export PATH=$PATH:$JAVA_HOME/bin. 完成以上任一步骤之后再次尝试运行 `flutter doctor –v` 查看问题是否得到解决[^4]. ```python import os print(os.environ.get('JAVA_HOME')) ``` 上面这段 Python 小脚本可以帮助验证当前环境中是否有正确设定好该参数以及其对应的绝对地址是什么样子的。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值