Android打包
AS新建签名打包
- 打开Flutter的Android目录
- Androidstudio > Build > Generate Signed Bundle / Apk
- 选择要打包的格式:Android App Bundle(.aab) 或者 Apk(.apk),点击Next
- Create new key Store (这里创建一个新的签名文件,如果已经创建了签名文件可以直接使用)
创建签名文件
字段 | 说明 |
---|---|
key store path | 签名文件路径 |
password | 密码 |
confirm | 确认密码 |
Alias | 别名 |
Validity(years) | 有效期限(年) |
First and Last Name | 全名 |
Organizational Unit | 组织单位 |
Organization | 组织 |
City or Locality | 城市或地区 |
State or Province | 州或省 |
Country Code(XX) | 国家代码 |
- 填入签名信息之后,点击ok,再选择Next
- Destination Folder,选择打包好之后输出的目录
- 选择要构建的版本,debug / profile / release,点击create
使用命令行打包
如果已经创建好了签名文件(例如签名文件:app.key),那么可以通过命令行打包
- 在android/app目录下创建文件夹key,把签名文件放置该目录,例如:/android/app/key/app.key
- 把 📎proguard-android.txt 和proguard-rules.pro(自定义混淆规则)两个文件放置/android/app/目录下,用于打包代码混淆
- 创建key.properties文件,放置/android/目录下,代码如下:
storePassword=填入你的密码
keyPassword=填入你的密码
keyAlias=填入签名文件的别名
storeFile=app/key/app.key
- 配置build.gradle文件,代码如下:
// 读取签名配置文件
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
def rootDirPath = getRootDir().toString() + File.separator
println "读取密钥文件" + keystorePropertiesFile.exists()
println "keyAlias = " + keystoreProperties['keyAlias']
println "keyPassword = " + keystoreProperties['keyPassword']
println "storeFile = " + keystoreProperties['storeFile']
println "storePassword = " + keystoreProperties['storePassword']
println "rootDir = " + rootDirPath
android {
defaultConfig {
applicationId "com.example.flutter_app"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
ndk {
// https://flutter.cn/docs/add-to-app/android/project-setup
// Flutter 当前仅支持 为 x86_64,armeabi-v7a 和 arm64-v8a 构建预编(AOT)的库
// 使用 abiFilters 这个 Android Gradle 插件 API 来指定 APK 中支持的架构,从而避免 lib flutter.so 无法生成而导致应用运行时崩溃
abiFilters "armeabi-v7a", "arm64-v8a", "x86_64"
}
}
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile rootDirPath + keystoreProperties['storeFile'] ? file(rootDirPath + keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
debug {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile rootDirPath + keystoreProperties['storeFile'] ? file(rootDirPath + keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
release {
// 开启代码混淆
minifyEnabled true
// Signing with the debug keys for now, so `flutter run --release` works.
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
// https://developer.android.com/studio/build/shrink-code?hl=zh-cn#native-crash-support
// 添加原生代码调试符号文件,原生代码调试符号文件的大小上限为 300 MB。如果调试符号占用空间过大,请使用 SYMBOL_TABLE 而不是 FULL 缩减文件大小。
ndk {
abiFilters "armeabi-v7a", "arm64-v8a", "x86_64"
debugSymbolLevel 'SYMBOL_TABLE'
}
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}
}
- 执行以下命令
flutter build apk --release
flutter build appbundle --release
- 打包命令执行完毕后,在项目的build目录找到可安装程序
build/app/outputs/flutter-apk/app-release.apk
build/app/outputs/bundle/release/app-release.aab
iOS打包
前置条件:macOS + Xcode
打包导出.ipa (测试包)
- 点击进入apple开发者后台
- 点击Identifiers创建应用的id
- 找到Flutter项目iOS目录下的Runner.xcworkspace文件,通过Xcode打开
- Runner > Signing & Capabilities > 选择Team
- Bundle identifier 与在开发者后台创建的应用id保持一致
- Xcode > Product > Archive
- Distribute App > Ad Hoc > Next > Next > Export
打包上传AppStore (正式包)
Distribute App > App Store Connect > Upload
MacOS打包
前置条件:macOS + Xcode
打包可执行程序
- 找到Flutter的iOS目录下的Runner.xcworkspace文件,通过Xcode打开
- Xcode > Product > Archive
- Distribute App > Development > Development Team > Automatically manage signing > Export
打包可安装程序
- 执行打包命令
flutter build macos
- 安装nodejs
brew install nodejs
- 执行打包命令
create-dmg '/Users/xxx/Desktop/FlutterProjects/xxx/build/macos/Build/Products/Release/xxx.app' --out '/Users/xxx/xxx.dmg'
Windows打包
前置条件:windows + Visual Studio(勾选c++)
打包可执行程序
- 执行打包命令
flutter build windows
- 打包命令执行完毕后,在项目的build目录找到可执行程序
build\windows\x64\runner\Release\
将runner下的Release目录(包含所有文件)拷贝出来,点击exe文件打开可执行程序
打包可安装程序
- 下载Inno Setup软件 (中文简体)
- 打开Inno Setup软件,新建打包脚本
设置脚本向导,点击下一步
填入应用程序的信息:软件名称,版本,公司,网站地址
添加安装文件夹名称
指定flutter编译后的exe可执行程序路径,并且点击“Add folder”添加\build\windows\runner\Release*文件夹
点击下一步
继续点击下一步,允许创建菜单栏的图标和创建桌面快捷方式
配置安装协议、安装前介绍、安装后描述内容的txt文件路径(可以留空不填)
配置安装模式:为所有用户安装或者询问
语言配置,可以选择多个语言
配置安装包导出路径、文件名称以及安装应用图标
生成打包脚本
点击完成保存脚本
- 运行脚本打包成exe安装程序
把脚本保存到指定目录,方便下次直接使用脚本打包
- 找到打包好的exe安装程序
双击运行安装测试
选择安装语言
自定义安装目录
创建桌面快捷方式
准备安装
安装完成