美文网首页
检测无用的方法

检测无用的方法

作者: 人仙儿a | 来源:发表于2018-12-07 19:40 被阅读36次

1.所有的方法

通过buildSetting->LinkMap设为yes且设map路径生成文件。

2.真正使用的方法:

通过otool反编译出selrefs段,如:otool -v -s __DATA __objc_selrefs /Users/a58/Library/Developer/Xcode/DerivedData/bangjob-gwroieegkblzmfbtvtdfeztswqdc/Build/Products/Debug-iphoneos/bangjob.app/bangjob >> bang_sel生成文件。执行文件路径指的是app包里的macho文件。

3.无用的方法 = 所有方法 - 真正的方法

所有代码如下:

/*
 *  所有方法-真正的方法=无用的方法
 */
- (void)testMinus {
    NSMutableSet *set1 = [self testFile];
    NSMutableSet *set2 = [self testOtoolBangFile];
    NSLog(@"所有方法count:%ld, 真正使用的方法count:%ld", set1.count, set2.count);
    NSLog(@"正在相减...");
    [set1 minusSet:set2];
    NSLog(@"无用方法的count:%ld, 包含set方法", set1.count);

    NSMutableSet *containSetterSet = [NSMutableSet set];
    for (NSString *sel in set1) {
        if([sel hasPrefix:@"set"]) {
            [containSetterSet addObject:sel];
        }
    }
    
    NSLog(@"去除set方法 %ld个", containSetterSet.count);
    [set1 minusSet:containSetterSet];
    
    NSLog(@"最终无用方法的count:%ld个", set1.count);
    [[set1 allObjects] writeToFile:@"/Users/a58/noUse-sel" atomically:YES];
    NSLog(@"还是不行,包含getter方法!平时直接用_变量并不调getter方法;并且包含第三方的无用方法");
}

/*
 *  真正使用的方法。
 *  通过命令otool -v -s __DATA __objc_selrefs /Users/a58/Library/Developer/Xcode/DerivedData/bangjob-gwroieegkblzmfbtvtdfeztswqdc/Build/Products/Debug-iphoneos/bangjob.app/bangjob >> bang_sel 生成文件
 */
- (NSMutableSet *)testOtoolBangFile {
    
    NSError *error;
    NSString *content = [NSString stringWithContentsOfFile:@"/Users/a58/bang_sel" encoding:NSASCIIStringEncoding error:&error];
    NSString *patternStr = [NSString stringWithFormat:@"__objc_methname:([0-9a-zA-Z_:]+)\\n"];
    NSRegularExpression *regularexpression = [[NSRegularExpression alloc]
                                              initWithPattern:patternStr
                                              options:NSRegularExpressionCaseInsensitive
                                              error:nil];
    
    NSArray<NSTextCheckingResult *> *matches = [regularexpression matchesInString:content options:NSMatchingReportProgress range:NSMakeRange(0, content.length)];
    
    NSLog(@"真正使用的方法:before - count:%ld", matches.count);
    NSMutableSet *set = [NSMutableSet set];
    for (NSTextCheckingResult *result in matches) {
        NSRange range2 = [result rangeAtIndex:1];
        NSString *method = [content substringWithRange:range2];
        [set addObject:method];
    }
    NSLog(@"真正使用的方法:after - count:%ld", set.count);
    return set;
}

/*
 * 所有的方法。
 * 通过buildSetting->LinkMap设为yes且设map路径生成文件
 */
