Flutter Notes|Flutter-Apk 大小優化探索

追逐,探索,永不停歇~android

在這裏插入圖片描述

前言

還記得剛入坑 Flutter 打包時,被深深震驚了一番,臥槽,這包好大!git

  • ✓ Built build/app/outputs/apk/release/app-release.apk (23.8MB).

足足將近 24 MB,第一反應真的懵逼了。github

固然直接提交市場後,也是被人各類 diss,緣由仍是沒什麼功能,包賊大,用戶下載賊不舒服。緩存

強烈要求優化 Apk 大小。bash

既然是探索,前提我仍是個剛入 Flutter 坑的小白白,因此嘛,不免不全面,歡迎各位大佬拍磚、指點~架構

探索之路 一部曲

首先,我首次打包的方式以下:app

雷同使用下面的命令(默認帶有 --release):ide

  • flutter build apk

1、熊貓壓縮法(減小 0.7 MB)

首先第一想法,圖片我沒作壓縮,一樣通過查看後,發現圖片在 apk 佔比爲 4.1% :post

  • 2.3 % Flutter 引用到的資源文件;
  • 1.8% Android 啓動頁的背景圖。

最後,咱們經過國寶之手試試最後能減小多少?測試

這裏分別針對 Flutter 下圖片資源、Android/iOS 啓動頁進行壓縮。

再次運行 build apk 後,完成輸出以下日誌:

  • ✓ Built build/app/outputs/apk/release/app-release.apk (23.1MB).

再來看 Apk 中圖片的佔比以及下降到 1%:

最終 Apk 大小直接減小了 0.7 MB,仍是比較爽的。

2、so 優化大法(減小 14 MB)

作 Android 的小夥伴知道,對於咱們這些小廠沒能力搞動態下發 so 的小渣渣而言,只能默默逆向大廠 Apk,看看人家是怎麼作的,而後借(抄)鑑(襲)。

針對 Flutter 打出的 Apk 包,排在第一位的即是 lib,佔比 86.4%,足足有 19.6 MB:

這裏看到將咱們編寫的 Dart 代碼轉化爲不一樣架構下的 so 庫,以供原生調用(我是這麼猜想的哈)。

針對不一樣 CPU 架構所表明含義,尤爲 Flutter 打包 Apk 生成的三種 CPU 架構分別對應什麼含義:

  • x86_64: Intel 64 位,通常用於平板或者模擬器,支持 x86 以及 x86_64 CPU 架構設備。
  • arm64-v8a: 第 8 代 64 位,包含 AArch3二、AArch64 兩個執行狀態,且對應 32 、64 bit,而且支持 armeabi、armeabi-v7a 以及 arm64-v8a。
  • armeabi-v7a: 第 7 代 arm v7,使用硬件浮點運算,具備高級拓展功能,兼容 armeabi 以及 armeabi-v7a,並且目前大部分手機都是這個架構。

其實咱們第一次經過 flutter build apk 命令生成 apk 時,Google 這裏已經爲咱們提示了:

heliquan@Mac  ~/CodePro/FlutterPro/xxx_app master ● flutter build apk
You are building a fat APK that includes binaries for android-arm, android-arm64, android-x64.
If you are deploying the app to the Play Store, it's recommended to use app bundles or split the APK to reduce the APK size. To generate an app bundle, run: flutter build appbundle --target-platform android-arm,android-arm64,android-x64 Learn more on: https://developer.android.com/guide/app-bundle To split the APKs per ABI, run: flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi Learn more on: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split Running Gradle task 'assembleRelease'... Running Gradle task 'assembleRelease'... Done 13.8s ✓ Built build/app/outputs/apk/release/app-release.apk (23.1MB). 複製代碼

接下來經過如下命令進行分別打包(構建指定 CPU 架構類型 Apk 包):

  • flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi

這裏解釋下這個命令的含義:

  • 首先 flutter build apk 表示當前構建 release 包;
  • 後面 android-arm,android-arm64,android-x64 則是指定生成對應架構的 release 包;
  • 最後的 --split-per-abi 則表示告知須要按照咱們指定的類型分別打包,若是移除則直接構建包含全部 CPU 架構的 Apk 包。

因此這個命令的含義就是告訴編譯器,我須要你爲我針對我指定的三種不一樣架構分別生成對應的 Apk 包。

