首页 > 其他分享 >2016-07-30-Android中的类加载器及类加载流程

2016-07-30-Android中的类加载器及类加载流程

时间:2024-02-26 15:13:40浏览次数:183  
标签:code 07 BaseDexClassLoader 30 pathList String null class 加载

Android中的类加载器及类加载流程

Android中的类加载器有三种,DexClassLoaderPathClassLoaderBootClassLoader
其中BootClassLoader是系统启动时预加载常用类的,一般使用不到。DexClassLoaderPathClassLoader都是继承自BaseDexClassLoader
DexClassLoaderPathClassLoader并没有重写BaseDexClassLoader中的任何方法,所以源码只需要看BaseDexClassLoader即可。

由于Android SDK并没有包含BaseDexClassLoader,所以需要到源码查询网站查询源码,如下:

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dalvik.system;

import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;

import android.annotation.SystemApi;
import android.compat.annotation.UnsupportedAppUsage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import libcore.util.NonNull;
import libcore.util.Nullable;
import sun.misc.CompoundEnumeration;

/**
 * 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;

    /**
     * Array of ClassLoaders that can be used to load classes and resources that the code in
     * {@code pathList} may depend on. This is used to implement Android's
     * <a href=https://developer.android.com/guide/topics/manifest/uses-library-element>
     * shared libraries</a> feature.
     * <p>The shared library loaders are always checked before the {@code pathList} when looking
     * up classes and resources.
     *
     * <p>{@code null} if the class loader has no shared library.
     *
     * @hide
     */
    protected final ClassLoader[] sharedLibraryLoaders;

    /**
     * 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, librarySearchPath, parent, null, false);
    }

    /**
     * @hide
     */
    public BaseDexClassLoader(String dexPath, File optimizedDirectory,
            String librarySearchPath, ClassLoader parent, boolean isTrusted) {
        this(dexPath, librarySearchPath, parent, null, isTrusted);
    }

    /**
     * @hide
     */
    public BaseDexClassLoader(String dexPath,
            String librarySearchPath, ClassLoader parent, ClassLoader[] libraries) {
        this(dexPath, librarySearchPath, parent, libraries, false);
    }

    /**
     * BaseDexClassLoader implements the Android
     * <a href=https://developer.android.com/guide/topics/manifest/uses-library-element>
     * shared libraries</a> feature by changing the typical parent delegation mechanism
     * of class loaders.
     * <p> Each shared library is associated with its own class loader, which is added to a list of
     * class loaders this BaseDexClassLoader tries to load from in order, immediately checking
     * after the parent.
     * The shared library loaders are always checked before the {@code pathList} when looking
     * up classes and resources.
     *
     * @hide
     */
    public BaseDexClassLoader(String dexPath,
            String librarySearchPath, ClassLoader parent, ClassLoader[] sharedLibraryLoaders,
            boolean isTrusted) {
        super(parent);
        // Setup shared libraries before creating the path list. ART relies on the class loader
        // hierarchy being finalized before loading dex files.
        this.sharedLibraryLoaders = sharedLibraryLoaders == null
                ? null
                : Arrays.copyOf(sharedLibraryLoaders, sharedLibraryLoaders.length);
        this.pathList = new DexPathList(this, dexPath, librarySearchPath, null, isTrusted);

        // Run background verification after having set 'pathList'.
        this.pathList.maybeRunBackgroundVerification(this);

        reportClassLoaderChain();
    }

    /**
     * Reports the current class loader chain to the registered {@code reporter}.
     *
     * @hide
     */
    @SystemApi(client = MODULE_LIBRARIES)
    @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
    public void reportClassLoaderChain() {
        if (reporter == null) {
            return;
        }

        String[] classPathAndClassLoaderContexts = computeClassLoaderContextsNative();
        if (classPathAndClassLoaderContexts.length == 0) {
            return;
        }
        Map<String, String> dexFileMapping =
                new HashMap<>(classPathAndClassLoaderContexts.length / 2);
        for (int i = 0; i < classPathAndClassLoaderContexts.length; i += 2) {
            dexFileMapping.put(classPathAndClassLoaderContexts[i],
                    classPathAndClassLoaderContexts[i + 1]);
        }
        reporter.report(Collections.unmodifiableMap(dexFileMapping));
    }

    /**
     * Computes the classloader contexts for each classpath entry in {@code pathList.getDexPaths()}.
     *
     * Note that this method is not thread safe, i.e. it is the responsibility of the caller to
     * ensure that {@code pathList.getDexPaths()} is not modified concurrently with this method
     * being called.
     *
     * @return A non-null array of non-null strings of length
     *   {@code 2 * pathList.getDexPaths().size()}. Every even index (0 is even here) is a dex file
     *   path and every odd entry is the class loader context used to load the previously listed dex
     *   file. E.g. a result might be {@code { "foo.dex", "PCL[]", "bar.dex", "PCL[foo.dex]" } }.
     */
    private native String[] computeClassLoaderContextsNative();

    /**
     * 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 librarySearchPath the list of directories containing native
     *   libraries, delimited by {@code File.pathSeparator}; may be {@code null}
     * @param parent the parent class loader
     *
     * @hide
     */
    public BaseDexClassLoader(ByteBuffer[] dexFiles, String librarySearchPath, ClassLoader parent) {
        super(parent);
        this.sharedLibraryLoaders = null;
        this.pathList = new DexPathList(this, librarySearchPath);
        this.pathList.initByteBufferDexPath(dexFiles);
        // Run background verification after having set 'pathList'.
        this.pathList.maybeRunBackgroundVerification(this);
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        // First, check whether the class is present in our shared libraries.
        if (sharedLibraryLoaders != null) {
            for (ClassLoader loader : sharedLibraryLoaders) {
                try {
                    return loader.loadClass(name);
                } catch (ClassNotFoundException ignored) {
                }
            }
        }
        // Check whether the class in question is present in the dexPath that
        // this classloader operates on.
        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;
    }

    /**
     * Adds a new dex path to path list.
     *
     * @param dexPath dex path to add to path list
     *
     * @hide
     */
    @SystemApi(client = MODULE_LIBRARIES)
    @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
    public void addDexPath(@Nullable 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)}.
     *
     * @param libPaths collection of paths to be added to path list
     *
     * @hide
     */
    @SystemApi(client = MODULE_LIBRARIES)
    @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
    public void addNativePath(@NonNull Collection<String> libPaths) {
        pathList.addNativePath(libPaths);
    }

    @Override
    protected URL findResource(String name) {
        if (sharedLibraryLoaders != null) {
            for (ClassLoader loader : sharedLibraryLoaders) {
                URL url = loader.getResource(name);
                if (url != null) {
                    return url;
                }
            }
        }
        return pathList.findResource(name);
    }

    @Override
    protected Enumeration<URL> findResources(String name) {
        Enumeration<URL> myResources = pathList.findResources(name);
        if (sharedLibraryLoaders == null) {
          return myResources;
        }

        Enumeration<URL>[] tmp =
            (Enumeration<URL>[]) new Enumeration<?>[sharedLibraryLoaders.length + 1];
        // This will add duplicate resources if a shared library is loaded twice, but that's ok
        // as we don't guarantee uniqueness.
        for (int i = 0; i < sharedLibraryLoaders.length; i++) {
            try {
                tmp[i] = sharedLibraryLoaders[i].getResources(name);
            } catch (IOException e) {
                // Ignore.
            }
        }
        tmp[sharedLibraryLoaders.length] = myResources;
        return new CompoundEnumeration<>(tmp);
    }

    @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 fake 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;
    }

    /**
     * Returns colon-separated set of directories where libraries should be
     * searched for first, before the standard set of directories.
     *
     * @return colon-separated set of search directories
     *
     * @hide
     */
    @SystemApi(client = MODULE_LIBRARIES)
    @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
    public @NonNull 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 {@code null} will cancel reporting.
     * @hide
     */
    @SystemApi(client = MODULE_LIBRARIES)
    @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
    public static void setReporter(@Nullable Reporter newReporter) {
        reporter = newReporter;
    }

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

    /**
     * Reports the construction of a {@link BaseDexClassLoader} and provides opaque
     * information about the class loader chain.
     *
     * @hide
     */
    @SystemApi(client = MODULE_LIBRARIES)
    @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
    public interface Reporter {
        /**
         * Reports the construction of a BaseDexClassLoader and provides opaque information about
         * the class loader chain. For example, if the childmost ClassLoader in the chain:
         * {@quote BaseDexClassLoader { foo.dex } -> BaseDexClassLoader { base.apk }
         *    -> BootClassLoader } was just initialized then the load of {@code "foo.dex"} would be
         * reported with a classLoaderContext of {@code "PCL[];PCL[base.apk]"}.
         *
         * @param contextsMap A map from dex file paths to the class loader context used to load
         *     each dex file.
         *
         * @hide
         */
        @SystemApi(client = MODULE_LIBRARIES)
        @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
        void report(@NonNull Map<String, String> contextsMap);
    }
}

