Tinker源码分析一

从加载流程来分析

处理下载之后的修复包:

/** 
* new patch file to install, try install them with :patch process
 * Generally you will not use it
 * 
* @param context 
* @param patchLocation 
*/
public static void onReceiveUpgradePatch(Context context, String patchLocation) {   

 Tinker.with(context).getPatchListener().onPatchReceived(patchLocation, true);

}

调用TinkerInstalleronReceiveUpgradePatch方法,在这个方法里面初始化Tinker类,并且调用PatchListeneronPatchReceived(String path, boolean isUpgrade)方法。

private Tinker(Context context, int tinkerFlags, LoadReporter loadReporter, PatchReporter patchReporter,               PatchListener listener, File patchDirectory, File patchInfoFile,               boolean isInMainProc, boolean isPatchProcess, boolean tinkerLoadVerifyFlag) {    
    this.context = context; 
    this.listener = listener;
    this.loadReporter = loadReporter;
    this.patchReporter = patchReporter;
    this.tinkerFlags = tinkerFlags;
    this.patchDirectory = patchDirectory;
    this.patchInfoFile = patchInfoFile;
    this.isMainProcess = isInMainProc;
    this.tinkerLoadVerifyFlag = tinkerLoadVerifyFlag;
    this.isPatchProcess = isPatchProcess;
}

其中:patchDirectory是patch包的存储位置。patchInfoFile是存储Patch包版本等信息的文件。

一、对修复包的第一层验证

这是Tinker 的初始化方法,初始化了LoaderReporterPatchReporter
PatchListener等。

值得注意的是Tinker初始化之前必须得先调用TinkerInstaller的install方法,这个后面再说。

/** 
* when we receive a patch, what would we do? 
* you can overwrite it
 * 
* @param path 
* @param isUpgrade 
* @return 
*/
@Overridepublic int onPatchReceived(String path, boolean isUpgrade) {
     int returnCode = patchCheck(path, isUpgrade);    
     if (returnCode == ShareConstants.ERROR_PATCH_OK) {  
         TinkerPatchService.runPatchService(context, path, isUpgrade);   
      } else {         
       Tinker.with(context).getLoadReporter().
       onLoadPatchListenerReceiveFail(new File(path), returnCode, isUpgrade);   
      }    
    return returnCode;
}

DefaultPatchListener中,对修复包进行检查,如果修复包ok,则开启一个单独的进程合成全量包。如果修复包不完整或者有其它问题,则调用LoadReporteronLoadPatchListenerReceiveFail(File patchFile, int errorCode, boolean isUpgrade)方法通知。

patchCheck验证了:

  • Tinker开关是否开启
  • 修复文件是否存在
  • 是不是通过合成进程执行的操作
  • 合成进程是否正在执行

第二、开启合成进程

public static void runPatchService(Context context, String path, boolean isUpgradePatch) {
    Intent intent = new Intent(context, TinkerPatchService.class);
    intent.putExtra(PATCH_PATH_EXTRA, path);
    intent.putExtra(PATCH_NEW_EXTRA, isUpgradePatch);
    context.startService(intent);
}

TinkerPatchService中开启合成进程的服务。它是一个IntentSerivce,是在单独的线程中进行的操作。下面来看一下onHandleIntent中的操作。

