1.在App的build.gradle近速,加入如下配置
//apply plugin: 'auto-patch-plugin'//制作補(bǔ)丁時使用
apply plugin: 'robust'//打包時使用
dependencies {
compile 'com.meituan.robust:robust:0.3.8'//需要的依賴庫
}
2.在整個項目的build.gradle加入classpath如下
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.meituan.robust:gradle-plugin:0.3.8'
classpath 'com.meituan.robust:auto-patch-plugin:0.3.8'
}
}
3.然后在自己的引導(dǎo)頁面或主頁書寫加載執(zhí)行補(bǔ)丁的代碼
注**如果你是測試你可以在本地加載補(bǔ)丁 ? 如果你是要上線使用建議直接讓后臺寫個接口供你提交和下載補(bǔ)丁
以下是我直接請求網(wǎng)絡(luò)加載補(bǔ)丁的代碼僅供參考
private voidrunRobust() {
try{
String destFileDir= Environment.getExternalStorageDirectory().getPath()+ File.separator+"robust";
File file=newFile(destFileDir);
if(file .exists()? && file .isDirectory()){
FileManager.deleteFolderFile(destFileDir,true);
}
}catch(Exception e){
}
MyHttp http=newMyHttp(this);
http.downloadbug(this,"你的補(bǔ)丁下載地址",newHttpCallBack(){
@Override
public voidonSuccess(String t) {
super.onSuccess(t);
newPatchExecutor(getApplicationContext(),newPatchManipulateImp(),newCallback()).start();
setAnim();
}
@Override
public voidonFailure(interrorNo, String strMsg) {
super.onFailure(errorNo, strMsg);
setAnim();
}
});
}
//補(bǔ)丁加載成功失敗的回調(diào)
classCallbackimplementsRobustCallBack {
@Override
public voidonPatchListFetched(booleanresult,booleanisNet, List patches) {
System.out.println("《《《《robust arrived in onPatchListFetched");
}
@Override
public voidonPatchFetched(booleanresult,booleanisNet, Patch patch) {
System.out.println("《《《《robust arrived in onPatchFetched");
}
@Override
public voidonPatchApplied(booleanresult, Patch patch) {
System.out.println(" robust arrived in onPatchApplied ");
}
@Override
public voidlogNotify(String log, String where) {
System.out.println(" robust arrived in logNotify "+ where);
}
@Override
public voidexceptionNotify(Throwable throwable, String where) {
throwable.printStackTrace();
System.out.println(" robust arrived in exceptionNotify "+ where);
}
}
可以使用以下代碼直接加載本地的補(bǔ)丁
new PatchExecutor(上下文,xxx,callback我也懂).start()
new PatchExecutor(getApplicationContext(), new PatchManipulateImp(), newCallback()).start();
adb push G:\meituan\patch.jar /sdcard/robust/patch.jar//本地測試用
4.需要手動copy一份DEMO中或下面我的文件robust.xml的配置文件到app的目錄下,該文件各個配置注釋的很清楚,若沒特殊要求,需要修改
5.運(yùn)行生成apk的命令
執(zhí)行打包命令:
gradlew clean ?assembleRelease --stacktrace --no-daemon
注:不想使用命令生成的也可以通過打成正式包來生成apk
6.apk生成成功后找到build/outputs/robust/methodsMap.robust,build/outputs/mapping/mapping.txt(需要開啟混淆后才會出現(xiàn)此文件)韩脑,我們需要自己分別拷貝到app/robust下,在app目錄新建個叫robust的文件夾粹污,把這兩個文件放進(jìn)去就ok了段多。
保存好現(xiàn)有的工程及robust下的文件 ?最好是備份下傳到百度網(wǎng)盤或自己的硬盤上當(dāng)我們的程序出現(xiàn)問題需要更新的時候我們要通過我們的這個工程進(jìn)行修改打補(bǔ)丁提交補(bǔ)丁來實(shí)現(xiàn)熱更新的。
7.程序上線一段后壮吩,我們要生成補(bǔ)丁patch.jar來更新代碼了
我們可以在備份的代碼中進(jìn)行更改和添加數(shù)據(jù) ?更改后的方法上要加上注解 ?@Modify
添加了新的方法后 添加注解 @Add ? 有Lambda的需要添加 RobustModify.modify()
修改好后进苍,我們就可以把 //apply plugin: 'auto-patch-plugin' ?打開
之后再在終端執(zhí)行一遍生成apk的命令行。直到終端那里出現(xiàn) auto patch endsuccessfully(最后 build failed 是正常的粥航,別緊張孩子)琅捏。再然后就是要把patch.jar push 到手機(jī)目錄路徑下或者上傳到你們的服務(wù)器。