美文网首页
热修复原理解剖

热修复原理解剖

作者: 遥遥的远方 | 来源:发表于2018-01-04 17:27 被阅读0次
    • 微信 -----thinker热修复(framework---替换整个dex就是整个class文件)
      dex分包(class文件打包成dex)
      Aclass(Bug) --dex1 ---dex2(重新生成的,去替换dex1,插队)
      java变成class ---dex---apk(打包流程)

    • 阿里云----andfix热修复(替换一个method就是替换一个class中的一个方法)--定点清除bug
      第一步:在服务器 生成修复包 这个修复包是不抛出异常的class----打包成dex---用户下载最新的dex包文件
      第二步: app客户端加载 dex包
      第三步:找到dex包中要修复的class----在找到Method(修复好的)----在找到Method(Bug)----替换

    第二步的实现:
               private Context contex;
               public DxManager(Context context){}
               private void loadDex(File dexFilePath){
               
             //加载dex
             DexFile dexFile = DexFile.loadDex(dex文件路径,dex缓存路径,标志位(Contex.MODE_PRIVATE));
             //遍历dex里面的class
               Enumeration<String> entry = dexFile.entries();
    //遍历
               while(){
                     String className = entry.nextElement();
                       //修复好的realClass,怎么找到要修复的class(bug)--通过注解的方式找到
                       Class realClass = dexFile.load(className ,context.getClassLoad()(类加载器));
                        //修复
                       Method[] methods = reclass.getDeclareMethods();
                       for(){
                           Replace replace = method.getAnnotation(Replace.class);
                           if(replace==null) continue;
                           String wrongClassName = replace.class();
                             String wrongMethodName = replace.method();
                           Class wrongClass = Class.forName(wrongClassName);
    //最终拿到错误的Method对象                        
    Method  wrongMethod = wrongClass.getMehtod(wrongMethodName,method.getParamenterTypes);
                         //修复方法,用C++写方法用于替换
                         
                       }
                   }
               }
       }
    
    建立注解:
    @Target(ElementType.METHOD)//用在哪个位置上(可以是方法,成员变量)
    @Retention(RetentionPolicy.RUNTIME)//加载注解时运行在哪个时刻
    public  @interface Replace{
     String class();
      String method();
    }
    
    
    //客户端会出错的代码---版本已经发出去的代码
    public class Caculutor{
     public int catulator(
       //抛异常
       int i = 0;
        int j = 10;
     return j/i;
     )
    }
    
    //服务端进行修复代码--把这个类单独拿出来打包成dex文件--build-classes-com-包名-类名(要打包成dex文件的类)
    public class Caculutor{
    @Replace(class="包名+类名",method="要修复的方法")
     public int catulator(
       //修复
       int i = 1;
        int j = 10;
     return j/i;
     )
    }
    

    //讲class打包成dex
    build-tools -dx.bat

    java中的main方法执行后会在jvm中开辟堆区(main--方法)和栈区(对象区)
    方法的调用就是栈区方法的----栈帧

    art结构体(执行的方法在系统层生成art结构体)

    4QAQRQSTQWB%P%R18G1G7TT.png

    相关文章

      网友评论

          本文标题:热修复原理解剖

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