查找iOS上每个外部库贡献的大小 [英] Find size contributed by each external library on iOS

查看:61
本文介绍了查找iOS上每个外部库贡献的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试减小我的应用商店二进制文件的大小,并且我们有很多外部库可能会影响最终ipa的大小.有没有办法找出每个外部静态库在最终二进制文件中所占用的空间(除了删除每个静态库之外?)?

I'm trying to reduce my app store binary size and we have lots of external libs that might be contributing to the size of the final ipa. Is there any way to find out how much each external static lib takes up in the final binary (Other than going about removing each one ?) ?

推荐答案

所有这些信息都包含在链接映射中,如果您有耐心进行筛选(对于大型应用程序,它可能会很大).链接映射以人类可读文本的形式列出了所有库,它们的目标文件以及打包到您的应用程序中的所有符号.通常,默认情况下,没有将项目配置为生成项目,因此您必须快速更改项目文件.

All of this information is contained in the link map, if you have the patience for sifting through it (for large apps, it can be quite large). The link map has a listing of all the libraries, their object files, and all symbols that were packaged into your app, all in human-readable text. Normally, projects aren't configured to generate them by default, so you'll have to make a quick project file change.

在Xcode中:

  1. 在目标的构建设置"下,搜索地图"
  2. 在下面的结果中,在链接"部分下,将写链接映射文件"设置为是"
  3. 确保记下链接映射文件的路径"下列出的完整路径和文件名

下次创建应用程序时,将获得一个链接映射转储到该文件路径.请注意,该路径是相对于您的应用程序在DerivedData文件夹中的位置的(通常为~/Library/Developer/Xcode/DerivedData/<your-app-name>-<random-string-of-letters-and-numbers>/Build/Intermediates/...,但为YMMV).由于它只是一个文本文件,因此可以使用任何文本编辑器进行读取.

The next time you build your app you'll get a link map dumped to that file path. Note that the path is relative to your app's location in the DerivedData folder (usually ~/Library/Developer/Xcode/DerivedData/<your-app-name>-<random-string-of-letters-and-numbers>/Build/Intermediates/..., but YMMV). Since it's just a text file, you can read it with any text editor.

链接映射的内容分为3个部分,其中2个与您要查找的内容有关:

