How to set build and version number of Flutter app

前端 未结 5 1867
悲哀的现实
悲哀的现实 2020-12-29 19:55

How do you set the version name and version code of a Flutter app without having to go into the Android and iOS settings?

In my pubspec.yaml I have

v         


        
5条回答
  •  生来不讨喜
    2020-12-29 20:50

    Thanks to user abion47 for finding and condensing the following answer from the article Versioning with Flutter.

    Re-enabling auto versioning

    By default a Flutter project is set up to automatically update the Android and iOS settings based on the version setting in pubspec.yaml when you build the project. If, however, you have since overridden those settings, you can re-enable that behavior by doing the following:

    iOS

    Open the ios/Runner/Info.plist file. Set the value for CFBundleVersion to $(FLUTTER_BUILD_NUMBER) and set the value for CFBundleShortVersionString to $(FLUTTER_BUILD_NAME). The XML for the file should look something like this:

    
    
    
    
      ...
      CFBundleVersion
      $(FLUTTER_BUILD_NUMBER)
      CFBundleShortVersionString
      $(FLUTTER_BUILD_NAME)
      ...
    
    ...
    

    Android

    Open the android/app/build.gradle file. Ensure you are properly loading the Flutter properties at the top of the file:

    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.")
    }
    

    Then set the android.defaultConfig section so that versionName is flutterVersionName and versionCode is flutterVersionCode.toInteger():

    android {
      ...
      defaultConfig {
        ...
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
      }
    }
    

提交回复
热议问题