Android 热修复技术值_Robust(最简单)

技术介绍
技术 介绍
热更新 修改某处我们需要进行少量修改的地方或者Bug,修复快,时效性高
增量更新 原有app的基础上只更新发生变化的地方,其余保持原样
升级更新 在当前的版本做了大的修改时,我们需要全部下载Apk进行升级
Robust的实现
流程:

1.集成 Robust,生成 apk。保存期间的混淆文件 mapping.txt,以及 Robust 生成记录文件 methodMap.robust
2.使用注解 @Modify 或者方法 RobustModify.modify() 标注需要修复的方法
3.开启补丁插件,执行生成 apk 命令,获得补丁包 patch.jar
4.通过推送或者接口的形式,通知 app 有补丁,需要修复 5.加载补丁文件不需要重新启动应用

一、添加依赖:

1.添加 classpath 'com.meituan.robust:gradle-plugin:0.3.3'
classpath 'com.meituan.robust:auto-patch-plugin:0.3.3'到build.gradle中

buildscript {
    repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.2.2'
    classpath 'com.meituan.robust:gradle-plugin:0.3.3'
    classpath 'com.meituan.robust:auto-patch-plugin:0.3.3'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}}

2.添加 compile 'com.meituan.robust:robust:0.3.3'到app-build.gradle-dependencies中

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-     core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.meituan.robust:robust:0.3.3'
testCompile 'junit:junit:4.12'}

3.添加两种模式到app-build.gradle中,具体看注解

 apply plugin: 'com.android.application'
 apply plugin: 'auto-patch-plugin'  //生成插件是打开
//apply plugin: 'robust'//生成Apk时打开

4.在app-build.gradle打开混淆,如果不打开混淆,编译Apk时将不生成mipping.txt文件,Robust更新会报错,找不到此文件,具体看后面

 buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.RoubstDemo
    }
}

注意:程序需要签名,不签名会导致安装Apk时提示文件损坏,签名可参考此篇文章Android-热修复技术之AndFix

二、添加robust.xml到app根目录中,看注解:




    
    
    true
    

    
    
    
    false

    
    
    
    false

    
    true
    

    
    
    false

    
    true
    

    
    true
    





    com.ffcs.z.robustdemo








    com.ffcs.z.robustdemo







注意:其中需要修改的为packname 、patchPackname 两个包名

Android 热修复技术值_Robust(最简单)_第1张图片
robust

robust配置完成

三、基本应用程序

需要开启的权限:





1.MainActivity

 package com.ffcs.z.robustdemo;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.meituan.robust.PatchExecutor;


public class MainActivity extends AppCompatActivity {

Button btn;
Button seconde;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();


}

private void init() {
    btn = (Button) findViewById(R.id.btn);
    seconde= (Button) findViewById(R.id.btn_seconde);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new PatchExecutor(getApplicationContext(), new PatchManipulateImp(), new RobustCallBackSample()).start();
        }
    });
    seconde.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this,SecondeActivity.class));
        }
    });
}}

声明两个按钮,一个更新按钮,一个跳转有bug的页面,其中需要注意的是更新所使用的方法为PatchExecutor,参数一、当前上下文,参数二,关联patch.jar的信息,参数三、回调

PatchManipulateImp .java

package com.ffcs.z.robustdemo;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import com.meituan.robust.Patch;
import com.meituan.robust.PatchManipulate;
import com.meituan.robust.RobustApkHashUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
  * Created by mivanzhang on 17/2/27.
  * We recommend you rewrite your own PatchManipulate class ,adding      your special patch Strategy,in the demo we just load the patch directly
  *   Pay attention to the difference of patch's LocalPath and patch's    TempPath
  *    We recommend LocalPath store the origin patch.jar which may be encrypted,while TempPath is the true runnable jar
  *    我们推荐继承PatchManipulate实现你们App独特的A补丁加载策略,其中setLocalPath设置补丁的原始路径,这个路径存储的补丁是加密过得,setTempPath存储解密之后的补丁,是可以执行的jar文件
