Flutter如何打包安卓apk包

作为安卓原生开发多年的我,也是第一次接触这种方式配置打包信息。原谅我的无知。

一、在项目根目录下新建 key.properties 文件

配置签名的基本信息

1
2
3
4
storePassword=chenyunlin
keyPassword=chenyunlin
keyAlias=Mark
storeFile=/Users/mark/Desktop/Document/keyStore/MarkKey.jks

二、配置到app目录下的 build.gradle 文件

在安卓目录下添加如下配置:导入配置 key.properties 的路径

1
2
3
4
5
6
def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
android{
······
}

配置签名的信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
android {
····
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
debug {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
····
}

这样下来安卓的配置信息配置完成。
下面修改buildType是:

1
2
3
4
5
6
7
8
9
10
buildTypes {
release {
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.release
}
debug {
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}

三、切换到Terminal中

输入 Flutter build apk,请静待程序运行完成。