在开发Flutter项目的时候自动打包可能需要修改到版本号,每次去到对应Android和iOS项目下面修改又不太方便,在Google了一遍后,尝试了下下面的方法可以在自动打包的时候修改打包版本号
imageAndroid项目
项目配置
修改项目下app/build.gradle
文件,加入读取flutter中设置的versionCode
和versionName
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
throw new GradleException("versionCode not found. Define flutter.versionCode in the local.properties file.")
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
throw new GradleException("versionName not found. Define flutter.versionName in the local.properties file.")
}
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 27
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.hello"
minSdkVersion 16
targetSdkVersion 27
versionCode flutterVersionCode.toInteger()
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
打包命令
在打包命令后面增加--build-name
或者--build-number
具体看需要
flutter build apk --build-name=1.0.3
flutter build apk --build-number=3
flutter build apk --build-name=1.0.3 --build-number=3
iOS项目
项目配置
在项目目录下面找到info.plist
文件,以源码方式打开,设置CFBundleShortVersionString
键的值为$(FLUTTER_BUILD_NAME)
,CFBundleVersion
设置为$(FLUTTER_BUILD_NUMBER)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
...
<key>CFBundleShortVersionString</key>
<string>$(FLUTTER_BUILD_NAME)</string>
<key>CFBundleVersion</key>
<string>$(FLUTTER_BUILD_NUMBER)</string>
...
</dict>
</plist>
在项目中build settings
搜索versioning
设置Current Project Version
的值为(FLUTTER_BUILD_NUMBER)
如图
打包命令
flutter build ios --build-name=1.0.3
网友评论