复制这个java文件到对应源码文件夹下就可以在Android Studio中查看了。

通过调试可以看到,Android中普通类的加载器其实是PathClassLoader。追踪PathClassLoader.findClass方法,即可获取Android的类加载过程:

PathClassLoader.findClass -- 继承自 --> BaseDexClassLoader.findClass()
-> BaseDexClassLoader.pathList.findClass()
-> DexPathList.dexElements.foreach { element.findClass() }
-> Element.findClass()
-> Element.dexFile.loadClassBinaryName()
-> DexFile.defineClass()

即类加载过程通过BaseDexClassLoader.findClassDexPathList.findClassElement.findClassDexFile.loadClassBinaryName,最终会落到DexFile.defineClass方法中,然后就交给native层了。

其中需要注意的是,在BaseDexClassLoader.findClass的开头有这么一段:

        if (sharedLibraryLoaders != null) {
            for (ClassLoader loader : sharedLibraryLoaders) {
                try {
                    return loader.loadClass(name);
                } catch (ClassNotFoundException ignored) {
                }
            }
        }

这段是在Android 10新加入的,据称是为了实现shared library功能的,在之前的版本中没有这一段。

通过替换DexPathList实现热修复

在上一节中知道了,类加载的流程如下:
BaseDexClassLoader.findClass() ->
BaseDexClassLoader.pathList.findClass() ->
DexPathList.dexElements.foreach { element.findClass() } ->
Element.findClass() -> ...

