当项目开发完毕,准备上线前,需要对应用进行最终构建。

本节的目前有如下几点:

  • 编译 build releae
  • 程序瘦身
  • 混淆程序
  • 修改程序名称
  • 制作图标
  • 制作启动画面

可参考Flutter 实战从零开始 新闻客户端 - 10 编译发布正式版 | duCafeCat

1. APP图标

1.1 规格尺寸

规格说明:

1.2 图标尺寸

android 512x512

ios 1024x1024

1.3 在线工具

https://www.designevo.com/cn/logo-maker/

1.4 flutter_launcher_icons 插件

https://pub.dev/packages/flutter_launcher_icons

1.5 生成图标

通过上面的在线工具设计好icon,然后编辑pubspec.yaml文件利用插件来生成icon。

  1. 设置配置文件

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    dev_dependencies:
      flutter_launcher_icons: "^0.13.1"
    
    flutter_launcher_icons:
      android: "launcher_icon"
      ios: true
      image_path: "assets/icon/icon.png"
      min_sdk_android: 21 # android min sdk min:16, default 21
      web:
        generate: true
        image_path: "path/to/image.png"
        background_color: "#hexcode"
        theme_color: "#hexcode"
      windows:
        generate: true
        image_path: "path/to/image.png"
        icon_size: 48 # min:48, max:256, default: 48
      macos:
        generate: true
        image_path: "path/to/image.png"
    
  2. 运行插件

    1
    2
    
    flutter pub get
    flutter pub run flutter_launcher_icons
    

1.6 图片目录

android/app/src/main/res

ios/Runner/Assets.xcassets/AppIcon.appiconset

2. 启动图片

2.2.1 规格说明

https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/adaptivity-and-layout/#device-screen-sizes-and-orientations

https://developer.android.com/about/dashboards/index.html#Screens

https://uiiiuiii.com/screen/

2.2.2 图片尺寸

iPhone XS Max 1242px × 2688px

android xxhdpi xhdpi

2.2.3 在线工具

https://hotpot.ai/icon_resizer

利用上述在线工具生成对应大小的图片,分别防止在对应文件夹 修改app/src/main/res/drawable/launch_background.xml以及app/src/main/res/drawable-v21/launch_background.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--    <item android:drawable="?android:colorBackground" />-->
    <!-- 自定义的背景色 -->
    <item android:drawable="@color/cyan" />
    <!-- You can insert your own image assets here -->
    <item>
        <!-- launch_image就是不同大小尺寸下相同的启动图名称 -->
        <bitmap
            android:gravity="fill"
            android:src="@mipmap/launch_image" />
    </item>
</layer-list>

3. Android发布

3.1 证书签名说明

https://developer.android.com/studio/publish/app-signing?hl=zh-cn

3.2 生成证书

1
2
3
4
5
# 进入目录 android/app/
keytool -genkey -v -keystore ./key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key

# 输出文件
android/app/key.jks

3.3 Gradle 配置

  • android/gradle.properties
android.enableAapt2=false # 不检测依赖资源

这个配置暂时不知道具体的目的

  • android/key.properties‘
storePassword=123456
keyPassword=123456
keyAlias=key
storeFile=./key.jks

声明key属性

  • android/app/build.gradle
// 定义属性读取对象,读取 android/key.properties
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 28

    ...

    // 签名配置
    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {

        // 发布配置
        release {
            signingConfig signingConfigs.release
        }
    }
}

3.4 修改版本号

  • pubspec.yaml

    1
    
    version: 1.0.0+1
    

3.5 修改程序名称

  • android/app/src/main/AndroidManifest.xml
1
2
3
4
<application
    android:name="io.flutter.app.FlutterApplication"
    android:label="天气预报"
    android:icon="@mipmap/launcher_icon">

设置App名称并提供对应的icon地址

3.6 设置网络权限

  • android/app/src/main/AndroidManifest.xml
1
2
3
4
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

3.7 编译打包

1
flutter build apk --split-per-abi

打包输出如下:

1
2
3
✓ Built build/app/outputs/apk/release/app-armeabi-v7a-release.apk (7.2MB).
✓ Built build/app/outputs/apk/release/app-arm64-v8a-release.apk (7.4MB).
✓ Built build/app/outputs/apk/release/app-x86_64-release.apk (7.6MB).

3.8 混淆编译

https://github.com/flutter/flutter/wiki/Obfuscating-Dart-Code

Flutter 1.16.2 以上默认支持混淆,不需要特殊设置,只需要在构建命令后面加上: --obfuscate --split-debug-info=/<project-name>/<directory>

从 flutter 1.16.2 开始,以下信息已过时。仅当您使用的是较早版本的 Flutter 时才使用此选项。如果您使用的是 Flutter 1.16.2 或更高版本,请参阅flutter.dev 上的Obfuscating Dart code

  • android/gradle.properties
extra-gen-snapshot-options=--obfuscate
  • android/proguard-rules.pro
#Flutter Wrapper
-dontwarn io.flutter.**
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }
  • android/app/build.gradle
buildTypes {
    release {
        signingConfig signingConfigs.release

        minifyEnabled true  //资源压缩设置
        useProguard true    //代码压缩设置

        //读取代码压缩配置文件
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

    }
}

然后再次发布编译即可!

3.9 减少应用大小

当构建应用的发行版本时,考虑使用 --split-debug-info 标记。该标记会显著减少代码量。关于使用此标记的示例,请查看文档 Obfuscating Dart code

其他减少应用大小的方式:

  • 删除无用的资源

  • 尽量减少从库中引入的资源

  • 压缩 PNG 和 JPEG 文件

要减少Flutter应用的大小,可以考虑以下几种方法:

  1. 移除不必要的依赖库:检查项目中使用的第三方库,确保只包含实际需要的库。有时候,开发者会引入一些不必要的库,导致应用大小增加。

  2. 优化资源文件:检查应用中的资源文件,如图像、音频和视频等。使用适当的压缩算法来减小它们的大小,或者考虑使用WebP或其他高效的图像格式。此外,只包含应用实际使用的资源文件,避免不必要的资源文件增加应用大小。

  3. 代码混淆和压缩:使用代码混淆工具来减小应用的代码大小, 这可以减少应用的整体大小。

  4. 按需加载:使用Flutter的延迟加载功能,按需加载和初始化组件和资源。这样可以减少初始加载时的应用大小。

  5. 使用ABI分割:对于Android应用,可以使用ABI(Application Binary Interface)分割功能,将应用根据不同的CPU架构分割成多个APK文件。这样,在用户设备上只需下载和安装与其设备架构相对应的APK,减少了下载和安装的大小。

  6. AOT编译:使用AOT(Ahead-of-Time)编译模式,而不是JIT(Just-in-Time)模式。AOT编译可以减少应用启动时间和内存占用,因为它将Dart代码提前编译为本机机器码。

  7. 使用App Bundle:对于Android应用,使用Android App Bundle格式进行发布。App Bundle可以根据用户设备的配置和语言设置,动态生成优化的APK文件,减少下载和安装的大小。

  8. 移除调试和开发相关的代码和资源:在发布应用之前,确保移除所有与调试和开发相关的代码和资源,这些通常不会在正式发布的应用中使用,但会增加应用的大小。

以上是一些常见的方法,可以帮助减少Flutter应用的大小。根据具体情况,你可以选择适合你的应用的方法来减小应用大小。