The contents of the link map are divided into 3 sections, of which 2 will be relevant to what you're looking for:

  1. 目标文件:此部分包含最终应用程序中包含的所有目标文件的列表,包括您自己的代码以及所包含的任何第三方库的代码.重要的是,每个目标文件还列出了其来源的库;
  2. 部分:本部分与您的问题无关,包含处理器部分及其部分的列表;
  3. 符号:此部分包含您感兴趣的原始数据:所有符号/方法的列表以及它们的绝对位置(即,处理器内存映射中的地址),大小,最重要的是,对其包含的对象模块的引用(在文件"列下).

从此原始数据中,您可以获得进行所需大小计算所需的一切.从#1可以看到,对于每个库,都有N个可能的组成对象模块;每个模块都有一个.从#2中可以看到,对于每个对象模块,都有M个可能的符号,每个符号的大小为S.对于任何给定的库,大小的大致顺序将类似于O(N * M * S).这仅是为了指示您将要在实际计算中使用的组件,而并不是任何有用的公式.要自己执行计算,很遗憾地说,我不知道任何现有工具会为您进行必要的处理,但是鉴于链接映射只是一个文本文件,并且具有一点脚本魔术和独创性您可以构建一个脚本来完成繁重的工作.

From this raw data, you have everything you need to do the required size calculation. From #1, you see that, for every library, there are N possible constituent object modules; from #2, you see that, for every object module, there are M possible symbols, each occupying size S. For any given library, then, your rough order of size will be something like O(N * M * S). That's only to give you an indication of the components that would go into your actual calculations, it's not any sort of a useful formula. To perform the calculation itself, I'm sorry to say that I'm not aware of any existing tools that will do the requisite processing for you, but given that the link map is just a text file, with a little script magic and ingenuity you can construct a script to do the heavy lifting.

例如,我有一个示例项目链接到以下库: https://github.com/ColinEberhardt/LinqToObjectiveC(示例项目本身来自有关ReactiveCocoa的不错的教程,请参见: http://www.raywenderlich.com /62699/reactivecocoa-tutorial-pt1 ),我想知道它占用了多少空间.我已经生成了一个链接映射TwitterInstant-LinkMap-normal-x86_64.txt(它在模拟器中运行).为了找到该库包含的所有对象模块,我这样做:

For example, I have a little sample project that links to the following library: https://github.com/ColinEberhardt/LinqToObjectiveC (the sample project itself is from a nice tutorial on ReactiveCocoa, here: http://www.raywenderlich.com/62699/reactivecocoa-tutorial-pt1), and I want to know how much space it occupies. I've generated a link map, TwitterInstant-LinkMap-normal-x86_64.txt (it runs in the simulator). In order to find all object modules included by the library, I do this:

$ grep -i "libLinqToObjectiveC.a" TwitterInstant-LinkMap-normal-x86_64.txt

这给了我这个

[  8] /Users/Smyrl/Library/Developer/Xcode/DerivedData/TwitterInstant-ecppmzhbawtxkwctokwryodvgkur/Build/Products/Debug-iphonesimulator/libLinqToObjectiveC.a(LinqToObjectiveC-dummy.o)
[  9] /Users/Smyrl/Library/Developer/Xcode/DerivedData/TwitterInstant-ecppmzhbawtxkwctokwryodvgkur/Build/Products/Debug-iphonesimulator/libLinqToObjectiveC.a(NSArray+LinqExtensions.o)
[ 10] /Users/Smyrl/Library/Developer/Xcode/DerivedData/TwitterInstant-ecppmzhbawtxkwctokwryodvgkur/Build/Products/Debug-iphonesimulator/libLinqToObjectiveC.a(NSDictionary+LinqExtensions.o)

第一列包含我需要的符号表的交叉引用,因此我可以搜索这些符号:

The first column contains the cross-references to the symbol table that I need, so I can search for those:

$ cat TwitterInstant-LinkMap-normal-x86_64.txt | grep -e "\[  8\]"

这给了我

0x100087161 0x0000001B  [  8] literal string: PodsDummy_LinqToObjectiveC
0x1000920B8 0x00000008  [  8] anon
0x100093658 0x00000048  [  8] l_OBJC_METACLASS_RO_$_PodsDummy_LinqToObjectiveC
0x1000936A0 0x00000048  [  8] l_OBJC_CLASS_RO_$_PodsDummy_LinqToObjectiveC
0x10009F0A8 0x00000028  [  8] _OBJC_METACLASS_$_PodsDummy_LinqToObjectiveC
0x10009F0D0 0x00000028  [  8] _OBJC_CLASS_$_PodsDummy_LinqToObjectiveC

第二列包含所讨论符号的大小(以十六进制表示),因此,如果将它们全部加起来,将得到0x103或259个字节.

The second column contains the size of the symbol in question (in hexadecimal), so if I add them all up, I get 0x103, or 259 bytes.

更好的是,我可以做一些流黑客,将其缩减为基本要素,并为我做些补充:

Even better, I can do a bit of stream hacking to whittle it down to the essential elements and do the addition for me:

$ cat TwitterInstant-LinkMap-normal-x86_64.txt | grep -e "\[  8\]" | grep -e "0x" | awk '{print $2}' | xargs printf "%d\n" | paste -sd+ - | bc

这使我的数字直线上升:

which gives me the number straight up:

259

"\[ 9\]"(13016字节)和"\[ 10\]"(5503字节)进行相同的操作,然后将它们添加到前面的259个字节中,我得到18778个字节.

Doing the same for "\[ 9\]" (13016 bytes) and "\[ 10\]" (5503 bytes), and adding them to the previous 259 bytes, gives me 18778 bytes.

您当然可以改善我在此处所做的流黑客攻击,以使其更加健壮(在此实现中,必须确保正确获得正确的空格数并用括号括起来),但是您在最少知道这个主意.

You can certainly improve upon the stream hacking I've done here to make it a bit more robust (in this implementation, you have to make sure you get the exact number of spaces right and quote the brackets), but you at least get the idea.

这篇关于查找iOS上每个外部库贡献的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