flutter 一键打出不同包名、应用名、版本名、签名、应用图标、版本号的安装包

1. build.gradle 文件中配置不同的应用信息

    flavorDimensions "app"
    productFlavors {
        app1 {
            //配置包名
            manifestPlaceholders = [str: "releaseStr", package_name: "com.example.demo1"]
            applicationId "com.example.demo1"
            versionCode 1
            versionName "1.0"
            //配置应用名称
            resValue "string", "app_name", "应用1"
            resValue "bool", "isrRank", 'true'
            buildConfigField "int", "TYPE", "1"
            //配置应用图标
            manifestPlaceholders = [ENVIRONMENT: "app1",
                                    app_icon   : "@mipmap/ic_launcher1"]
            signingConfig signingConfigs.release1
        }
        app2 {
            manifestPlaceholders = [str: "releaseStr", package_name: "com.example.demo2"]
            applicationId "com.example.demo2"
            versionCode 2
            versionName "2.0"
            resValue "string", "app_name", "app2"
            resValue "bool", "isrRank", 'true'
            buildConfigField "int", "TYPE", "2"
            manifestPlaceholders = [ENVIRONMENT: "app2",
                                    app_icon   : "@mipmap/ic_launcher2"]
            signingConfig signingConfigs.release2
        }
    }

另外,记得在 AndroidManifest.xml 中配置相关信息

 <application
        android:allowBackup="true"
        android:icon="${app_icon}"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

2. 加入flavor 后,调试运行报错:

Exception: Gradle build failed to produce an .apk file. It's likely that this file was generated under D:\Rl\project\ywAppFlutter\build, but the tool couldn't find it.

原因:flutter在编译完成后,找不到相应的渠道包下面生成的apk,需要配置对应的渠道的apk

解决方案:红线处配置相应的渠道名称
在这里插入图片描述
flutter 一键打出不同包名、应用名、版本名、签名、应用图标、版本号的安装包_第1张图片

3.打包命令

Flavor和入口文件配置完成后,我们可以尝试打包了,打包方式如下:flutter build apk --[debug/release] --flavor [flavor] -t [entrance]参数说明:[debug/release]:指定debug或release,[flavor]:指定Flavor,[entrance]:指定入口文件名称。所以打包令示例如下:

flutter build apk --debug --flavor app1 -t lib/main_android_dev.dart
flutter build apk --release --flavor app1 -t lib/main_android_dev.dart

参考文章链接:
干货:在Flutter项目下安卓flavor打包配置实践
Android Gradle —— flavorDimensions 与 productFlavors
android 一键打包。一键打出不同包名、应用名、版本名、签名、应用图标、版本号的安装包

你可能感兴趣的:(flutter)