Android ClassLoader源码分析

本文详细分析了Android的BaseDexClassLoader及其内部的DexPathList,探讨了ClassLoader如何加载Dex文件,重点介绍了dexElements数组在加载过程中的作用。
摘要由CSDN通过智能技术生成

转载请注明链接:https://blog.csdn.net/feather_wch/article/details/88428618

Android ClassLoader源码分析

版本: 2019-03-12


BaseDexClassLoader

1、BaseDexClassLoader内部重要概念总结

  1. DexPathList pathList: 内部有一个dexElements数组,存储着该ClassLoader的所有dex/resources的路径

1、BaseDexClassLoader源码

package dalvik.system;

/**
 * Base class for common functionality between various dex-based
 * {@link ClassLoader} implementations.
 */
public class BaseDexClassLoader extends ClassLoader {
   

    /**
     * Hook for customizing how dex files loads are reported.
     *
     * This enables the framework to monitor the use of dex files. The
     * goal is to simplify the mechanism for optimizing foreign dex files and
     * enable further optimizations of secondary dex files.
     *
     * The reporting happens only when new instances of BaseDexClassLoader
     * are constructed and will be active only after this field is set with
     * {@link BaseDexClassLoader#setReporter}.
     */
    /* @NonNull */ private static volatile Reporter reporter = null;

    private final DexPathList pathList;

    /**
     * Constructs an instance.
     * Note that all the *.jar and *.apk files from {@code dexPath} might be
     * first extracted in-memory before the code is loaded. This can be avoided
     * by passing raw dex files (*.dex) in the {@code dexPath}.
     *
     * @param dexPath the list of jar/apk files containing classes and
     * resources, delimited by {@code File.pathSeparator}, which
     * defaults to {@code ":"} on Android.
     * @param optimizedDirectory this parameter is deprecated and has no effect since API level 26.
     * @param librarySearchPath the list of directories containing native
     * libraries, delimited by {@code File.pathSeparator}; may be
     * {@code null}
     * @param parent the parent class loader
     */
    public BaseDexClassLoader(String dexPath, File optimizedDirectory,
            String librarySearchPath, ClassLoader parent) {
   
        this(dexPath, optimizedDirectory, librarySearchPath, parent, false);
    }

    /**
     * @hide
     */
    public BaseDexClassLoader(String dexPath, File optimizedDirectory,
            String librarySearchPath, ClassLoader parent, boolean isTrusted) {
   
        super(parent);
        this.pathList = new DexPathList(this, dexPath, librarySearchPath, null, isTrusted);

        if (reporter != null) {
   
            reportClassLoaderChain();
        }
    }

    /**
     * Reports the current class loader chain to the registered {@code reporter}.
     * The chain is reported only if all its elements are {@code BaseDexClassLoader}.
     */
    private void reportClassLoaderChain() {
   
        ArrayList<BaseDexClassLoader> classLoadersChain = new ArrayList<>();
        ArrayList<String> classPaths = new ArrayList<>();

        classLoadersChain.add(this);
        classPaths.add(String.join(File.pathSeparator, pathList.getDexPaths()));

        boolean onlySawSupportedClassLoaders = true;
        ClassLoader bootClassLoader = ClassLoader.getSystemClassLoader().getParent();
        ClassLoader current = getParent();

        while (current != null && current != bootClassLoader) {
   
            if (current instanceof BaseDexClassLoader) {
   
                BaseDexClassLoader bdcCurrent = (BaseDexClassLoader) current;
                classLoadersChain.add(bdcCurrent);
                classPaths.add(String.join(File.pathSeparator, bdcCurrent.pathList.getDexPaths()));
            } else {
   
                onlySawSupportedClassLoaders = false;
                break;
            }
            current = current.getParent();
        }

        if (onlySawSupportedClassLoaders) {
   
            reporter.report(classLoadersChain, classPaths);
        }
    }

    /**
     * Constructs an instance.
     *
     * dexFile must be an in-memory representation of a full dexFile.
     *
     * @param dexFiles the array of in-memory dex files containing classes.
     * @param parent the parent class loader
     *
     * @hide
     */
    public BaseDexClassLoader(ByteBuffer[] dexFiles, ClassLoader parent) {
   
        // TODO We should support giving this a library search path maybe.
        super(parent);
        this.pathList = new DexPathList(this, dexFiles);
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
   
        List<Throwable> suppressedExceptions = new ArrayList<Throwable>();
        Class c = pathList.findClass(name, suppressedExceptions);
        if (c == null) {
   
            ClassNotFoundException cnfe = new ClassNotFoundException(
                    "Didn't find class \"" + name + "\" on path: " + pathList);
            for (Throwable t : suppressedExceptions) {
   
                cnfe.addSuppressed(t);
            }
            throw cnfe;
        }
        return c;
    }