有的小夥伴就說了,你這空口無憑,沒證據啊。

好,我給你運行一波~

  • 驗證:flutter build apk --target-platform android-arm,android-arm64,android-x64 結果
heliquan@Mac  ~/CodePro/FlutterPro/xxx_app   master ●  flutter build apk --target-platform android-arm,android-arm64,android-x64                
You are building a fat APK that includes binaries for android-arm, android-arm64, android-x64.
If you are deploying the app to the Play Store, it's recommended to use app bundles or split the APK to reduce the APK size. To generate an app bundle, run: flutter build appbundle --target-platform android-arm,android-arm64,android-x64 Learn more on: https://developer.android.com/guide/app-bundle To split the APKs per ABI, run: flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi Learn more on: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split Removed unused resources: Binary resource data reduced from 817KB to 815KB: Removed 0% Running Gradle task 'assembleRelease'... Running Gradle task 'assembleRelease'... Done 115.8s ✓ Built build/app/outputs/apk/release/app-release.apk (23.1MB). 複製代碼

看見沒,事實論證結果。

最後,咱們採起告知編譯器爲咱們生成指定 CPU 架構的 Apk 的方式,並查看對應輸出日誌信息:

heliquan@Mac  ~/CodePro/FlutterPro/xxx_app   master ●  flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi                                         
Removed unused resources: Binary resource data reduced from 816KB to 814KB: Removed 0%
Removed unused resources: Binary resource data reduced from 816KB to 814KB: Removed 0%
Removed unused resources: Binary resource data reduced from 816KB to 814KB: Removed 0%
Running Gradle task 'assembleRelease'...                                
Running Gradle task 'assembleRelease'... Done                      36.0s
✓ Built build/app/outputs/apk/release/app-armeabi-v7a-release.apk (9.8MB).
✓ Built build/app/outputs/apk/release/app-arm64-v8a-release.apk (10.1MB).
✓ Built build/app/outputs/apk/release/app-x86_64-release.apk (10.2MB).
複製代碼

看看 app-armeabi-v7a-release.apk 包大小,結果是否是賊喜人?由 23.8 MB 直接減小到 9.8 MB。

隨後咱們看下對應的 apk 內容:

lib 佔比也從原來的 86.4%,19.6 MB 直接減小爲 67.2%,大小 6.3 MB。

3、混淆大法好(減小 0.4 MB)

還記得 Android 混淆的魅力嗎?

  • 增長逆向難度;
  • 減小 Apk 大小;
  • 。。。

對此 Flutter 也爲咱們提供了混淆命令:

  • flutter build apk --obfuscate --split-debug-info=//

簡單說下我我的對於此命令的理解:

  • --obfuscate:開啓混淆操做;
  • --split-debug-info=:將因混淆生成的 map 符號表緩存到此位置。

這裏咱們先測試下,直接構建完整包,並添加混淆操做,輸出的 apk 大小有多少:

heliquan@Mac  ~/CodePro/FlutterPro/xxx_app   master ●  flutter build apk --obfuscate --split-debug-info=HLQ_Struggle
You are building a fat APK that includes binaries for android-arm, android-arm64, android-x64.
If you are deploying the app to the Play Store, it's recommended to use app bundles or split the APK to reduce the APK size. To generate an app bundle, run: flutter build appbundle --target-platform android-arm,android-arm64,android-x64 Learn more on: https://developer.android.com/guide/app-bundle To split the APKs per ABI, run: flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi Learn more on: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split Running Gradle task 'assembleRelease'... Running Gradle task 'assembleRelease'... Done 60.3s ✓ Built build/app/outputs/apk/release/app-release.apk (21.9MB). 複製代碼

一樣也在項目根目錄下生成了符號文件:

相比一開始的 23.8 MB,減小了 1.9 MB。那麼咱們直接針對不一樣 CPU 生成對應的 Apk 並添加混淆結果又是怎樣呢?

➜  xxx_app git:(master) ✗ flutter build apk --obfuscate --split-debug-info=debugInfo  --target-platform android-arm,android-arm64,android-x64 --split-per-abi                                       
Running Gradle task 'assembleRelease'...                                
Running Gradle task 'assembleRelease'... Done                      39.3s
✓ Built build/app/outputs/apk/release/app-armeabi-v7a-release.apk (9.4MB).
✓ Built build/app/outputs/apk/release/app-arm64-v8a-release.apk (9.7MB).
✓ Built build/app/outputs/apk/release/app-x86_64-release.apk (9.8MB).
複製代碼

