Android 获取apk中的所有类

直接把下面代码放到工具类调用即可.


import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.duole.games.sdk.core.utils.PlatformLog;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import dalvik.system.BaseDexClassLoader;
import dalvik.system.DexFile;

public class AntiPlugging {

    private static BaseDexClassLoader mClassLoader = (BaseDexClassLoader) Thread.currentThread().getContextClassLoader();

    public static void checkApp(Context context) {
        if (mClassLoader == null) {
            mClassLoader = (BaseDexClassLoader) context.getClassLoader();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                List list = getAllClassesName();
                if (list != null && !list.isEmpty()) {
                    for (int i = 0; i < list.size(); i++) {
                        String name = list.get(i);
                    }
                }
            }
        }).start();
    }

    private static List getAllClassesName() {
        List classNames = new ArrayList<>();
        try {
            Field f_pathList = getField("pathList", dalvik.system.BaseDexClassLoader.class);
            Field f_dexElements = getField("dexElements", getClass("dalvik.system.DexPathList"));
            Field f_dexFile = getField("dexFile", getClass("dalvik.system.DexPathList$Element"));

            Object pathList = getObjectFromField(f_pathList, mClassLoader);
            Object[] list = (Object[]) getObjectFromField(f_dexElements, pathList);

            if (list == null) {
                return classNames;
            }
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                //DexFile在API26版本被弃用
                for (int i = 0; i < list.length; i++) {
                    DexFile file = (DexFile) getObjectFromField(f_dexFile, list[i]);
                    if (file != null) {
                        Enumeration entries = file.entries();
                        while (entries != null && entries.hasMoreElements()) {
                            String className = entries.nextElement();
                            classNames.add(className);
                        }
                    }
                }
            } else {
                Class dexFile = getClass("dalvik.system.DexFile");
                if (dexFile != null) {
                    Field f_mCookie = getField("mCookie", dexFile);
                    if (f_mCookie != null) {
                        Method classNameList = getMethod("getClassNameList", dexFile, Object.class);
                        if (classNameList != null) {
                            for (int i = 0; i < list.length; i++) {
                                Object file = getObjectFromField(f_dexFile, list[i]);
                                Object o_mCookie = getObjectFromField(f_mCookie, file);
                                if (o_mCookie == null) {
                                    continue;
                                }
                                String[] classList = (String[]) classNameList.invoke(file, o_mCookie);
                                if (classList != null) {
                                    Collections.addAll(classNames, classList);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
        }
        return classNames;
    }

    private static Field getField(String field, Class Class) throws NoSuchFieldException {
        if (field != null && Class != null) {
            return Class.getDeclaredField(field);
        }
        return null;
    }

    private static Method getMethod(String method, Class Class, Class... parameterTypes) throws NoSuchMethodException {
        if (Class != null && parameterTypes != null) {
            Method res = Class.getDeclaredMethod(method, parameterTypes);
            res.setAccessible(true);
            return res;
        }
        return null;
    }

    private static Class getClass(String className) throws ClassNotFoundException {
        return mClassLoader.loadClass(className);
    }

    private static Object getObjectFromField(Field field, Object arg) throws IllegalAccessException {
        if (field != null && arg != null) {
            field.setAccessible(true);
            return field.get(arg);
        }
        return null;
    }
}

你可能感兴趣的:(android,java,开发语言)