@Overrideprotected void onHandleIntent(Intent intent) {
    final Context context = getApplicationContext();
    Tinker tinker = Tinker.with(context);
    tinker.getPatchReporter().onPatchServiceStart(intent);
    //调用patchReporter的onPatchServiceStart(Intent 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);
    boolean isUpgradePatch = getPatchUpgradeExtra(intent);
    long begin = SystemClock.elapsedRealtime();
    boolean result;
    long cost;
    Throwable e = null;
    increasingPriority();
    PatchResult patchResult = new PatchResult();
    try {
        if (isUpgradePatch) {
            if (upgradePatchProcessor == null) {
                throw new TinkerRuntimeException("upgradePatchProcessor is null.");
            }
            result = upgradePatchProcessor.tryPatch(context, path, patchResult);
        } else { 
           //just recover from exist patch
            if (repairPatchProcessor == null) {
                throw new TinkerRuntimeException("upgradePatchProcessor is null.");
            }
            result = repairPatchProcessor.tryPatch(context, path, patchResult);
        }
    } catch (Throwable throwable) {
        e = throwable;
        result = false;
        tinker.getPatchReporter().onPatchException(patchFile, e, isUpgradePatch);
    }
    cost = SystemClock.elapsedRealtime() - begin; 
   tinker.getPatchReporter(). 
       onPatchResult(patchFile, result, cost, isUpgradePatch); 
   patchResult.isSuccess = result;
    patchResult.isUpgradePatch = isUpgradePatch;
    patchResult.rawPatchFilePath = path;
    patchResult.costTime = cost;
    patchResult.e = e;
    AbstractResultService.runResultService(context, patchResult);
}

有几个操作需要注意的:

  • 调用patchReporteronPatchServiceStart(Intent intent)方法通知。

  • 让服务置于前台服务increasingPriority

    1.startForeground(int id, Notification notification)
    这个方法,可以让后台服务置于前台,就像音乐播放器的,播放服务一样,不会被系统杀死。
    2.开启一个InnerService降低被杀死的概率。

  • TinkerInstaller调用的install方法,会初始化两个AbstractPatchReparePatchUpgradePatch。在这里会调用它们的tryPatch(Context context, String tempPatchPath, PatchResult patchResult)方法来执行合并操作。并将结果返回。

  • 如果两个AbstractPatch为空,则会捕获异常。并且调用PatchReporteronPatchException来通知。

  • 修复成功后调用PatchReporteronPatchResult()来通知,有花费时间等信息。

  • 调用AbstactResultServicerunResultService(Context context, PatchResult result)方法。也是在TinkerInstallerInstall的时候赋值。也是一个IntentService

这个Service会将补丁合成进程返回的结果返回给主进程,在单独的线程中执行。onHandleIntent中只是回调了runResultService(Context context, PatchResult result)方法。通过IntentService完成了进程间的通信。

