热修复框架 - Tinker patch合成流程

经过TinkerInstaller.install 安装之后,Tinker相关初始化工作也都做好了,万事俱备等待手动执行合成patch包。

一、合成patch包

主动触发:

TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(), patch);//patchs是diff patch路径

TinkerInstaller.java

public static void onReceiveUpgradePatch(Context context, String patchLocation) {
    Tinker.with(context).getPatchListener().onPatchReceived(patchLocation);
}

DefaultPatchListener.java

public int onPatchReceived(String path) {
    final File patchFile = new File(path);
   final String patchMD5 = SharePatchFileUtil.getMD5(patchFile);
   final int returnCode = patchCheck(path, patchMD5);
   if (returnCode == ShareConstants.ERROR_PATCH_OK) {
        runForgService();
        //补丁校验成功,则启动服务来处理patch合成,这个服务是单独起了patch进程的
       TinkerPatchService.runPatchService(context, path);
   } else {
        Tinker.with(context).getLoadReporter().onLoadPatchListenerReceiveFail(new File(path), returnCode);
   }
    return returnCode;
}

TinkerPatchService.java

public static void runPatchService(final Context context, final String path) {
    TinkerLog.i(TAG, "run patch service...");
   Intent intent = new Intent(context, TinkerPatchService.class);
   intent.putExtra(PATCH_PATH_EXTRA, path);
   intent.putExtra(RESULT_CLASS_EXTRA, resultServiceClass.getName());
   try {
        context.startService(intent);//启动当前服务
   } catch (Throwable thr) {
        TinkerLog.e(TAG, "run patch service fail, exception:" + thr);
   }
}

@Override
protected void onHandleIntent(Intent intent) {
    increasingPriority();//提升服务优先级为前台,减少内存紧张时候被lmk kill的风险
   doApplyPatch(this, intent);//服务执行
}

private static void doApplyPatch(Context context, Intent intent) {
    // Since we may retry with IntentService, we should prevent
   // racing here again.
   if (!sIsPatchApplying.compareAndSet(false, true)) {
        TinkerLog.w(TAG, "TinkerPatchService doApplyPatch is running by another runner.");
       return;
   }
    Tinker tinker = Tinker.with(context);
   tinker.getPatchReporter().onPatchServiceStart(intent);//初始化重试相关信息配置
   if (intent == null) {
        TinkerLog.e(TAG, "TinkerPatchService received a null intent, ignoring.");
       return;
   }
    String path = getPatchPathExtra(intent);
   if (path == null) {
        TinkerLog.e(TAG, "TinkerPatchService can't get the path extra, ignoring.");
       return;
   }
    File patchFile = new File(path);
   long begin = SystemClock.elapsedRealtime();
   boolean result;
   long cost;
   Throwable e = null;
   PatchResult patchResult = new PatchResult();
   try {
        if (upgradePatchProcessor == null) {
            throw new TinkerRuntimeException("upgradePatchProcessor is null.");
       }
       //合成patch
        result = upgradePatchProcessor.tryPatch(context, path, patchResult);
   } catch (Throwable throwable) {
        e = throwable;
       result = false;
       tinker.getPatchReporter().onPatchException(patchFile, e);
   }
    cost = SystemClock.elapsedRealtime() - begin;
   tinker.getPatchReporter()
            .onPatchResult(patchFile, result, cost);
   patchResult.isSuccess = result;
   patchResult.rawPatchFilePath = path;
   patchResult.costTime = cost;
   patchResult.e = e;
   //通过AbstractResultService实现类来处理返回结果,比如定制一些合成完成之后的退出机制。
   AbstractResultService.runResultService(context, patchResult, getPatchResultExtra(intent));
   sIsPatchApplying.set(false);
}

合成patch看upgradePatchProcessor.tryPatch

UpgradePatch.java

