Android类加载

2023-02-04  本文已影响0人  Michael0016

双亲委托

某个类加载时,首先委托给parent加载,依次递归,如果parent可以拿到对象则返回,否则由子类获取

优点
  1. 避免重复加载
  2. 安全考虑,防止核心API被修改
    protected Class<?> loadClass(String name, boolean resolve)
        throws ClassNotFoundException
    {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                try {
                    if (parent != null) {
                        c = parent.loadClass(name, false);
                    } else {
                        c = findBootstrapClassOrNull(name);
                    }
                } catch (ClassNotFoundException e) {
                    // ClassNotFoundException thrown if class not found
                    // from the non-null parent class loader
                }

                if (c == null) {
                    // If still not found, then invoke findClass in order
                    // to find the class.
                    c = findClass(name);
                }
            }
            return c;
    }

热修复

类加载是有先后顺序,
首先获取到当前应用PathClassLoader,通过反射获取到DexPathList的pathList;修改pathList中的dexElements,

  1. 把补丁包patch.dex转化为Element[] patch
  2. 获取到dexElement对象属性
  3. patch+Dexelement合并,并反射赋值给dexElements


    image.png
image.png
AndFix

反射 类加载


image.png

参考:https://www.bilibili.com/video/BV1tS4y1r7qe/?spm_id_from=333.880.my_history.page.click&vd_source=5e4ad34e631c98068c626ad8a062e008

上一篇 下一篇

猜你喜欢

热点阅读