第三、最后的合成方法
为了不至于离的太远,我们还顺着刚才的流程分析AbstractPatchtryPatch方法。我们先看下UpgradePatch

 @Override
    public boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult) {
        Tinker manager = Tinker.with(context);
        final File patchFile = new File(tempPatchPath);
        if (!manager.isTinkerEnabled() || !ShareTinkerInternals.isTinkerEnableWithSharedPreferences(context)) {
            TinkerLog.e(TAG, "UpgradePatch tryPatch:patch is disabled, just return");
            return false;
        }
        if (!patchFile.isFile() || !patchFile.exists()) {
            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, true, returnCode);
            return false;
        }
        patchResult.patchTinkerID = signatureCheck.getNewTinkerID();
        patchResult.baseTinkerID = signatureCheck.getTinkerID();
        //it is a new patch, so we should not find a exist
        SharePatchInfo oldInfo = manager.getTinkerLoadResultIfPresent().patchInfo;
        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;
        SharePatchInfo newInfo;
        //already have patch
        if (oldInfo != null) { 
           if (oldInfo.oldVersion == null || oldInfo.newVersion == null) {
                TinkerLog.e(TAG, "UpgradePatch tryPatch:onPatchInfoCorrupted");
                manager.getPatchReporter().onPatchInfoCorrupted(patchFile, oldInfo.oldVersion, oldInfo.newVersion, true);
                return false;
            }
            if (oldInfo.oldVersion.equals(patchMd5) || oldInfo.newVersion.equals(patchMd5)) {
                TinkerLog.e(TAG, "UpgradePatch tryPatch:onPatchVersionCheckFail");
                manager.getPatchReporter().onPatchVersionCheckFail(patchFile, oldInfo, patchMd5, true); 
               return false;
            }
            newInfo = new SharePatchInfo(oldInfo.oldVersion, patchMd5);        } else {
            newInfo = new SharePatchInfo("", patchMd5);
        }
        //check ok, we can real recover a new patch
        final String patchDirectory = manager.getPatchDirectory().getAbsolutePath();
        TinkerLog.i(TAG, "UpgradePatch tryPatch:dexDiffMd5:%s", patchMd5);
        final String patchName = SharePatchFileUtil.getPatchVersionDirectory(patchMd5); 
       final String patchVersionDirectory = patchDirectory + "/" + patchName; 
       TinkerLog.i(TAG, "UpgradePatch tryPatch:patchVersionDirectory:%s", patchVersionDirectory);
        //it is a new patch, we first delete if there is any files
        //don't delete dir for faster retry//
        SharePatchFileUtil.deleteDir(patchVersionDirectory); 
       //copy file 
       File destPatchFile = new File(patchVersionDirectory + "/" + SharePatchFileUtil.getPatchVersionFile(patchMd5));
        try {
            SharePatchFileUtil.copyFileUsingStream(patchFile, destPatchFile);            TinkerLog.w(TAG, "UpgradePatch after %s size:%d, %s size:%d", patchFile.getAbsolutePath(), patchFile.length(),                destPatchFile.getAbsolutePath(), destPatchFile.length());
        } catch (IOException e) {
//            e.printStackTrace();
            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, true);
            return false;
        }
        //we use destPatchFile instead of patchFile, because patchFile may be deleted during the patch process
        if (!DexDiffPatchInternal.tryRecoverDexFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile, true)) {            TinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, try patch dex failed");
            return false;
        }
        if (!BsDiffPatchInternal.tryRecoverLibraryFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile, true)) {            TinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, try patch library failed"); 
           return false;
        }
        if (!ResDiffPatchInternal.tryRecoverResourceFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile, true)) {            TinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, try patch resource failed");
            return false; 
       } 
       final File patchInfoFile = manager.getPatchInfoFile();
        if (!SharePatchInfo.rewritePatchInfoFileWithLock(patchInfoFile, newInfo, SharePatchFileUtil.getPatchInfoLockFile(patchDirectory))) {            TinkerLog.e(TAG, "UpgradePatch tryPatch:new patch recover, rewrite patch info failed"); 
           manager.getPatchReporter().onPatchInfoCorrupted(patchFile, newInfo.oldVersion, newInfo.newVersion, true); 
           return false;
        }
        TinkerLog.w(TAG, "UpgradePatch tryPatch: done, it is ok");        return true; 
   }

方法有点长, 我们一步一步分析,里面有几个点需要注意。

  • ShareSecurityCheck是检查签名的类,里面封闭了签名检查的方法
  • ShareTinkerInternals的静态方法checkTinkerPackage(Context context, int tinkerFlag, File patchFile, ShareSecurityCheck securityCheck)传入了ShareSecurityCheck,它代理了:
    1.签名检查
    2.TinkerId检查
    3.Tinker开关检查,dex、resource、librirymeta信息检查。
    如果检查失败通过PatchReporter抛出onPatchPackageCheckFail
    -SharePatchInfo,存储了修复包的版本信息,有oldVersionnewVersionnewVersion是修复包的md5值。
    4.什么时候oldInfo会存在呢?加载成功过一次,也修复成功了。再次执行合成的时候。如果下载的包还是之前的包,则会报告onPatchVersionCheckFail。如果是新的修复包,则会把 oldVersion赋值给 SharePatchInfo(String oldVer, String newVew)中的ondVer。
  • 拷贝修复包到data/data目录
  • DexDiff合成dex、BsDiff合成library、BsDiff合成res。
  • 拷贝SharePatchInfoPatchInfoFile中,PatchInfoFileTinkerInstallerinstall方法中初始化。