    /**
     * @hide
     */
    public void addDexPath(String dexPath) {
   
        addDexPath(dexPath, false /*isTrusted*/);
    }

    /**
     * @hide
     */
    public void addDexPath(String dexPath, boolean isTrusted) {
   
        pathList.addDexPath(dexPath, null /*optimizedDirectory*/, isTrusted);
    }

    /**
     * Adds additional native paths for consideration in subsequent calls to
     * {@link #findLibrary(String)}
     * @hide
     */
    public void addNativePath(Collection<String> libPaths) {
   
        pathList.addNativePath(libPaths);
    }

    @Override
    protected URL findResource(String name) {
   
        return pathList.findResource(name);
    }

    @Override
    protected Enumeration<URL> findResources(String name) {
   
        return pathList.findResources(name);
    }

    @Override
    public String findLibrary(String name) {
   
        return pathList.findLibrary(name);
    }

    /**
     * Returns package information for the given package.
     * Unfortunately, instances of this class don't really have this
     * information, and as a non-secure {@code ClassLoader}, it isn't
     * even required to, according to the spec. Yet, we want to
     * provide it, in order to make all those hopeful callers of
     * {@code myClass.getPackage().getName()} happy. Thus we construct
     * a {@code Package} object the first time it is being requested
     * and fill most of the fields with dummy values. The {@code
     * Package} object is then put into the {@code ClassLoader}'s
     * package cache, so we see the same one next time. We don't
     * create {@code Package} objects for {@code null} arguments or
     * for the default package.
     *
     * <p>There is a limited chance that we end up with multiple
     * {@code Package} objects representing the same package: It can
     * happen when when a package is scattered across different JAR
     * files which were loaded by different {@code ClassLoader}
     * instances. This is rather unlikely, and given that this whole
     * thing is more or less a workaround, probably not worth the
     * effort to address.
     *
     * @param name the name of the class
     * @return the package information for the class, or {@code null}
     * if there is no package information available for it
     */
    @Override
    protected synchronized Package getPackage(String name) {
   
        if (name != null && !name.isEmpty()) {
   
            Package pack = super.getPackage(name);

            if (pack == null) {
   
                pack = definePackage(name, "Unknown", "0.0", "Unknown",
                        "Unknown", "0.0", "Unknown", null);
            }

            return pack;
        }

        return null;
    }

    /**
     * @hide
     */
    public String getLdLibraryPath() {
   
        StringBuilder result = new StringBuilder();
        for (File directory : pathList.getNativeLibraryDirectories()) {
   
            if (result.length() > 0) {
   
                result.append(':');
            }
            result.append(directory);
        }

        return result.toString();
    }

    @Override public String toString() {
   
        return getClass().getName() + "[" + pathList + "]";
    }

    /**
     * Sets the reporter for dex load notifications.
     * Once set, all new instances of BaseDexClassLoader will report upon
     * constructions the loaded dex files.
     *
     * @param newReporter the new Reporter. Setting null will cancel reporting.
     * @hide
     */
    public static void setReporter(Reporter newReporter) {
   
        reporter = newReporter;
    }

    /**
     * @hide
     */
    public static Reporter getReporter() {
   
        return reporter;
    }

    /**
     * @hide
     */
    public interface Reporter {
   
        /**
         * Reports the construction of a BaseDexClassLoader and provides information about the
         * class loader chain.
         * Note that this only reports if all class loader in the chain are BaseDexClassLoader.
         *
         * @param classLoadersChain the chain of class loaders used during the construction of the
         *     class loader. The first element is the BaseDexClassLoader being constructed,
         *     the second element is its parent, and so on.
         * @param classPaths the class paths of the class loaders present in
         *     {@param classLoadersChain}. The first element corresponds to the first class
         *     loader and so on. A classpath is represented as a list of dex files separated by
         *     {@code File.pathSeparator}.
         */
        void report(List<BaseDexClassLoader> classLoadersChain, List<String> classPaths);
    }
}

DexPathList

1、DexPathList源码

package dalvik.system;

/**
 * A pair of lists of entries, associated with a {@code Class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值