美文网首页FlutterFlutter学习
Flutter自动打包修改Android iOS项目版本号

Flutter自动打包修改Android iOS项目版本号

作者: Tasselx | 来源:发表于2020-08-05 13:25 被阅读0次

    在开发Flutter项目的时候自动打包可能需要修改到版本号,每次去到对应Android和iOS项目下面修改又不太方便,在Google了一遍后,尝试了下下面的方法可以在自动打包的时候修改打包版本号

    image

    Android项目

    项目配置

    修改项目下app/build.gradle文件,加入读取flutter中设置的versionCodeversionName

    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) 如图

    image

    打包命令

    flutter build ios --build-name=1.0.3
    

    参考: Versioning with Flutter

    相关文章

      网友评论

        本文标题:Flutter自动打包修改Android iOS项目版本号

        本文链接:https://www.haomeiwen.com/subject/fhgnrktx.html