未混淆的 v7a 大小與開啓混淆相比,開啓混淆減小了 0.4 MB。

還不錯。

對於混淆的文件,出問題怎麼調試呢?

莫慌,Flutter 一樣提供了 symbolize 神器,固然這個不在涉獵範圍內,就不詳細解釋了,知道就好:

heliquan@Mac  ~/CodePro/FlutterPro/haozhuan_app   master ●  flutter symbolize -h
Symbolize a stack trace from an AOT compiled flutter application.

Usage: flutter symbolize [arguments]
-h, --help                                                                     Print this usage information.
-d, --debug-info=</out/android/app.arm64.symbols>                              A path to the symbols file generated with "--split-debug-info".
-i, --input=</crashes/stack_trace.err>                                         A file path containing a Dart stack trace.
-o, --output=<A file path for a symbolicated stack trace to be written to.>    

Run "flutter help" to see global options.
複製代碼

End

上面叨叨半天,總結一個比較有用的命令:

  • flutter build apk --obfuscate --split-debug-info=HLQ_Struggle --target-platform android-arm,android-arm64,android-x64 --split-per-abi

含義就是,哥,幫我針對不一樣 CPU 架構分別打包,別忘記混淆哈,生成的符號表文件記得幫我放在 HLQ_Struggle 目錄下。

詳細日誌以下:

heliquan@Mac  ~/CodePro/FlutterPro/xxx_app   master ●   flutter build apk --obfuscate --split-debug-info=HLQ_Struggle --target-platform android-arm,android-arm64,android-x64 --split-per-abi                                          
Removed unused resources: Binary resource data reduced from 816KB to 814KB: Removed 0%
Removed unused resources: Binary resource data reduced from 816KB to 814KB: Removed 0%
Removed unused resources: Binary resource data reduced from 816KB to 814KB: Removed 0%
Running Gradle task 'assembleRelease'...                                
Running Gradle task 'assembleRelease'... Done                      36.9s
✓ Built build/app/outputs/apk/release/app-armeabi-v7a-release.apk (9.4MB).
✓ Built build/app/outputs/apk/release/app-arm64-v8a-release.apk (9.7MB).
✓ Built build/app/outputs/apk/release/app-x86_64-release.apk (9.8MB).
複製代碼

固然也有小夥伴說了,打包前 clean 下,生成的包會小,實際測試一下:

heliquan@Mac  ~/CodePro/FlutterPro/xxx_app   master ●  flutter clean           
Cleaning Xcode workspace...                                         3.3s
Deleting build...                                                2,774ms (!)
Deleting .dart_tool...                                              41ms
Deleting Generated.xcconfig...                                       0ms
Deleting flutter_export_environment.sh...                            0ms
Deleting App.framework...                                            9ms
 heliquan@Mac  ~/CodePro/FlutterPro/xxx_app   master ●   flutter build apk --obfuscate --split-debug-info=HLQ_Struggle --target-platform android-arm,android-arm64,android-x64 --split-per-abi                                          
Running Gradle task 'assembleRelease'...                                                                                                                                                  Removed unused resources: Binary resource data reduced from 816KB to 814KB: Removed 0%                             
Removed unused resources: Binary resource data reduced from 816KB to 814KB: Removed 0%                             
Removed unused resources: Binary resource data reduced from 816KB to 814KB: Removed 0%                             
Running Gradle task 'assembleRelease'...                                                                           
Running Gradle task 'assembleRelease'... Done                     215.3s (!)
✓ Built build/app/outputs/apk/release/app-armeabi-v7a-release.apk (9.4MB).
✓ Built build/app/outputs/apk/release/app-arm64-v8a-release.apk (9.7MB).
✓ Built build/app/outputs/apk/release/app-x86_64-release.apk (9.8MB).
複製代碼

根據以上輸出結果,並沒發現減小了哪兒。

一點小經歷分享,固然確定會有更好的操做方法,可是目前僅次於此,歡迎各位大佬交流~

Thanks

相關文章
相關標籤/搜索