基于单一Flutter代码库自动创建并发布多应用的技术实现问询
实现思路与方案
一、资源与配置自动替换(配置文件、启动页、图标、应用名称)
完全可以通过脚本实现自动化替换,核心思路是提前准备好各渠道的资源,用脚本批量替换项目内的对应文件或字段:
- 应用名称与Bundle Identifier:直接用脚本修改原生配置文件,比如用
sed替换安卓strings.xml里的应用名称,用plutil修改iOSInfo.plist的Bundle ID:# 替换安卓应用名称 sed -i '' 's/默认应用名/新应用名/g' android/app/src/main/res/values/strings.xml # 替换iOS Bundle ID plutil -replace CFBundleIdentifier -string "com.new.bundle.id" ios/Runner/Info.plist - 应用图标与启动页:按渠道分类存储图标、启动页资源,打包前用脚本复制覆盖项目内的对应目录。比如安卓替换
android/app/src/main/res/mipmap-*下的图标,iOS替换ios/Runner/Assets.xcassets/AppIcon.appiconset内的文件,启动页同理替换对应资源目录。 - 配置文件:把不同环境的配置文件(比如API地址、环境标识)按规则命名,用脚本复制覆盖项目默认配置,比如
cp configs/new_channel_config.dart lib/config.dart。
二、Firebase推送配置适配:无需新建项目
同一个Firebase项目可以添加多个应用(对应不同Bundle Identifier),完全没必要每次新建项目:
- 先在Firebase控制台手动为每个需要的Bundle ID创建对应应用,下载各自的
google-services.json(安卓)和GoogleService-Info.plist(iOS),按渠道命名存储(比如google-services-channelA.json)。 - 打包前用脚本将对应渠道的Firebase配置文件复制到项目指定路径:
# 替换安卓Firebase配置 cp configs/google-services-channelA.json android/app/google-services.json # 替换iOS Firebase配置 cp configs/GoogleService-Info-channelA.plist ios/Runner/GoogleService-Info.plist - 若极端场景需要动态创建Firebase应用,可调用Firebase Admin SDK的API实现,但手动预创建已经能满足绝大多数需求,没必要过度复杂。
三、自动发布至应用商店
1. Google Play Store
- 用
flutter build appbundle构建AAB包,再通过Google Play Publisher API配合脚本(Python/Shell)上传,或用Fastlane的supply插件一键完成上传、版本更新。 - 提前在Google Cloud控制台配置服务账号并获取密钥文件,设置好环境变量后即可通过脚本自动调用API完成发布。
2. App Store
- 用Fastlane的
gym插件构建IPA包,再用deliver插件上传至App Store Connect。 - 配合Fastlane的
match插件可自动管理苹果开发者账号的证书、描述文件,实现从构建到发布的全流程自动化。
四、整体流程串联
把上述步骤整合为Shell脚本或用Makefile管理,示例脚本如下:
#!/bin/bash # 1. 替换应用名称与Bundle ID sed -i '' 's/默认应用名/渠道A应用名/g' android/app/src/main/res/values/strings.xml plutil -replace CFBundleIdentifier -string "com.channelA.app" ios/Runner/Info.plist # 2. 替换图标与启动页 cp -r assets/icons/channelA/* android/app/src/main/res/mipmap-xxxhdpi/ cp -r assets/launcher/channelA/* ios/Runner/Assets.xcassets/LaunchImage.imageset/ # 3. 替换Firebase配置 cp configs/google-services-channelA.json android/app/google-services.json cp configs/GoogleService-Info-channelA.plist ios/Runner/GoogleService-Info.plist # 4. 构建并发布安卓包 flutter build appbundle --release fastlane supply --aab build/app/outputs/bundle/release/app-release.aab # 5. 构建并发布iOS包 flutter build ios --release fastlane gym --scheme Runner --export_method app-store fastlane deliver --ipa build/ios/ipa/Runner.ipa
内容的提问来源于stack exchange,提问作者saibot
相关产品推荐
相关产品推荐

