在做iOS SDK的时候,有时候SDK是面向游戏开发者的,很多开发者并不会使用模拟器进行调试,所以我们提供的SDK文件,没必要带有模拟器架构代码,把这部分代码去除掉,最终提供的SDK包体会小很多,也不会收到CP对包体大小的吐槽~

python是一门很有意思的语言,上到Web应用下到小工具,都能开发,下面分享一段小工具代码,主要功能用来去除.a静态库模拟器架构代码!

 

#coding=utf-8

import sys
import os
import  subprocess
import time

def get_sdk_infos(sdk_path):
    cmd = "lipo -info %s" % sdk_path
    cmpsplit=cmd.split()
    output = subprocess.check_output(cmpsplit)
    result=set(output.split())
    architectures=[];
    thin_sdks=[];

    for d in result:
        if('armv7' in d or 'armv7s' in d or 'arm64' in d or 'x86_64' in d or 'i386' in d):
            architectures.append(d);
            output_path = create_arch_framework(sdk_path, d);
            if ('armv7' in d or 'armv7s' in d or 'arm64' in d):
                thin_sdks.append(output_path);
    str = '  '.join(thin_sdks)
    created_file_path=sdk_path
    cmd = u"lipo -create %s -output %s" % (str, created_file_path)
    cmpsplit = cmd.split()
    try:
        output = subprocess.check_output(cmpsplit)
    except subprocess.CalledProcessError, e:
        print '%s error:%s!' % (cmd, e)
    time.sleep(2)
    for file_temp in thin_sdks:
        if(os.path.exists(file_temp)):
            pass
    pass

def get_file_path(sdk_path,rename_name):
    root_path = os.path.dirname(sdk_path);
    output_sdk_path = ''
    if (os.path.isfile(sdk_path)):
        array_result= sdk_path.split('/');
        framework_path= array_result[len(array_result)-2];
        framework_sdk_path=array_result[len(array_result)-1];
        if '.a' in framework_sdk_path:
            temp_sdk_path = os.path.split(sdk_path)
            file_name = temp_sdk_path[1];
            file_name_ext = file_name.split('.');
            file_name = file_name_ext[0] + '_%s' % (rename_name);
            new_file_name = '%s/%s.%s' % (root_path, file_name, file_name_ext[1]);
            output_sdk_path = new_file_name;
        else:
            framework_root_path = os.path.dirname(sdk_path);
            output_sdk_path=os.path.join(framework_root_path,'%s_%s' %(framework_sdk_path,rename_name));
        pass
    return output_sdk_path;

def create_arch_framework(sdk_path,arch):
    output_sdk_path = get_file_path(sdk_path,arch);
    cmd = u"lipo %s -thin %s -output %s" %(sdk_path,arch,output_sdk_path)
    cmpsplit = cmd.split()
    try:
        output = subprocess.check_output(cmpsplit)
    except subprocess.CalledProcessError, e:
        print '%s error:%s!' %(cmd,e)
    pass
    return output_sdk_path;

if __name__ == '__main__':
    paras_len= len(sys.argv)
    # 检查执行命令是否有效,举例: python SDK_Thinning.py /Users/您的电脑用户名/Desktop/python/AMap_iOS_SDK_ALL/MAMapKit.framework/MAMapKit
    if paras_len>=1:
        source_file_path = sys.argv[1]
        # 获取源文件路径参数,源文件类型为.a或者.framework静态库文件
        print 'source_file_path:',source_file_path
        result_file_path = get_sdk_infos(source_file_path);
        print 'success!'
    else:
        print 'error!'
    

 下面就使用方式举例:

以高德地图iOS SDK为例,目标是去除AMap_iOS_2DMap_Lib库的模拟器代码,从而减少包体大小!

1.分析库代码的架构

2.保留armv7和arm64架构代码

3.下图红款文件就是我们需要的文件

4.最后我们来验证结果是否正确

 

版权声明:本文为YueHeiZS原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/YueHeiZS/p/9252186.html