Tinker源码分析四

2016-11-03  本文已影响410人  Allenlll

一、UpgradePatch类
tryPatch(Context context, String tempPatchPath, PatchResult patchResult)方法

getPatchInfoLockFile: /data/data/tinker.sample.android/tinker/info.lock

二、DexDiffPatchInternal类

第一种对应完全新增的dex,第二种对应本来没变化,但是由于其他dex的变化需要打入这个dex里引用了那个变化了的dex的类

final String infoPath = info.path;
String patchRealPath;
if(infoPath.equals("")) {
    patchRealPath = info.rawName;} 
else { 
   patchRealPath = info.path + "/" + info.rawName;
}

InputStream oldInputStream = apk.getInputStream(rawApkFileEntry); InputStream newInputStream = patch.getInputStream(patchFileEntry);

SmallPatchedDexItemFile : /data/data/tinker.sample.android/tinker/patch-931e8e6c/dex/smallpatch_info.ddextra

new DexPatchApplier(oldInputStream, oldDexSize, newInputStream,
 smallPatchInfoFile).executeAndSaveTo(zos);
public DexPatchApplier(
        InputStream oldDexIn,
        int initDexSize,
        InputStream patchFileIn,
        SmallPatchedDexItemFile extraInfoFile) throws IOException {
        this(
            new Dex(oldDexIn, initDexSize), 
           (patchFileIn != null ? new DexPatchFile(patchFileIn) : null),
            extraInfoFile
    );
}

this.extraInfoFile = SmallPatchedDexItemFile;

if (extraInfoFile == null || !extraInfoFile.isAffectedOldDex(this.oldDexSignStr)) {
    patchedToc.stringIds.off
            = this.patchFile.getPatchedStringIdSectionOffset();
} else {
    patchedToc.stringIds.off =
 this.extraInfoFile.getPatchedStringIdOffsetByOldDexSign(oldDexSignStr);

dex如果为空,则使用patchFile来计算位置偏移。

if (extraAddedDexElementsFile == null) { 
   this.patchedDex = new Dex(patchFileIn.getPatchedDexSize());
} else {
    this.patchedDex = new Dex(
            extraAddedDexElementsFile.getPatchedDexSizeByOldDexSign(this.oldDexSignStr) 
   );
}

得到extracted文件之后

files = {File[2]@4819} 
 0 = {File@4822} "/data/data/tinker.sample.android/tinker/patch-931e8e6c/dex/classes.dex.jar"
 1 = {File@4823} "/data/data/tinker.sample.android/tinker/patch-931e8e6c/dex/test.dex.jar"

三、ShareDexDiffPatchInfo
class.dex

还会有一个test.dex:

oldDexCrC=0时,完全新增的dex。调用extractDexToJar方法

test.dex是测试插入是否第一次的,有些手机可以插入dex,但不会生效。

上一篇 下一篇

猜你喜欢

热点阅读