打包

Catalogue   

下载证书,本地安装到钥匙链中

配置Xcode

打包命令

修改项目的版本号和构建次数

若要使pubspec.yaml文件中的version生效,需要修改Android、iOS项目的配置文件。

Android项目配置文件修改

确认android/app/build.gradle文件中使用了Flutter的versionCode和versionName

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 FileNotFoundException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}

android {
compileSdkVersion 31

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

defaultConfig {
applicationId "com.xiaopeng.scepter"
minSdkVersion 21
targetSdkVersion 31
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
}

iOS项目

ios/Runner/Info.plist文件中使用Flutter的变量,使用$(FLUTTER_BUILD_NAME)和$(FLUTTER_BUILD_NUMBER)。

1
2
3
4
5
6
7
8
9
10
11
12
<?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>

构建脚本设置版本

1
2
3

# 设置版本号、设置构建次数
flutter build apk --build-name=1.1.1 --build-number=3

包体积优化

1
2
3
4
5
# --analyze-size参数用于生成*-code-size-analysis_*.json文件,该文件可以导入到devtools中进行分析包组成
flutter build xx --analyze-size

# 打开devtools
flutter pub global run devtools

优化方法

使用obfuscate参数

obfuscate参数会对应用进行混淆,split-debug-info参数配置符号表输出路径,用于后续解析。

1
2

flutter build apk --obfuscate --split-debug-info=/<project-name>/<directory>

参考