@Override
public boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult) {
   Tinker manager = Tinker.with(context);
   final File patchFile = new File(tempPatchPath);
   //tinker热修复enbale
   if (!manager.isTinkerEnabled() || !ShareTinkerInternals.isTinkerEnableWithSharedPreferences(context)) {
       TinkerLog.e(TAG, "UpgradePatch tryPatch:patch is disabled, just return");
       return false;
   }
   //patch文件合法
   if (!SharePatchFileUtil.isLegalFile(patchFile)) {
       TinkerLog.e(TAG, "UpgradePatch tryPatch:patch file is not found, just return");
       return false;
   }
   //check the signature, we should create a new checker
   ShareSecurityCheck signatureCheck = new ShareSecurityCheck(context);
   int returnCode = ShareTinkerInternals.checkTinkerPackage(context, manager.getTinkerFlags(), patchFile, signatureCheck);
   if (returnCode != ShareConstants.ERROR_PACKAGE_CHECK_OK) {
       TinkerLog.e(TAG, "UpgradePatch tryPatch:onPatchPackageCheckFail");
       manager.getPatchReporter().onPatchPackageCheckFail(patchFile, returnCode);
       return false;
   }
   String patchMd5 = SharePatchFileUtil.getMD5(patchFile);
   if (patchMd5 == null) {
       TinkerLog.e(TAG, "UpgradePatch tryPatch:patch md5 is null, just return");
       return false;
   }
   //use md5 as version
   patchResult.patchVersion = patchMd5;
   TinkerLog.i(TAG, "UpgradePatch tryPatch:patchMd5:%s", patchMd5);
   //check ok, we can real recover a new patch
   final String patchDirectory = manager.getPatchDirectory().getAbsolutePath();
   //info.lock 文件
   File patchInfoLockFile = SharePatchFileUtil.getPatchInfoLockFile(patchDirectory);
   //patch.info 文件
   File patchInfoFile = SharePatchFileUtil.getPatchInfoFile(patchDirectory);
   final Map<String, String> pkgProps = signatureCheck.getPackagePropertiesIfPresent();
   if (pkgProps == null) {
       TinkerLog.e(TAG, "UpgradePatch packageProperties is null, do we process a valid patch apk ?");
       return false;
   }
   final String isProtectedAppStr = pkgProps.get(ShareConstants.PKGMETA_KEY_IS_PROTECTED_APP);
   final boolean isProtectedApp = (isProtectedAppStr != null && !isProtectedAppStr.isEmpty() && !"0".equals(isProtectedAppStr));
   SharePatchInfo oldInfo = SharePatchInfo.readAndCheckPropertyWithLock(patchInfoFile, patchInfoLockFile);
   //it is a new patch, so we should not find a exist
   SharePatchInfo newInfo;
   //already have patch
   if (oldInfo != null) {
       if (oldInfo.oldVersion == null || oldInfo.newVersion == null || oldInfo.oatDir == null) {
           TinkerLog.e(TAG, "UpgradePatch tryPatch:onPatchInfoCorrupted");
           manager.getPatchReporter().onPatchInfoCorrupted(patchFile, oldInfo.oldVersion, oldInfo.newVersion);
           return false;
       }
       if (!SharePatchFileUtil.checkIfMd5Valid(patchMd5)) {
           TinkerLog.e(TAG, "UpgradePatch tryPatch:onPatchVersionCheckFail md5 %s is valid", patchMd5);
           manager.getPatchReporter().onPatchVersionCheckFail(patchFile, oldInfo, patchMd5);
           return false;
       }
       final boolean usingInterpret = oldInfo.oatDir.equals(ShareConstants.INTERPRET_DEX_OPTIMIZE_PATH);
       if (!usingInterpret && !ShareTinkerInternals.isNullOrNil(oldInfo.newVersion) && oldInfo.newVersion.equals(patchMd5) && !oldInfo.isRemoveNewVersion) {
           TinkerLog.e(TAG, "patch already applied, md5: %s", patchMd5);
           // Reset patch apply retry count to let us be able to reapply without triggering
           // patch apply disable when we apply it successfully previously.
           UpgradePatchRetry.getInstance(context).onPatchResetMaxCheck(patchMd5);
           return true;
       }
       // if it is interpret now, use changing flag to wait main process
       final String finalOatDir = usingInterpret ? ShareConstants.CHANING_DEX_OPTIMIZE_PATH : oldInfo.oatDir;
       newInfo = new SharePatchInfo(oldInfo.oldVersion, patchMd5, isProtectedApp, false, Build.FINGERPRINT, finalOatDir, false);
   } else {
       newInfo = new SharePatchInfo("", patchMd5, isProtectedApp, false, Build.FINGERPRINT, ShareConstants.DEFAULT_DEX_OPTIMIZE_PATH, false);
   }
   // it is a new patch, we first delete if there is any files
   // don't delete dir for faster retry
   // SharePatchFileUtil.deleteDir(patchVersionDirectory);
   final String patchName = SharePatchFileUtil.getPatchVersionDirectory(patchMd5);
   final String patchVersionDirectory = patchDirectory + "/" + patchName;
   TinkerLog.i(TAG, "UpgradePatch tryPatch:patchVersionDirectory:%s", patchVersionDirectory);
   //copy file
   //把补丁包复制到/data/data/com.stan.tinkersdkdemo/tinker/patch-8b79c8cc/patch-8b79c8cc.apk
   File destPatchFile = new File(patchVersionDirectory + "/" + SharePatchFileUtil.getPatchVersionFile(patchMd5));
   try {
       // check md5 first
       if (!patchMd5.equals(SharePatchFileUtil.getMD5(destPatchFile))) {
           SharePatchFileUtil.copyFileUsingStream(patchFile, destPatchFile);
           TinkerLog.w(TAG, "UpgradePatch copy patch file, src file: %s size: %d, dest file: %s size:%d", patchFile.getAbsolutePath(), patchFile.length(),
               destPatchFile.getAbsolutePath(), destPatchFile.length());
       }
   } catch (IOException e) {
       TinkerLog.e(TAG, "UpgradePatch tryPatch:copy patch file fail from %s to %s", patchFile.getPath(), destPatchFile.getPath());
       manager.getPatchReporter().onPatchTypeExtractFail(patchFile, destPatchFile, patchFile.getName(), ShareConstants.TYPE_PATCH_FILE);
       return false;
   }
   //we use destPatchFile instead of patchFile, because patchFile may be deleted during the patch process
   //合成dex
   if (!DexDiffPatchInternal.tryRecoverDexFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile)) {
       TinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, try patch dex failed");
       return false;
   }

   //合成arkhot 针对方舟os
   if (!ArkHotDiffPatchInternal.tryRecoverArkHotLibrary(manager, signatureCheck,
           context, patchVersionDirectory, destPatchFile)) {
       return false;
   }
   //合成so
   if (!BsDiffPatchInternal.tryRecoverLibraryFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile)) {
       TinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, try patch library failed");
       return false;
   }

   //合成resource
   if (!ResDiffPatchInternal.tryRecoverResourceFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile)) {
       TinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, try patch resource failed");
       return false;
   }

   //对dex进行opt优化
   // check dex opt file at last, some phone such as VIVO/OPPO like to change dex2oat to interpreted
   if (!DexDiffPatchInternal.waitAndCheckDexOptFile(patchFile, manager)) {
       TinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, check dex opt file failed");
       return false;
   }

   //把结果重新写入到 patch.info
   if (!SharePatchInfo.rewritePatchInfoFileWithLock(patchInfoFile, newInfo, patchInfoLockFile)) {
       TinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, rewrite patch info failed");
       manager.getPatchReporter().onPatchInfoCorrupted(patchFile, newInfo.oldVersion, newInfo.newVersion);
       return false;
   }

   // Reset patch apply retry count to let us be able to reapply without triggering
   // patch apply disable when we apply it successfully previously.
   UpgradePatchRetry.getInstance(context).onPatchResetMaxCheck(patchMd5);
   TinkerLog.w(TAG, "UpgradePatch tryPatch: done, it is ok");
   return true;
}

这里很简单,就是把你存放的patch差分包复制到/data/data/com.stan.tinkersdkdemo/tinker/patch-8b79c8cc/patch-8b79c8cc.apk中,然后分别执行dex、so 、resources与基准包的合成。

dex、so 、resources的差分与合成有时候的话后续再单独开篇分析下。
参考:

https://blog.csdn.net/lmj623565791/article/details/60874334

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354