*setTempPath设置的补丁加载完毕即刻删除,如果不需要加密和解密补丁,两者没有啥区别 */

 public class PatchManipulateImp extends PatchManipulate {
/***
 * connect to the network ,get the latest patches
 * l联网获取最新的补丁
 * @param context
 *
 * @return
 */
@Override
protected List fetchPatchList(Context context) {
    //将app自己的robustApkHash上报给服务端,服务端根据robustApkHash来区分每一次apk build来给app下发补丁
    //apkhash is the unique identifier for  apk,so you cannnot patch wrong apk.
    String robustApkHash = RobustApkHashUtils.readRobustApkHash(context);
    Log.i("PatchManipulateImp","robustApkHash :" + robustApkHash);
    //connect to network to get patch list on servers
    //在这里去联网获取补丁列表
    Patch patch = new Patch();
    patch.setName("123");
    //we recommend LocalPath store the origin patch.jar which may be encrypted,while TempPath is the true runnable jar
    //LocalPath是存储原始的补丁文件,这个文件应该是加密过的,TempPath是加密之后的,TempPath下的补丁加载完毕就删除,保证安全性
    //这里面需要设置一些补丁的信息,主要是联网的获取的补丁信息。重要的如MD5,进行原始补丁文件的简单校验,以及补丁存储的位置,这边推荐把补丁的储存位置放置到应用的私有目录下,保证安全性
    patch.setLocalPath(Environment.getExternalStorageDirectory().getPath()+ File.separator+"robust"+File.separator + "patch");

    //setPatchesInfoImplClassFullName 设置项各个App可以独立定制,需要确保的是setPatchesInfoImplClassFullName设置的包名是和xml配置项patchPackname保持一致,而且类名必须是:PatchesInfoImpl
    //请注意这里的设置
    patch.setPatchesInfoImplClassFullName("com.ffcs.z.robustdemo.PatchesInfoImpl");
    List  patches = new ArrayList();
    patches.add(patch);
    return patches;
}

/**
 *
 * @param context
 * @param patch
 * @return
 *
 * you can verify your patches here
 */
@Override

protected boolean verifyPatch(Context context, Patch patch) {

    //do your verification, put the real patch to patch
    //放到app的私有目录
    patch.setTempPath(context.getCacheDir()+ File.separator+"robust"+File.separator + "patch");
    Log.i("PatchManipulateImp","verifyPatch :" + context.getCacheDir()+ File.separator+"robust"+File.separator + "patch");
    //in the sample we just copy the file
    try {
        copy(patch.getLocalPath(), patch.getTempPath());
    }catch (Exception e){
        e.printStackTrace();
        throw new RuntimeException("copy source patch to local patch error, no patch execute in path "+patch.getTempPath());
    }

    return true;
}
public void copy(String srcPath,String dstPath) throws IOException {
    Log.i("PatchManipulateImp","copy :"+srcPath );
    File src=new File(srcPath);
    if(!src.exists()){
        throw new RuntimeException("source patch does not exist ");
    }
    File dst=new File(dstPath);
    if(!dst.getParentFile().exists()){
        dst.getParentFile().mkdirs();
    }
    InputStream in = new FileInputStream(src);
    try {
        OutputStream out = new FileOutputStream(dst);
        try {
            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            out.close();
        }
    } finally {
        in.close();
    }
}
/**
 *
 * @param patch
 * @return
 *
 * you may download your patches here, you can check whether patch is in the phone
 */
@Override
protected boolean ensurePatchExist(Patch patch) {
    return true;
}}

RobustCallBackSample.java

package com.ffcs.z.robustdemo;

import android.util.Log;
import com.meituan.robust.Patch;
import com.meituan.robust.RobustCallBack;

 public class RobustCallBackSample implements RobustCallBack {


@Override
public void onPatchListFetched(boolean result, boolean isNet) {
    Log.i("RobustCallBack", "onPatchListFetched result: " + result);
}

@Override
public void onPatchFetched(boolean result, boolean isNet, Patch patch) {
    Log.i("RobustCallBack", "onPatchFetched result: " + result);
    Log.i("RobustCallBack", "onPatchFetched isNet: " + isNet);
    Log.i("RobustCallBack", "onPatchFetched patch: " + patch.getName());
}

@Override
public void onPatchApplied(boolean result, Patch patch) {
    Log.i("RobustCallBack", "onPatchApplied result: " + result);
    Log.i("RobustCallBack", "onPatchApplied patch: " + patch.getName());

}

@Override
public void logNotify(String log, String where) {
    Log.i("RobustCallBack", "logNotify log: " + log);
    Log.i("RobustCallBack", "logNotify where: " + where);
}

@Override
public void exceptionNotify(Throwable throwable, String where) {
    Log.e("RobustCallBack", "exceptionNotify where: " + where, throwable);
}}

2.SecondeActivity.java 第二个界面 声明了一个TextView控件

  package com.ffcs.z.robustdemo;

  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.widget.TextView;
  import com.meituan.robust.patch.annotaion.Modify;

public class SecondeActivity extends AppCompatActivity {

TextView t;

@Override
@Modify
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seconde);
    t= (TextView) findViewById(R.id.text);//        t.setText("未修改");
      t.setText("已经修改");
}}
四、编译生成Apk(PS:也就是有Bug(SecondeActivity.java中的TextView为“未修改”))

打开美团生成Apk模式

     apply plugin: 'com.android.application'
   //apply plugin: 'auto-patch-plugin'  //生成插件是打开
     apply plugin: 'robust'//生成Apk时打开

编译指令:gradlew clean assembleRelease --stacktrace --no-daemon

Android 热修复技术值_Robust(最简单)_第2张图片
编译有Bug的Apk
Android 热修复技术值_Robust(最简单)_第3张图片
编译后的工程项目app->build->outputs

创建app/robust文件夹 将outputs/release中的mipping.txt和outputs/robust中的methodsMap.robust文件考到app/robust文件下

mipping.txt:该文件列出了原始的类,方法和字段名与混淆后代码间的映射。这个文件很重要,可以用它来翻译被混淆的代码
methodsMap.robust:该文件在打补丁的时候用来区别到底哪些方法需要被修复,所以有它才能打补丁

五、生成补丁包

1.打开美团补丁模式 app-build.gradle:

  apply plugin: 'com.android.application'
  apply plugin: 'auto-patch-plugin'  //生成插件是打开
//apply plugin: 'robust'//生成Apk时打开

2.修改Bug

Android 热修复技术值_Robust(最简单)_第4张图片
image.png

此处除了Modify还有Add等,有需要的可以了解一下

3.编译程序

Android 热修复技术值_Robust(最简单)_第5张图片
编译结果
Android 热修复技术值_Robust(最简单)_第6张图片
生成更新包位置

将更新包考到手机robust文件夹中即可

更新时请按更新按钮

注意:手机需要手动开启存储权限,没有开启会报:未找到...../robust/patch.jar

你可能感兴趣的:(Android 热修复技术值_Robust(最简单))