- (NSMutableSet *)testFile {
    NSError *error;
    NSString *content = [NSString stringWithContentsOfFile:@"/Users/a58/linkMap.txt" encoding:NSASCIIStringEncoding error:&error];
//    NSString *patternStr = [NSString stringWithFormat:@"([+-]\\[.+\\s(.+)\\])"];
    NSString *patternStr = [NSString stringWithFormat:@"[+-]\\[[0-9a-zA-Z_]+\\s([0-9a-zA-Z_:]+)\\]\\n"];
    NSRegularExpression *regularexpression = [[NSRegularExpression alloc]
                                              initWithPattern:patternStr
                                              options:NSRegularExpressionCaseInsensitive
                                              error:nil];
    
    NSArray<NSTextCheckingResult *> *matches = [regularexpression matchesInString:content options:NSMatchingReportProgress range:NSMakeRange(0, content.length)];
    
    NSLog(@"所有方法:before - count:%ld", matches.count);
    NSLog(@"正在去重...");
    NSMutableSet *set = [NSMutableSet set];
    for (NSTextCheckingResult *result in matches) {
        NSRange range2 = [result rangeAtIndex:1];
        NSString *method = [content substringWithRange:range2];
        [set addObject:method];
    }
    NSLog(@"所有方法 after - count:%ld", set.count);
    [[set allObjects] writeToFile:@"/Users/a58/link-sel" atomically:YES];
    return set;
}

打印的日志如下:

2018-12-07 19:28:41.909175+0800 TestMachO2[80125:1791939] 所有方法:before - count:53987
2018-12-07 19:28:41.909410+0800 TestMachO2[80125:1791939] 正在去重...
2018-12-07 19:28:41.971372+0800 TestMachO2[80125:1791939] 所有方法 after - count:27194
2018-12-07 19:28:42.014835+0800 TestMachO2[80125:1791939] 真正使用的方法:before - count:23796
2018-12-07 19:28:42.042791+0800 TestMachO2[80125:1791939] 真正使用的方法:after - count:23796
2018-12-07 19:28:42.042883+0800 TestMachO2[80125:1791939] 所有方法count:27194, 真正使用的方法count:23796
2018-12-07 19:28:42.042911+0800 TestMachO2[80125:1791939] 正在相减...
2018-12-07 19:28:42.053201+0800 TestMachO2[80125:1791939] 无用方法的count:6709, 包含set方法
2018-12-07 19:28:42.055813+0800 TestMachO2[80125:1791939] 去除set方法 2633个
2018-12-07 19:28:42.057631+0800 TestMachO2[80125:1791939] 最终无用方法的count:4076个
2018-12-07 19:28:42.062334+0800 TestMachO2[80125:1791939] 还是不行,包含getter方法!平时直接用_变量并不调getter方法;并且包含第三方的无用方法

结论

最终找出无用的方法4076个。
但是getter方法和第三方的库的方法混在其中,无法剔除,只能通过人工一个一个查看来进行筛选。所以还需要尝试其它更好的方式!!!

相关文章

  • 检测无用的方法

    1.所有的方法 通过buildSetting->LinkMap设为yes且设map路径生成文件。 2.真正使用的方...

  • Objective-C二进制瘦身

    先说结论:我写了个工具检测无用方法、无用类以及无用协议,只需要Mach-O文件,对Build Setting里的S...

  • ProGuard入门

    ProGuard入门 简单过程 shrinker:检测和移除无用的类、方法、变量和属性 optimizer:优化代...

  • 工作中注意

    1.多次点击的连续请求控制 2.边界值考虑 3.无用样式编写 4.方法中的非法值检测

  • 事半功倍——正则表达式由浅入深 及 iOS实战

    零. 前言 在前面写的三篇文章:iOS分类 同名方法自动检测脚本、iOS瘦身——移除无用资源的LSUnusedRe...

  • Matrix ApkChecker实际使用之APK瘦身

    本文基于Tencent Matrix ApkChecker做得无用资源检测及图片大小检测。对于ApkChecker...

  • App瘦身

    写文章注册登录 × 使用Swift3开发了个MacOS的程序可以检测出objc项目中无用方法,然后一键全部清理 星...

  • iOS检测项目中无用类和方法

    1.下载文章末尾的工具FindSelectorsUnrefs.py2.打开终端cd到FindSelectorsUn...

  • oc常用的一些第三方库

    1、工程中无用图片扫描: 使用了LSUnusedResources这款Mac App检测;2、内存泄漏检测:使用...

  • iOS无用类的检测

    1.先去github上面下载源码,地址: https://github.com/netyouli/WHC_Scan...

网友评论

      本文标题:检测无用的方法

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