DexPathList.findClass方法:

    private Element[] dexElements;

    public Class<?> findClass(String name, List<Throwable> suppressed) {
        for (Element element : dexElements) {
            Class<?> clazz = element.findClass(name, definingContext, suppressed);
            if (clazz != null) {
                return clazz;
            }
        }

        if (dexElementsSuppressedExceptions != null) {
            suppressed.addAll(Arrays.asList(dexElementsSuppressedExceptions));
        }
        return null;
    }

可以发现,DexPathList加载类的方法是遍历dexElements数组依次加载,知道获取到值为止。所以可以通过修改这个数组,把新的dex文件放在数组的前面,使其加载修改后的类,从而实现热修复。

根据以上原理,写下这个工具类,有效性待验证:

public class HotFix {
    private static final String TAG = "HotFix";
    private static final ReflectUtil ReflectUtil = ReflectUtilKt.INSTANCE;

    /**
     * 传入热修复的dex文件位置,优先加载其中的类。
     *
     * @param context     the context
     * @param dexFilePath dex文件的位置
     */
    @SuppressWarnings("ConstantConditions")
    public static void fix(Context context, String dexFilePath) throws Exception {
        ClassLoader classLoader = context.getClassLoader();
        Log.d(TAG, "createClassLoader: " + classLoader.getClass().getSimpleName());
        // BaseDexClassLoader.pathList
        Field pathListField = ReflectUtil.getField(BaseDexClassLoader.class, "pathList");
        pathListField.setAccessible(true);
        Object pathList = pathListField.get(classLoader);
        // DexPathList.dexElements
        Field dexElementsField = ReflectUtil.getField(pathListField.getType(), "dexElements");
        dexElementsField.setAccessible(true);
        Object dexElements = dexElementsField.get(pathList);
        // 处理Element数组
        Class<?> elementType = dexElements.getClass().getComponentType();
        int length = Array.getLength(dexElements);
        Log.d(TAG, "createClassLoader: dexElements = " + dexElements + ", length = " + length);
        Object newDexElements = Array.newInstance(elementType, length + 1);
        // 旧的element依次往后挪一位
        for (int i = 0; i < length; i++) {
            Array.set(newDexElements, i + 1, Array.get(dexElements, i));
        }
        // 新的element插入到第0项
        DexFile dexFile = loadDex(context, dexFilePath); // todo create dex file
        Object insertedElement = elementType
                .getConstructor(DexFile.class)
                .newInstance(dexFile);
        Array.set(newDexElements, 0, insertedElement);
        // 替换dexElements
        dexElementsField.set(pathList, newDexElements);
    }

    private static DexFile loadDex(Context context, String path)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<DexFile> clazz = DexFile.class;
        Method loadDexMethod = clazz.getDeclaredMethod("loadDex", String.class, String.class, int.class);
        loadDexMethod.setAccessible(true);
        return (DexFile) loadDexMethod.invoke(null, path, context.getCacheDir().getAbsolutePath(), 0);
    }
}

