美文网首页
Smali方法数计算及思路分享

Smali方法数计算及思路分享

作者: 程某_Fran | 来源:发表于2024-03-11 11:31 被阅读0次

前言

去年毕业了,来到新公司。没多久就遇到了方法数(偶尔)计算错误,导致打包失败。因为业务原因,这边的打包脚本是php写的,所以我是没有打包机权限的。每当出现问题,排查都需要找php协助,在梳理了这边的打包流程后,直接采用了新方法,不再需要计算方法数。在梳理过程中也了解到,这边的计算方法数是参考37在网上公开的,在某些情况下计算是可信的,在此基础上,结合业务,带来准确性99%(也许)的计算方式。

使用场景

  1. 二次打包需要准确的计算方法数

使用说明

原理跟思想就不展开说了。直接贴上代码

package com.fran.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Fran
 * @date 2024/3/11
 * * * 说明:计算smali文件的最大方法数
 **/
public class SmaliFileMethodHelper {

    public static final SmaliFileMethodHelper getInstance() {
        return InnerHolder.sInstance;
    }

    public static void main(String[] args) {
        String classPath = "E:\\www\\hw\\10007-240307120819429111809\\smali";
        Set<String> methodCount = SmaliFileMethodHelper.getInstance().getMethodCount(classPath);
        System.out.println("clm count : " + methodCount);
        System.out.println("clm count : " + methodCount.size());
    }

    public Set<String> getMethodCount(String path) {
        return getMethodCount(new File(path));
    }

    public Set<String> getMethodCount(File file) {
        getMethodCountByDir(file);
        return mMethodCount;
    }

    public void getMethodCountByDir(File file) {
        if (file.isDirectory()) {
            File[] listFile = file.listFiles();
            if (listFile != null) {
                for (File tempFile : listFile) {
                    getMethodCountByDir(tempFile);
                }
            }
        } else {
            getMethodCountByFile(file);
        }
    }

    private void getMethodCountByFile(File file) {
        if (!file.isFile()) {
            return;
        }
        String content = readFileFromLine(file);
        processMethodCount(content);
    }

    private Set<String> mMethodCount = new HashSet<>();

    private void processMethodCount(String content) {
        String className = getClassByFile(content);
        String regex = "\\.method\\s.+|invoke-.*->.*";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            String line = matcher.group();
            String targetStr;
            if (line.startsWith(".method")) {
                targetStr = makeMethodInvoke(line, className);
            } else {
                targetStr = parseMethodInvoke(line);
            }
            mMethodCount.add(targetStr.trim());
        }
    }

    private String parseMethodInvoke(String line) {
        return line.substring(line.lastIndexOf(" "));
    }

    private String makeMethodInvoke(String line, String className) {
        return className + "->" + line.substring(line.lastIndexOf(" ")).trim();
    }

    private String readFileFromLine(File file) {
        StringBuilder builder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line).append("\r");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }

    private String getClassByFile(String content) {
        String className = "";
        String regex = "\\.class.+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            String line = matcher.group();
            className = line.substring(line.indexOf(" L"));
        }
        return className;
    }

    private SmaliFileMethodHelper() {

    }

    private static class InnerHolder {
        private static SmaliFileMethodHelper sInstance = new SmaliFileMethodHelper();
    }
}

题外话

在研究的过程中,也理解了之前的匹配规则为什么会有误差(主要是匹配逻辑,有可能会遇到string匹配上字符串,又或者有部分特点没匹配上(如下图)等)

# annotations
.annotation system Ldalvik/annotation/EnclosingMethod;
    value = Lcom/mbridge/msdk/foundation/webview/WebViewFragment;->onCreateView(Landroid/view/LayoutInflater;Landroid/view/ViewGroup;Landroid/os/Bundle;)Landroid/view/View;
.end annotation

为什么现在才写这个方法数

历史原因,因为在之前的公司,我都有一套根据回编的错误,来递归减少方法数阈值的逻辑。基本上计算不准确也不会有太大的问题,因为都是脚本自动执行的。

扩展的思路

因为现在我这边的不再兼容21之前的Android版本了,并且在这边的打包逻辑上,走的也是apk合并的方式。所以当时遇到计算方法不准后,想到解决思路就是不需要计算方法(对,你没看错)。不需要计算方法的原理也很简单,从虚拟机的加载逻辑可以看出,是优先加载classes.dex,classes2.dex,...对应的就是smali,smali_classes2,...。所以其实我们只需要把更新的smali资源,默认优先加载就可以了(保险起见我这边是把旧资源删除掉的,理论上应该不删除也可以)。即根据更新资源的apk的smali个数,把母包解压后的smali文件重命名就好了。

最后

希望大家都不会再遇到打包65535,方法数超的问题了

相关文章

网友评论

      本文标题:Smali方法数计算及思路分享

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