Flutter 解决iOS IPA过大问题

1,115 阅读2分钟

问题

从某个版本Flutter以后,我们发现flutter build ios后的产生的XX.app居然达到了惊人的400多兆。

然后尝试分析一下为什么会有如此之大:

flutter build ios --analyze-size

然后发现Frameworks居然有380多兆,再一进到里面一看是因为Flutter的文件导致的。

后来搜索了一下发现是因为:

ios的Flutter二进制文件增加了对bitcode的支持,从而导致体积增大。

具体原因是:

If we included variants with and without bitcode for iOS, 
that would even further complicate our variety of binary formats that we produce, 
leading to more confusion for beginner developers, and larger release archives. 
We currently have binaries for (static, dynamic, swift 2.2, swift 2.3, swift 3.0, swift 3.0.1, swift 3.0.2), 
all of which could have both variants with/without bitcode.

其实类似问题在其他库中也存在,比如Realm。

如果要上架苹果商店,系统会自动剔除这些代码,从而达到瘦身效果,也就是说用户最终下载到的安装文件并不会有4000多兆,所以这种情况下,体积变大无非就是浪费点上传时间,无可厚非。 但是如果我们想要内部测试怎么办,或者你要将你的xx.app拿给第三方签名怎么办,你要知道微信现在最大只让传200M的文件哦。

所以,这时我们可以使用xcrun bitcode_strip命令来剔除bitcode代码。

比如我们打出来的包是Runner.app,右键显示包内容,依次进入Frameworks-Flutter.framework文件夹(我更喜欢用terminal,hiahia)。

然后用终端cd到Flutter.framework目录下,运行命令移除bitcode代码:

xcrun bitcode_strip Flutter -r -o Flutter

再查看一下,Runner.app是不是小了许多呢?

补充知识

贴一下xcrun bitcode_strip文档:

       -o    output
              specifies the output file as output.

       -r     specifies the bitcode segment is to be removed.

       -m     specifies  the  bitcode segment is to be removed and a marker is
              left.

       -l     specifies   that   only   the   bitcode    segment    and    the
              (__TEXT,__info_plist)  section is to be left in the Mach-O file.

最后

欢迎公众微信公众号:OpenFlutter