iOS代码减肥-删去无用办法

一、背景

内部CICD平台在做APP产物剖析,有一项是无用类和无用办法的产出,本篇主要从代码层面通过删去无用办法做一些优化

二、方案整理

通过otool剖析mach-o文件

三、实践

环境

  • otool
  • python3以上版本

留意

本篇文章主要是针对OC工程扫描,如果是swift混编工程会有问题

流程图

iOS代码减肥-删去无用办法

具体步骤

第一步:获取一切协议办法列表

# 获取protocol中一切的办法
def header_protocol_selectors(file_path):
    # 删去途径前后的空格
    file_path = file_path.strip()
    if not os.path.isfile(file_path):
        return None
    protocol_sels = set()
    file = open(file_path, 'r', encoding='unicode_escape')
    is_protocol_area = False
    # 开始遍历文件内容
#    .decode('UTF8')
    for line in file.readlines():
        # 删去注释信息
        # delete description
#        print(line)
#        print("***********")
        line = re.sub('".*"', '', line)
        # delete annotation
        line = re.sub('//.*', '', line)
        # 检测是否是 @protocol
        # match @protocol
        if re.compile('s*@protocols*w+').findall(line):
            is_protocol_area = True
        # match @end
        if re.compile('s*@end').findall(line):
            is_protocol_area = False
        # match sel
        if is_protocol_area and re.compile('s*[-|+]s*(').findall(line):
            sel_content_match_result = None
            # - (CGPoint)convertPoint:(CGPoint)point toCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace
            if ':' in line:
                # match sel with parameters
                # 【"convertPoint:","toCoordinateSpace:"]
                sel_content_match_result = re.compile('w+s*:').findall(line)
            else:
                # - (void)invalidate;
                # match sel without parameters
                # invalidate;
                sel_content_match_result = re.compile('w+s*;').findall(line)
            if sel_content_match_result:
                # 办法参数拼接
                # convertPoint:toCoordinateSpace:
                funcList = ''.join(sel_content_match_result).replace(';', '')
                protocol_sels.add(funcList)
    file.close()
    return protocol_sels
# 获取一切protocol定义的办法
def protocol_selectors(path, project_path):
    print('获取一切的protocol中的办法...')
    header_files = set()
    protocol_sels = set()
    # 获取当时引证的系统库中的办法列表
    system_base_dir = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk'
    # get system librareis
    lines = os.popen('otool -L ' + path).readlines()
    for line in lines:
        # 去除首尾空格
        line = line.strip()
        # /System/Library/Frameworks/MediaPlayer.framework/MediaPlayer (compatibility version 1.0.0, current version 1.0.0)
        # /System/Library/Frameworks/MediaPlayer.framework/MediaPlayer
        # delete description,
        line = re.sub('(.*)', '', line).strip()
        if line.startswith('/System/Library/'):
            # [0:-1],获取数组的左起第一个,到倒数最终一个,不包含最终一个,[1,-1)左闭右开
            library_dir = system_base_dir + '/'.join(line.split('/')[0:-1])
            if os.path.isdir(library_dir):
                # 获取当时系统架构中一切的类
                # 获取合集
                header_files = header_files.union(os.popen('find %s -name "*.h"' % library_dir).readlines())
    if not os.path.isdir(project_path):
        exit('Error: project path error')
    # 获取当时途径下面一切的.h文件途径
    header_files = header_files.union(os.popen('find %s -name "*.h"' % project_path).readlines())
    for header_path in header_files:
        # 获取一切查找到的文件下面的protocol办法,这些办法,不能用来统计
        header_protocol_sels = header_protocol_selectors(header_path)
        if header_protocol_sels:
            protocol_sels = protocol_sels.union(header_protocol_sels)
    return protocol_sels

第二步:从一切协议办法列表里删去射中黑名单的办法

例如:load、c++构造不要扫描

    def ignore_selectors(sel):
        if sel == '.cxx_destruct':
            return True
        if sel == 'load':
            return True
        return False

第三步:获取项目一切的办法列表

def imp_selectors(path):
    print('获取一切的办法,除了setter and getter办法...')
    # return struct: {'setupHeaderShadowView':['-[TTBaseViewController setupHeaderShadowView]']}
    #  imp     0x100001260 -[AppDelegate setWindow:] ==>> -[AppDelegate setWindow:],setWindow:
    re_sel_imp = re.compile('s*imps*0xw+ ([+|-][.+s(.+)])')
    re_properties_start = re.compile('s*baseProperties 0xw{9}')
    re_properties_end = re.compile('w{16} 0xw{9} _OBJC_CLASS_$_(.+)')
    re_property = re.compile('s*names*0xw+ (.+)')
    imp_sels = {}
    is_properties_area = False
    # “otool - ov”将输出Objective - C类结构及其定义的办法。
    for line in os.popen('/usr/bin/otool -oV %s' % path).xreadlines():
        results = re_sel_imp.findall(line)
        if results:
            #  imp     0x100001260 -[AppDelegate setWindow:] ==>> [-[AppDelegate setWindow:],setWindow:]
            (class_sel, sel) = results[0]
            if sel in imp_sels:
                imp_sels[sel].add(class_sel)
            else:
                imp_sels[sel] = set([class_sel])
        else:
            # delete setter and getter methods as ivar assignment will not trigger them
            # 删去相关的set办法
            if re_properties_start.findall(line):
                is_properties_area = True
            if re_properties_end.findall(line):
                is_properties_area = False
            if is_properties_area:
                property_result = re_property.findall(line)
                if property_result:
                    property_name = property_result[0]
                    if property_name and property_name in imp_sels:
                        # properties layout in mach-o is after func imp
                        imp_sels.pop(property_name)
                        # 拼接set办法
                        setter = 'set' + property_name[0].upper() + property_name[1:] + ':'
                        # 干掉set办法
                        if setter in imp_sels:
                            imp_sels.pop(setter)
    return imp_sels

第四步:获取一切被引证的办法列表

def ref_selectors(path):
    print('获取一切被调用的办法...')
    re_selrefs = re.compile('__TEXT:__objc_methname:(.+)')
    ref_sels = set()
    lines = os.popen('/usr/bin/otool -v -s __DATA __objc_selrefs %s' % path).readlines()
    for line in lines:
        results = re_selrefs.findall(line)
        if results:
            ref_sels.add(results[0])
    return ref_sels

第五步:获取未使用办法列表

unref_sels = set()
for sel in imp_sels:
        # 如果当时的办法不在protocol中,也不再引证的办法中,那么以为这个办法没有被用到
        # protocol sels will not apppear in selrefs section
        if sel not in ref_sels and sel not in protocol_sels:
            unref_sels = unref_sels.union(filter_selectors(imp_sels[sel]))

效果

优化前 优化后
380MB 376MB
由于平常我们比较注重,所以优化效果不明显

相关链接

脚本和demo链接