Flutter项目打包

244 阅读3分钟

Flutter项目打包

添加App图标

  1. 安装 flutter_launcher_icons 插件

    flutter pub add dev:flutter_launcher_icons
    
  2. 添加配置信息,打开pubspec.yaml

    flutter_icons:
    image_path: "images/icon1024.png"
    android: true 
    ios: true
    
  3. 执行创建命令

    flutter pub run flutter_launcher_icons:main`
    
注意事项
  1. Format: 32-bit PNG
  2. icon 大小必须是  1024x1024
  3. 确保在 40px 大小时也能清晰可见,这是 icon 最小的尺寸。
  4. icon 不能大于 1024KB
  5. icon 不能是透明的。
  6. 必须是正方形,不能有圆角。

添加启动图标

  1. 安装flutter_native_splash插件‘

    flutter pub add flutter_native_splash
    
  2. 在 pubspec.yaml 的末尾加上配置

    flutter_native_splash:
     color: "#42a5f5"
     image: images/splash.png
    
  3. 执行创建命令

    flutter pub run flutter_native_splash:create
    
  4. 在 pubspec.yaml 中增加 branding配置

    flutter_native_splash:
     color: "#42a5f5"
     image: images/splash.png
     branding: images/logo.png 
    
注意

在安卓12+之后需要配置在pubspec.yaml添加

android_12:

background_color: "#ffffff" # 背景颜色

icon_background_color: "#ffffff" # 图标背景颜色

image: "assets/splash_icon.png" # 闪屏图标路径

签名

打开 Android Studio ,找到 Build 菜单下面的 Generated Signed Bundle/APK

11.png

有两个选项

  • Android App Bundle 如果你要上传到 google 商店,选这个。你暂可以理解为 这是 APK 优化方案,可惜不是所有的商店都支持。
  • APK 就是传统的打包,为了兼容所有商痁。

接下来又会弹窗

12.png

Key store path: 是你要将 key 保存在哪里,可以先找个地方放,后面可以 copy 到其它地方。应在位置路径末尾添加一个扩展名为 .jks 的文件名。点击 Create new 新建一个,比如文件名可以叫 key17.jks。

key store passowrd: 为您的密钥库创建并确认一个安全的密码。设置好了必须要记住。

  • Alias:为您的密钥输入一个标识名。 比如可以叫 key17。
  • Password:为您的密钥创建并确认一个安全的密码。它应该与密钥库密码相同。把它设成和 key store passowrd 一样就没问题了。密码要求至少 6 位。
  • Validity (years) :以年为单位设置密钥的有效时长。密钥的有效期应至少为 25 年,以便您可以在应用的整个生命期内使用同一密钥为应用更新签名。

Certificate: 为证书输入一些关于您本人的信息。此信息不会显示在应用中,但会作为 APK 的一部分包含在您的证书中。只有第一项是必须填的,其它的空着就行。

13.png

使用密钥为应用自动签名

先把创建好的 key 文件 copy 到 android 目录下。

建立 keystore.properties 文件

在 android 文件夹下建立名为 keystore.properties 文件

storePassword=123456
keyPassword=123456
keyAlias=key17
storeFile=../key17.jks

注意:这里的 keyAlias 要和前面设置的 key alias 保持一致,否则找不到 key。密码当然也要一样。

添加 gradle 配置

在 build.gradle 文件中,按如下方式加载 keystore.properties 文件(必须在 android 代码块前面)

def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties() 
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
输入存储在 keystoreProperties 对象中的签名信息
android {
  signingConfigs {
    release {
      keyAlias keystoreProperties['keyAlias']
      keyPassword keystoreProperties['keyPassword']
      storeFile file(keystoreProperties['storeFile'])
      storePassword keystoreProperties['storePassword']
    }
  }
  buildTypes {
       release {
            signingConfig signingConfigs.release
       }
  }
  ...
}

发布优化

启用缩减、混淆处理和优化功能,

ndroid {
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    ...
}

发布和安装

 flutter build apk --release

通过 flutter install 命令安装

打包的apk改名+版本号+时间配置

在android闭包里面

android.applicationVariants.all { variant ->  
 variant.outputs.all {  
 outputFileName = "GenerateAPK_${buildType.name}_v${versionName}_${generateTime()}.apk"  
 }  
}
def generateTime() {
    return new Date().format("yyyy_MM_dd_HH_mm_ss")
}