美文网首页
iOS ipa减包

iOS ipa减包

作者: th先生 | 来源:发表于2021-01-26 16:35 被阅读0次

    最近开启了一波APP瘦身计划,一通操作,感觉学习了不少东西,分享一下。
    首先对同行的ipa包进行了一下分析对比,发现这么一个有趣的现象:
    竞品A ipa包108M, appstore下载大小108M
    竞品B ipa包128M, appstore下载大小103M
    自己 ipa包146M, appstore下载大小82M

    原来这个跟ipa包大小没有直接必然的联系。

    接下来就是该干点什么了,拆开他们的ipa,显示包内容进行分析:我发现竞品A的图片,基本上没有放到Assets中,而是直接放到了项目文件夹里。随后我将自己的图片也按照这种做法,重新打包,发现ipa大小没啥变化。

    随后又发现Build Settings中有一个Compress PNG Files,将其设置为NO,打包后发现ipa大小不变。

    iOS 对Assets中的图片进行了优化,会帮助开发者减小安装包大小。这一点具体是如何实现,我也没查到。
    查看我的项目中的Assets原图大小为37M,随后解压ipa包内容中.car文件,得到解压后的图片,发现为55M,图片被放大了,对比了原图与解压后的图,有的放大了200k,有的不变,有的缩小了几k。这里可以参考:
    https://cloud.tencent.com/developer/article/1368027
    https://www.cnblogs.com/lesten/p/14253878.html

    一般我们开发都是导入.png类型的图片,但是png会比jpg大一些,苹果也是建议我们使用png图片,原因自己百度一下吧。如果你把jpg拖进Assets中,那么苹果会自动给你转成png,从解压的ipa包的.car就可以看出来。

    那么解压.car,获取里面的图片又是怎么做呢?
    请移步到github,search cartool就好了。
    运行可能会报错:

        
    //  CUIThemeFacet *facet = [CUIThemeFacet themeWithContentsOfURL:[NSURL fileURLWithPath:carPath] error:&error];
    //
    //  CUICatalog *catalog = [[CUICatalog alloc] init];
    //
    //  /* Override CUICatalog to point to a file rather than a bundle */
    //  [catalog setValue:facet forKey:@"_storageRef"];
    //
        
        CUICatalog *catalog = nil;
        if ([CUICatalog instancesRespondToSelector:@selector(initWithURL:error:)]) {
            /* If CUICatalog has the URL API (Mojave), use it. */
            catalog = [[CUICatalog alloc] initWithURL:[NSURL fileURLWithPath:carPath] error:&error];
        } else {
            CUIThemeFacet *facet = [CUIThemeFacet themeWithContentsOfURL:[NSURL fileURLWithPath:carPath] error:&error];
            catalog = [[CUICatalog alloc] init];
            /* Override CUICatalog to point to a file rather than a bundle */
            [catalog setValue:facet forKey:@"_storageRef"];
        }
        NSCAssert(!error, @"Error attempting to open asset catalog (%@): %@", carPath, error);
        
        /* CUICommonAssetStorage won't link */
        CUICommonAssetStorage *storage = [[NSClassFromString(@"CUICommonAssetStorage") alloc] initWithPath:carPath];
    

    参考上面的代码进行修改。
    运行的时候,先设置解压目录,
    选择Products下面的cartool,然后选择EditScheme

    WechatIMG13.png
    Run模式下选择Arguments分别添加.car目录和解压后的目录。
    WechatIMG14.png

    说了半天该怎么压缩安装包呢?
    1、删除项目中没用的图片,参考:https://www.jianshu.com/p/5d9c5d03d462
    2、删除项目中没用的类,参考:https://www.jianshu.com/p/de03ea15f399?utm_source=desktop&utm_medium=timeline
    也可以利用AppCode 检测未使用的代码:菜单栏 ->Code->InspectCode
    3、压缩项目中的图片,大图超过100k的可以转成webp格式加载,但是加载速度比png慢,效率低。加载参考:https://www.jianshu.com/p/74fab9c7de77
    4、使用imageOptim 或者 TinyPng 进行图片压缩。但是有时压缩后效果并不明显。
    参考:https://imageoptim.com/xcode.html
    5、Build Settings->Optimization Level Debug模式默认为None[-O0],Release模式默认为Fastest, Smallest[-Os] 参考:https://blog.csdn.net/x32sky/article/details/90665787
    6、Build Settings->VALID_ARCHS 设置:arm64 arm64e 参考:https://www.cnblogs.com/lulushen/p/8135269.html

    相关文章

      网友评论

          本文标题:iOS ipa减包

          本文链接:https://www.haomeiwen.com/subject/iuheaktx.html