稍后再分别分析patch包的资源合成操作。先分析一下最早调用的TinkerInstallerinstall方法。

四、安装方法
TinkerInstaller类中有两个安装方法一个是默认的,一个是自定义属性的。

/**
 * install tinker with default config, you must install tinker before you use their api
 * or you can just use {@link TinkerApplicationHelper}'s api 
* 
* @param applicationLike 
*/
public static void install(ApplicationLike applicationLike) {
    Tinker tinker = new Tinker.Builder(applicationLike.getApplication()).build();
    Tinker.create(tinker);
    tinker.install(applicationLike.getTinkerResultIntent());
}

这个是默认的方法,自定义方法只不过install的参数传入了自己自定义的一些类。

/**
 * install tinker with custom config, you must install tinker before you use their api
 * or you can just use {@link TinkerApplicationHelper}'s api *
 * @param applicationLike 
* @param loadReporter
 * @param patchReporter 
* @param listener
 * @param resultServiceClass 
* @param upgradePatchProcessor 
* @param repairPatchProcessor
 */
public static void install(ApplicationLike applicationLike, LoadReporter loadReporter, PatchReporter patchReporter,                           PatchListener listener, Class<? extends AbstractResultService> resultServiceClass,                           AbstractPatch upgradePatchProcessor, AbstractPatch repairPatchProcessor) {
    Tinker tinker = new Tinker.Builder(applicationLike.getApplication())        .tinkerFlags(applicationLike.getTinkerFlags())
        .loadReport(loadReporter)
        .listener(listener)
        .patchReporter(patchReporter) 
       .tinkerLoadVerifyFlag(applicationLike.getTinkerLoadVerifyFlag()).build();
    Tinker.create(tinker);
    tinker.install(applicationLike.getTinkerResultIntent(), resultServiceClass,
 upgradePatchProcessor, repairPatchProcessor);
}

传入的参数有:

  • ApplicationLike应用的代理application
  • LoadReporter加载合成的包的报告类
  • PatchReporter打修复包过程中的报告类
  • PatchListener对修复包最开始的检查
  • ResultService 从合成进程取合成结果
  • UpgradePatchProcessor 生成一个新的patch合成包
  • ReparePatchProcessor修复上一次合成失败的修复包

下面看一下Tinker中核心的install方法

/**
 * you must install tinker first!! 
*
 * @param intentResult 
* @param serviceClass 
* @param upgradePatch
 * @param repairPatch 
*/public void install(Intent intentResult, Class<? extends 
AbstractResultService> serviceClass,
                    AbstractPatch upgradePatch, AbstractPatch repairPatch) {
    sInstalled = true;
    AbstractResultService.setResultServiceClass(serviceClass);
    TinkerPatchService.setPatchProcessor(upgradePatch, repairPatch);
    if (!isTinkerEnabled()) {
        TinkerLog.e(TAG, "tinker is disabled");
        return;
    }
    if (intentResult == null) {
        throw new TinkerRuntimeException("intentResult must not be null.");
    }
    tinkerLoadResult = new TinkerLoadResult(); 
   tinkerLoadResult.parseTinkerResult(getContext(), intentResult);
    //after load code set
    loadReporter.onLoadResult(patchDirectory, tinkerLoadResult.loadCode, tinkerLoadResult.costTime);
    if (!loaded) {
        TinkerLog.w(TAG, "tinker load fail!");
    }}
  • 设置自定义的ResultService
  • 设置自定义的UpgradePatchReparePatch
  • 创建TinkerLoadResult调用parseTinkerResult(Context context, Intent intentResult)解析上次合成之后的信息:花费时间,返回值等。
  • 调用LoaderReporteronLoadResult方法,通知,加载结果。

分析到些结束,下一篇将分析一下具体的合成过程。

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

推荐阅读更多精彩内容