美文网首页
Android类加载

Android类加载

作者: Michael0016 | 来源:发表于2023-02-04 20:04 被阅读0次

双亲委托

某个类加载时,首先委托给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

相关文章

网友评论

      本文标题:Android类加载

      本文链接:https://www.haomeiwen.com/subject/mcwohdtx.html