热修复 tinker接入及源码分析( 九 )

  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    比较清晰,主要关注r.方法,调用的是. 。ps:这里有个有意思的地方(),其内部实现为:
    private void increasingPriority() {TinkerLog.i(TAG, "try to increase patch process priority");try {Notification notification = new Notification();if (Build.VERSION.SDK_INT < 18) {startForeground(notificationId, notification);} else {startForeground(notificationId, notification);// start InnerServicestartService(new Intent(this, InnerService.class));}} catch (Throwable e) {TinkerLog.i(TAG, "try to increase patch process priority error:" + e);}}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15

      如果你对“保活”这个话题比较关注,那么对这段代码一定不陌生,主要是利用系统的一个漏洞来启动一个前台 。如果有兴趣,可以参考此文:关于进程保活,你所需要知道的一切 。
      下面继续回到方法:
      # UpgradePatch@Overridepublic boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult) {Tinker manager = Tinker.with(context);final File patchFile = new File(tempPatchPath);//it is a new patch, so we should not find a existSharePatchInfo oldInfo = manager.getTinkerLoadResultIfPresent().patchInfo;String patchMd5 = SharePatchFileUtil.getMD5(patchFile);//use md5 as versionpatchResult.patchVersion = patchMd5;SharePatchInfo newInfo;//already have patchif (oldInfo != null) {newInfo = new SharePatchInfo(oldInfo.oldVersion, patchMd5, Build.FINGERPRINT);} else {newInfo = new SharePatchInfo("", patchMd5, Build.FINGERPRINT);}//check ok, we can real recover a new patchfinal String patchDirectory = manager.getPatchDirectory().getAbsolutePath();final String patchName = SharePatchFileUtil.getPatchVersionDirectory(patchMd5);final String patchVersionDirectory = patchDirectory + "/" + patchName;//copy fileFile destPatchFile = new File(patchVersionDirectory + "/" + SharePatchFileUtil.getPatchVersionFile(patchMd5));// check md5 firstif (!patchMd5.equals(SharePatchFileUtil.getMD5(destPatchFile))) {SharePatchFileUtil.copyFileUsingStream(patchFile, destPatchFile);}//we use destPatchFile instead of patchFile, because patchFile may be deleted during the patch processif (!DexDiffPatchInternal.tryRecoverDexFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile)) {TinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, try patch dex failed");return false;}return true;}