https://upload-images.jianshu.io/upload_images/3171298-8848cd3fb5d0c403.png

作者:littlefogcat
链接:https://www.jianshu.com/p/80bcf2b90305
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

标签:code,07,BaseDexClassLoader,30,pathList,String,null,class,加载
From: https://www.cnblogs.com/jamboo/p/18034334

相关文章

  • MDC300-16-ASEMI电源控制柜MDC300-16
    编辑:llMDC300-16-ASEMI电源控制柜MDC300-16型号:MDC300-16品牌:ASEMI封装:M25最大重复峰值反向电压:1600V最大正向平均整流电流(Vdss):300A功率(Pd):大功率芯片个数:6引脚数量:5类型:整流模块、整流桥正向浪涌电流:600A正向电压:1.35V最大输出电压(RMS):封装尺寸:如图工作温度:-40......
  • maven使用systemPath方式加载本地jar(代码库)
    比如,jar包直接放到根目录 pom.xml里这么配置<dependency><groupId>njcanc</groupId><artifactId>njcanc</artifactId><version>2.12</version><scope>system</scope><systemPath>${project.b......
  • 洛谷题单指南-贪心-P1094 [NOIP2007 普及组] 纪念品分组
    原题链接:https://www.luogu.com.cn/problem/P1094题意解读:贪心选择解题思路:贪心策略:将纪念品按价格由小到大排序,优先选择价格大的一直到超过分组价格上限,再选择价格小的直到超过价格上限,此为一组重复以上过程,直到所有数据都遍历到,采用一头一尾双指针即可。证明:如果最大价格......
  • LG5290/LOJ3052 春节十二响 题解(启发式合并)
    考虑当这个东西是一条链的时候我们该怎么做,显然\(1\)​会有两个儿子,然后两个儿子分别是一条链。所以我们可以给两个儿子的链上的所有节点分别加到两个堆里,每次取出两个堆的最大值加入到我们选择的答案中,然后把两个堆的最大值全部pop掉。最终的答案就是我们pop完一个堆之后,......
  • 无法加载DLL“xxxx”:找不到指定的模块
    以前的一个c#项目,今天运行的时候突然发现调用DLL时出现了下面的错误。心中很诧异,明明以前能用的,今天怎么报错了。代码没有修改,也不存在DLL路径错误的问题。后来看到有网友说用depends这个软件可以对DLL文件进行分析,就拿来一试。果然找到了问题。是以前编译器调用的库和现在编......
  • 1.30
    属性是HTML元素提供的附加信息。HTML属性HTML元素可以设置属性属性可以在元素中添加附加信息属性一般描述于开始标签属性总是以名称/值对的形式出现,比如:name="value"。属性实例HTML链接由<a>标签定义。链接的地址在 href属性中指定:实例<ahref="http://ww......
  • Programming Abstractions in C阅读笔记:p293-p302
    《ProgrammingAbstractionsinC》学习第73天,p293-p302总结,总计10页。一、技术总结1.时间复杂度(1)quadratictime(二次时间)p293,AlgorithmslikeselectionsortthatexhibitO(N^2)performancearesaidtoruninquadratictime。2.线性查找(linearsearch)p293,B......
  • Java基础07:基本运算符
    运算符1.Java语言支持如下运算符:1.1算术运算符:+,-,*,/,%,++,--1.2赋值运算符:=1.3关系运算符:>,<,>=,==,!=instanceof1.4逻辑运算符:&,|,^,~,>>,<<,>>>(了解)1.5条件运算符?:1.6扩展赋值运算:+=,-=,*=,/= ......
  • 07 使用IDEA开发
    IDEA软件安装软件IDEA官方下载地址步骤一下载进入官网地址选择电脑对应的版本进行下载。步骤二安装下载完成双击引用程序进行安装选择安装路径安装完成。步骤三运行双击打开IDEA解释下标注的地方:①:Projects:项目;②:NewProject:新建一个项......
  • CF1930E 2..3...4.... Wonderful! Wonderful! 题解
    DescriptionStackhasanarray$a$oflength$n$suchthat$a_i=i$forall$i$($1\leqi\leqn$).Hewillselectapositiveinteger$k$($1\leqk\leq\lfloor\frac{n-1}{2}\rfloor$)anddothefollowingoperationon$a$an......