Smali方法数计算及思路分享

前言

去年毕业了,来到新公司。没多久就遇到了方法数(偶尔)计算错误,导致打包失败。因为业务原因,这边的打包脚本是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 程良明
 * @date 2024/3/11
 * * * 说明:计算smali文件的最大方法数
 **/
public class SmaliFileMethodHelper {
    /**
     * 是否启用严苛模式
     */
    private final boolean IS_HARSH = false;

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

    public static void main(String[] args) {
        String classPath = "E:\\Downloaded\\文件\\mubao\\smali_classes3";
        int methodCount = SmaliFileMethodHelper.getInstance().getMethodCount(classPath);
        System.out.println("clm count : " + methodCount);
    }

    public int getMethodCount(String path) {

        getMethodCount(new File(path));
        int methodCount = mMethodCount.size();
        int fieldCount = mFieldCount.size();
        System.out.println("methodCount: " + methodCount);
        System.out.println("fieldCount: " + fieldCount);
//      System.out.println("fieldCount: " + mFieldCount);
        if (methodCount > fieldCount) {
            return methodCount;
        }
        return fieldCount;
    }

    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);
        if (file.getName().toLowerCase().contains("field")){
            System.out.println("clm: "+ file.getName());
        }
    }

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

    private void processMethodCount(String content) {
        String className = getClassByFile(content);
        String regex = "\\.method\\s.+|invoke-.*->.*";
        if (IS_HARSH) {
            regex = regex + "|value\\s=.*->.*|(iget|iput).*;->.*";
        }

        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);
                if (IS_HARSH) {
                    if (line.contains("value =") && line.contains(".enum")) {
                        continue;
                    }
                    if (line.startsWith("iget") | line.startsWith("iput")) {
//                  严苛模式下可能需要计算这些值
//                  计算这个,可能有误差,  特殊情况下需要这个,
                        int index1 = targetStr.indexOf(":") + 1;
                        int index2 = targetStr.lastIndexOf("$");
                        String type;
                        if (index2 > index1) {
                            type = targetStr.substring(index1, index2);
                        } else {
                            type = targetStr.substring(index1);
                        }
                        targetStr = type.replace("[", "").replace(";", "");
                    }
                }

            }
            mMethodCount.add(targetStr.trim());
        }


        processFieldCount(content);
    }


    private void processFieldCount(String content) {
        String className = getClassByFile(content);
        String regex = "(iget|iput|sget|sput).*|\\.field.*";
        if (IS_HARSH) {
//          regex = regex + "|value\\s=.*->.*|(iget|iput).*;->.*";
        }

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            String line = matcher.group();
            String targetStr = line;

            if (line.contains(" = ")) {
                targetStr = line.split(" = ")[0];
            }
            if (!targetStr.contains("->")) {
                targetStr = makeMethodInvoke(targetStr, className);

            } else {
                targetStr = targetStr.substring(targetStr.lastIndexOf(" ")).trim();
            }

            targetStr = targetStr.trim();

            mFieldCount.add(targetStr);


        }
    }

    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 makeMethodAnnotationInvoke(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,方法数超的问题了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。