@Override
public void onBaseContextAttached(Context base) {
super.onBaseContextAttached(base);
TinkerInstaller.install(this);
}
public static Tinker install(ApplicationLike applicationLike) {
Tinker tinker = new Tinker.Builder(applicationLike.getApplication()).build();
Tinker.create(tinker);
tinker.install(applicationLike.getTinkerResultIntent());
return tinker;
}
複製代碼
在Application中調用TinkerInstaller.install()初始化,在install()中使用Tinker.Builder()建立Tinker對象,而後調用create()設置Tinker對外單例提供,java
this.context = context;
this.mainProcess = TinkerServiceInternals.isInMainProcess(context);
this.patchProcess = TinkerServiceInternals.isInTinkerPatchServiceProcess(context);
this.patchDirectory = SharePatchFileUtil.getPatchDirectory(context);
if (this.patchDirectory == null) {
TinkerLog.e(TAG, "patchDirectory is null!");
return;
}
this.patchInfoFile = SharePatchFileUtil.getPatchInfoFile(patchDirectory.getAbsolutePath());
this.patchInfoLockFile = SharePatchFileUtil.getPatchInfoLockFile(patchDirectory.getAbsolutePath());
}
public Tinker build() {
if (loadReporter == null) {
loadReporter = new DefaultLoadReporter(context);
}
if (patchReporter == null) {
patchReporter = new DefaultPatchReporter(context);
}
if (listener == null) {
listener = new DefaultPatchListener(context);
}
if (tinkerLoadVerifyFlag == null) {
tinkerLoadVerifyFlag = false;
}
return new Tinker(context, status, loadReporter, patchReporter, listener, patchDirectory,
patchInfoFile, patchInfoLockFile, mainProcess, patchProcess, tinkerLoadVerifyFlag);
}
複製代碼
在TinkerBuiler的構造函數中主要完成了Tinker所須要的文件和文件夾的初始化,在build()中初始化loadReporter、patchReporter、listener,這些屬性在Tinker的加載過程當中相當重要。android
TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(),
Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch.patch");
複製代碼
public static void onReceiveUpgradePatch(Context context, String patchLocation) {
Tinker.with(context).getPatchListener().onPatchReceived(patchLocation);
}
複製代碼
public int onPatchReceived(String path) {
File patchFile = new File(path); //建立補丁包文件
//(1)、
int returnCode = patchCheck(path, SharePatchFileUtil.getMD5(patchFile)); // ————————檢查的細節
//(2)
if (returnCode == ShareConstants.ERROR_PATCH_OK) {
TinkerPatchService.runPatchService(context, path);//啓動 TinkerPatchService
} else { //(3)、校驗是失敗調用LoadReporter通知失敗
Tinker.with(context).getLoadReporter().onLoadPatchListenerReceiveFail(new File(path), returnCode);
}
return returnCode;
}
複製代碼
onPatchReceived()中執行如下操做:算法
public static void runPatchService(Context context, 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 {
enqueueWork(context, TinkerPatchService.class, JOB_ID, intent);
} catch (Throwable thr) {
TinkerLog.e(TAG, "run patch service fail, exception:" + thr);
}
}
複製代碼
在runPatchService()中建立Intent保存補丁路徑,調用enqueueWork()方法啓動TinkerPatchService服務,enqueueWork()在TinkerJobIntentService類中,enqueueWork()中根據Android版本的不一樣分別使用JobSchedule和PowerManager執行服務數組
public static void enqueueWork(@NonNull Context context, @NonNull ComponentName component, int jobId, @NonNull Intent work) {
synchronized (sLock) {
WorkEnqueuer we = getWorkEnqueuer(context, component, true, jobId);
we.ensureJobId(jobId);
we.enqueueWork(work);
}
}
static WorkEnqueuer getWorkEnqueuer(Context context, ComponentName cn, boolean hasJobId, int jobId) {
WorkEnqueuer we = sClassWorkEnqueuer.get(cn); // 從緩存中獲取任務
if (we == null) {
if (Build.VERSION.SDK_INT >= 26) {//SDK > 26 使用JobSchedule執行服務
we = new JobWorkEnqueuer(context, cn, jobId);
} else {
we = new CompatWorkEnqueuer(context, cn);
}
sClassWorkEnqueuer.put(cn, we);
}
return we;
}
複製代碼
使用JobSchedule或PowerManager啓動TinkerPatchService後,程序最終執行將調用TinkerPatchService.onHandleWork()緩存
@Override
protected void onHandleWork(Intent intent) {
//(1)進程保活
increasingPriority();
doApplyPatch(this, intent); //(2)執行解析
}
複製代碼
onHandleWork()中作了兩件大事:網絡
private static void doApplyPatch(Context context, Intent intent) {
Tinker tinker = Tinker.with(context);
tinker.getPatchReporter().onPatchServiceStart(intent); //(1)
String path = getPatchPathExtra(intent); //(2)
File patchFile = new File(path); //(3)建立patch文件
//開始時間
long begin = SystemClock.elapsedRealtime();
//(3)
PatchResult patchResult = new PatchResult();
try {
//(4)執行Patch文件解析,此處的upgradePatchProcessor是UpgradePatch的實例
//tryPath中執行文件的複製並返回結果,主要工做都在這
result = upgradePatchProcessor.tryPatch(context, path, patchResult);
} catch (Throwable throwable) {
tinker.getPatchReporter().onPatchException(patchFile, e); //通知異常
}
cost = SystemClock.elapsedRealtime() - begin;
tinker.getPatchReporter().onPatchResult(patchFile, result, cost); //通知解析花費的時長
//(5)保存結果信息在patchResult中
patchResult.isSuccess = result;
patchResult.rawPatchFilePath = path;
patchResult.costTime = cost;
patchResult.e = e;
//(3)
AbstractResultService.runResultService(context, patchResult, getPatchResultExtra(intent));
sIsPatchApplying.set(false);
}
複製代碼
doApplyPatch()中幹了跟多重要的事情:app
@Override
public boolean tryPatch(Context context, String tempPatchPath, PatchResult patchResult) {
Tinker manager = Tinker.with(context);
final File patchFile = new File(tempPatchPath);
//(1)建立檢查器檢查補丁的簽名和TinkerId
ShareSecurityCheck signatureCheck = new ShareSecurityCheck(context);
int returnCode = ShareTinkerInternals.checkTinkerPackage(context, manager.getTinkerFlags(), patchFile, signatureCheck);
//(2)從File文件中讀取字節流,並將字節流轉換成MD5字符串
String patchMd5 = SharePatchFileUtil.getMD5(patchFile);
//(3)保存Md5 的字符串做爲本次補丁文件的版本代號
patchResult.patchVersion = patchMd5;
//獲取的是dataDir/tinker文件路徑(/data/data/com.xxxx.sample/tinker)
final String patchDirectory = manager.getPatchDirectory().getAbsolutePath();
////(4)在tinker文件夾下建立info.lock文件
File patchInfoLockFile = SharePatchFileUtil.getPatchInfoLockFile(patchDirectory);
//(5)在tinker文件夾下建立patch.info文件
File patchInfoFile = SharePatchFileUtil.getPatchInfoFile(patchDirectory);
//(6)讀取文件信息,判斷當前是否存在舊的補丁程序,(每次添加補丁包則會在patch.info和info.lock保存補丁信息)
SharePatchInfo oldInfo = SharePatchInfo.readAndCheckPropertyWithLock(patchInfoFile, patchInfoLockFile);
SharePatchInfo newInfo; //(7)建立SharePatchInfo保存新、舊的補丁版本信息
if (oldInfo != null) { //存在舊的補丁包,處理
//在新的newInfo中保存舊的版本信息
newInfo = new SharePatchInfo(oldInfo.oldVersion, patchMd5, false, Build.FINGERPRINT, finalOatDir);
} else {//第一次執行補丁包
newInfo = new SharePatchInfo("", patchMd5, false, Build.FINGERPRINT, ShareConstants.DEFAULT_DEX_OPTIMIZE_PATH);
}
final String patchName = SharePatchFileUtil.getPatchVersionDirectory(patchMd5); //建立 patch-xxx 文件名
final String patchVersionDirectory = patchDirectory + "/" + patchName; // 建立tinker/patch-xxx 文件
//(8)建立 dataDir/tinker/patch-xxx/patch-xxx.apk文件,將patch文件複製其中
File destPatchFile = new File(patchVersionDirectory + "/" + SharePatchFileUtil.getPatchVersionFile(patchMd5));
try {
// 校驗patch-xxx.apk中生成和的MD5和 patchMd5是否一致,側面檢查內容是否已經複製過此補丁包
if (!patchMd5.equals(SharePatchFileUtil.getMD5(destPatchFile))) {
//(9)執行文件的複製
SharePatchFileUtil.copyFileUsingStream(patchFile, destPatchFile);
}
}
return true;
}
複製代碼
ShareTinkerInternals.checkTinkerPackage()中主要執行和校驗一下內容:ide
public boolean verifyPatchMetaSignature(File path) {
JarFile jarFile = null;
try {
jarFile = new JarFile(path);
final Enumeration<JarEntry> entries = jarFile.entries(); //獲取JarFile中的全部文件
while (entries.hasMoreElements()) { // 遍歷文件
JarEntry jarEntry = entries.nextElement();
final String name = jarEntry.getName();
if (name.startsWith("META-INF/")) {
continue;
}
if (!name.endsWith(ShareConstants.META_SUFFIX)) { // 查找後綴爲meta.txt文件
continue;
}
metaContentMap.put(name, SharePatchFileUtil.loadDigestes(jarFile, jarEntry)); //讀取meta.txt中的內容保存
Certificate[] certs = jarEntry.getCertificates();
}
}
return true;
}
複製代碼
// package_meta.txt 中的信息
* #base package config field
* #Tue Jan 22 18:16:52 CST 2019
* platform=all
* NEW_TINKER_ID=tinker_id_1.0
* TINKER_ID=tinker_id_1.0
* patchMessage=tinker is sample to use
* patchVersion=1.0
public HashMap<String, String> getPackagePropertiesIfPresent() {
String property = metaContentMap.get(ShareConstants.PACKAGE_META_FILE); //從metaContentMap中獲取package_meta.txt
String[] lines = property.split("\n");
for (final String line : lines) {
final String[] kv = line.split("=", 2); //Key、Value保存屬性信息
packageProperties.put(kv[0].trim(), kv[1].trim());
}
return packageProperties;
}
複製代碼
在執行checkTinkerPackage()檢查OK後繼續執行如下流程(具體見代碼註釋): 5. 從File文件中讀取字節流,並將字節流轉換成MD5字符串,此MD5字符串就做爲補丁的版本號 6. 在dataDir/tinker文件路徑下建立info.lock文件、patch.info文件 7. 讀取文件信息,判斷當前是否存在舊的補丁程序,建立SharePatchInfo保存新、舊的補丁版本信息 8. 建立 dataDir/tinker/patch-xxx/patch-xxx.apk文件,將patch文件即補丁包複製其中 9. 校驗patch-xxx.apk中生成和的MD5和 patchMd5是否一致,側面檢查內容是否已經複製過此補丁包函數
if (!DexDiffPatchInternal.tryRecoverDexFiles(manager, signatureCheck, context, patchVersionDirectory, destPatchFile)) {
return false;
}
複製代碼
protected static boolean tryRecoverDexFiles(Tinker manager, ShareSecurityCheck checker, Context context, String patchVersionDirectory, File patchFile) {
String dexMeta = checker.getMetaContentMap().get(DEX_META_FILE); //
boolean result = patchDexExtractViaDexDiff(context, patchVersionDirectory, dexMeta, patchFile);
return result;
}
複製代碼
首先從checker的metaContentMa中獲取assets/dex_meta.txt中內容,由上面的分析知道,metaContentMap中保存着meta.txt文件中的信息,這裏獲取其中的assets/dex_meta.txt中內容,dex_meta中記載着這次修改的補丁中包好的.dex文件學習
private static boolean patchDexExtractViaDexDiff(Context context, String patchVersionDirectory, String meta, final File patchFile) {
String dir = patchVersionDirectory + "/" + DEX_PATH + "/「; //建立tinker/patch-xxxx/dex/
if (!extractDexDiffInternals(context, dir, meta, patchFile, TYPE_DEX)) {//解析assets/dex_meta.txt信息
return false;
}
File dexFiles = new File(dir);
File[] files = dexFiles.listFiles(); //遍歷patch-xxxx/dex/中修復文件列表,找出其中dex、jar、apk的文件
List<File> legalFiles = new ArrayList<>();
if (files != null) {
for (File file : files) {
final String fileName = file.getName();
if (file.isFile()
&& (fileName.endsWith(ShareConstants.DEX_SUFFIX)
|| fileName.endsWith(ShareConstants.JAR_SUFFIX)
|| fileName.endsWith(ShareConstants.PATCH_SUFFIX))
) {
legalFiles.add(file); //legalFiles中保存全部的修復文件
}
}
}
final String optimizeDexDirectory = patchVersionDirectory + "/" + DEX_OPTIMIZE_PATH + "/「; //建立tinker/patch-xxxx/odex/
return dexOptimizeDexFiles(context, legalFiles, optimizeDexDirectory, patchFile); ????
}
複製代碼
在patchDexExtractViaDexDiff()中,首先建立tinker/patch-xxxx/dex/文件夾,而後解析meta中的信息保存在此文件中,而後遍歷dex文件找出其中的dex、jar、apk文件保存在legalFiles集合中,而後執行dexOptimizeDexFiles()方法,在此先看一下如何解析meta信息的;
private static boolean extractDexDiffInternals(Context context, String dir, String meta, File patchFile, int type) {
patchList.clear();
ShareDexDiffPatchInfo.parseDexDiffPatchInfo(meta, patchList); // 遍歷meta中的信息,並封裝在patchList集合中
// 建立 tinker/patch-xxxx/dex/ 目錄
File directory = new File(dir);
Tinker manager = Tinker.with(context);
try {
ApplicationInfo applicationInfo = context.getApplicationInfo();
String apkPath = applicationInfo.sourceDir; //獲取apk的路徑
apk = new ZipFile(apkPath); //建立apk的ZipFile
patch = new ZipFile(patchFile); //建立補丁包 patch-xxx.apk的Zip文件
for (ShareDexDiffPatchInfo info : patchList) { //遍歷patchFile中的文件
if (infoPath.equals("")) {
patchRealPath = info.rawName;
} else {
patchRealPath = info.path + "/" + info.rawName;
}
File extractedFile = new File(dir + info.realName); //在dex目錄下建立每一個dexFile
ZipEntry patchFileEntry = patch.getEntry(patchRealPath); //從補丁包中取出dex文件
ZipEntry rawApkFileEntry = apk.getEntry(patchRealPath); //從apk中取出dex文件
patchDexFile(apk, patch, rawApkFileEntry, patchFileEntry, info, extractedFile);//合併dex
}
}
//dex_meta.txt
classes.dex,,d4a8261c0b1ee8b309ac869fba20e1e1,d4a8261c0b1ee8b309ac869fba20e1e1,dac09804b9bf6728bcca1da866e3e8a4,2491048140,4231045191,jar
test.dex,,56900442eb5b7e1de45449d0685e6e00,56900442eb5b7e1de45449d0685e6e00,0,0,0,jar
//解析dex_meta.txt
public static void parseDexDiffPatchInfo(String meta, ArrayList<ShareDexDiffPatchInfo> dexList) {
String[] lines = meta.split("\n");
for (final String line : lines) {
final String name = kv[0].trim(); //class.dex
final String path = kv[1].trim();
final String destMd5InDvm = kv[2].trim();
final String destMd5InArt = kv[3].trim();
final String dexDiffMd5 = kv[4].trim();
final String oldDexCrc = kv[5].trim();
final String newDexCrc = kv[6].trim();
final String dexMode = kv[7].trim();
ShareDexDiffPatchInfo dexInfo = new ShareDexDiffPatchInfo(name, path, destMd5InDvm, destMd5InArt,
dexDiffMd5, oldDexCrc, newDexCrc, dexMode);
dexList.add(dexInfo);
}
}
複製代碼
總結一下:
private static void patchDexFile(ZipFile baseApk, ZipFile patchPkg, ZipEntry oldDexEntry, ZipEntry patchFileEntry, ShareDexDiffPatchInfo patchInfo, File patchedDexFile) throws IOException {
try {
//獲取apk中要修復文件的輸入流
InputStream oldDexStream = new BufferedInputStream(baseApk.getInputStream(oldDexEntry));
//獲取補丁包中修復文件的輸入流
InputStream patchFileStream = (patchFileEntry != null ? new BufferedInputStream(patchPkg.getInputStream(patchFileEntry)) : null);
final boolean isRawDexFile = SharePatchFileUtil.isRawDexFile(patchInfo.rawName); //判斷patch是不是.dex文件
if (!isRawDexFile || patchInfo.isJarMode) { //處理.jar文件
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(patchedDexFile)));
zos.putNextEntry(new ZipEntry(ShareConstants.DEX_IN_JAR));
if (!isRawDexFile) {
ZipInputStream zis = null;
try {
zis = new ZipInputStream(oldDexStream);
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (ShareConstants.DEX_IN_JAR.equals(entry.getName())) break;
}
new DexPatchApplier(zis, patchFileStream).executeAndSaveTo(zos);
}
} else {
new DexPatchApplier(oldDexStream, patchFileStream).executeAndSaveTo(zos);
}
}
} else {
new DexPatchApplier(oldDexStream, patchFileStream).executeAndSaveTo(patchedDexFile); //處理.dex文件
}
}
}
複製代碼
//從Intent中獲取設置的DefaultTinkerResultService類名,使用JibSchedule執行服務
AbstractResultService.runResultService(context, patchResult, getPatchResultExtra(intent));//從Intent中獲取
複製代碼
public void onPatchResult(PatchResult result) {
//中止TinkerPatchService服務進程
TinkerServiceInternals.killTinkerPatchServiceProcess(getApplicationContext());
if (result.isSuccess) {
deleteRawPatchFile(new File(result.rawPatchFilePath)); //rawPatchFilePath爲保存的是傳入時的Patch路徑
if (checkIfNeedKill(result)) {
android.os.Process.killProcess(android.os.Process.myPid()); //殺死當前進程
}
}
}
複製代碼
在獲取和解析補丁包後系統會重啓,在重啓時會執行Application的onBaseContextAttached(),在TinkerApplication的onBaseContextAttached中調用loadTinker(),loadTinker中經過反射執行TinkerLoader的tryLoad()方法:
private void onBaseContextAttached(Context base) {
loadTinker();
}
private static final String TINKER_LOADER_METHOD = "tryLoad";
private void loadTinker() {
try {
Class<?> tinkerLoadClass = Class.forName(loaderClassName, false, getClassLoader());
Method loadMethod = tinkerLoadClass.getMethod(TINKER_LOADER_METHOD, TinkerApplication.class);
Constructor<?> constructor = tinkerLoadClass.getConstructor();
tinkerResultIntent = (Intent) loadMethod.invoke(constructor.newInstance(), this);
}
}
複製代碼
public Intent tryLoad(TinkerApplication app) {
tryLoadPatchFilesInternal(app, resultIntent);
return resultIntent;
}
複製代碼
//(1)檢查並獲取工做目錄dataDir/tinker文件
File patchDirectoryFile = SharePatchFileUtil.getPatchDirectory(app);
String patchDirectoryPath = patchDirectoryFile.getAbsolutePath();
//(2)檢查獲取tinker/patch.info 和 info.lock文件
File patchInfoFile = SharePatchFileUtil.getPatchInfoFile(patchDirectoryPath);
File patchInfoLockFile = SharePatchFileUtil.getPatchInfoLockFile(patchDirectoryPath);
//(3)從patch.info 和 info.lock兩個文件中讀取保存的PatchInfo信息
patchInfo = SharePatchInfo.readAndCheckPropertyWithLock(patchInfoFile, patchInfoLockFile);
//patch-641e634c
String patchName = SharePatchFileUtil.getPatchVersionDirectory(version);
//獲取 tinker/patch-641e634c
String patchVersionDirectory = patchDirectoryPath + "/" + patchName;
//(4)檢查獲取補丁包目錄 tinker/patch-641e634c
File patchVersionDirectoryFile = new File(patchVersionDirectory);
//(5)檢查獲取保存拷貝內容的 tinker/patch-641e634c/patch-641e634c.apk
final String patchVersionFileRelPath = SharePatchFileUtil.getPatchVersionFile(version);
File patchVersionFile = (patchVersionFileRelPath != null ? new File(patchVersionDirectoryFile.getAbsolutePath(), patchVersionFileRelPath) : null);
//(6) 校驗patch-641e634c.apk文件 (重點 :檢驗簽名)
ShareSecurityCheck securityCheck = new ShareSecurityCheck(app); ////和下發時校驗同樣校驗patch-641e634c.apk
int returnCode = ShareTinkerInternals.checkTinkerPackage(app, tinkerFlag, patchVersionFile, securityCheck);
//(7)檢查meta文件中記錄的dex文件是否存在,並將結果保存在loadDexList集合中
boolean dexCheck = TinkerDexLoader.checkComplete(patchVersionDirectory, securityCheck, oatDex, resultIntent);
//處理DexLoader
if (isEnabledForDex) {
//執行dex文件的加載
boolean loadTinkerJars = TinkerDexLoader.loadTinkerJars(app, patchVersionDirectory, oatDex, resultIntent, isSystemOTA);
}
}
複製代碼
上面的整個流程主要執行拿到上面解析保存的patch文件,通過系列檢驗後加載合併全部的dex文件,具體細節以下:
public static boolean checkComplete(String directory, ShareSecurityCheck securityCheck, String oatDir, Intent intentResult) {
//(1)從metaContentMap中獲取assets/dex_meta.txt文件中的字符串
String meta = securityCheck.getMetaContentMap().get(DEX_MEAT_FILE);
ArrayList<ShareDexDiffPatchInfo> allDexInfo = new ArrayList<>();
ShareDexDiffPatchInfo.parseDexDiffPatchInfo(meta, allDexInfo);// (2)解析meta中的信息保存在allDexInfo集合中
HashMap<String, String> dexes = new HashMap<>();
for (ShareDexDiffPatchInfo info : allDexInfo) { //(3)遍歷allDexInfo集合
if (isJustArtSupportDex(info)) {
continue;
}
if (isVmArt && info.rawName.startsWith(ShareConstants.TEST_DEX_NAME)) {
testInfo = info; //獲取test.dex
} else if (isVmArt && ShareConstants.CLASS_N_PATTERN.matcher(info.realName).matches()) {
classNDexInfo.add(info); //保存.jar文件
} else {
dexes.put(info.realName, getInfoMd5(info));
loadDexList.add(info); //保存補丁的文件信息
}
}
if (isVmArt
&& (testInfo != null || !classNDexInfo.isEmpty())) {
if (testInfo != null) {
classNDexInfo.add(ShareTinkerInternals.changeTestDexToClassN(testInfo, classNDexInfo.size() + 1));
}
dexes.put(ShareConstants.CLASS_N_APK_NAME, "");
}
return true;
}
複製代碼
public static boolean loadTinkerJars(final TinkerApplication application, String directory, String oatDir, Intent intentResult, boolean isSystemOTA) {
//獲取系統加載的ClassLoader
PathClassLoader classLoader = (PathClassLoader) TinkerDexLoader.class.getClassLoader();
//在tinker/patch-641e634c/下建立dex文件夾
String dexPath = directory + "/" + DEX_PATH + "/";
ArrayList<File> legalFiles = new ArrayList<>();
for (ShareDexDiffPatchInfo info : loadDexList) {
//忽略僅支持art的文件
if (isJustArtSupportDex(info)) {
continue;
}
//遍歷文件集合,
String path = dexPath + info.realName;
File file = new File(path);
legalFiles.add(file); //遍歷集合找到dex/目錄下要加載的File集合
}
}
SystemClassLoaderAdder.installDexes(application, classLoader, optimizeDir, legalFiles); //執行ClassLoader加載
}
複製代碼
installDexes()方法中根據不一樣的版本,調用對應的install()方法執行加載,在install()中使用ClassLoader加載每一個文件中的dex文件,而後將獲取到的Elements合併在本來程序類的前面,達到替換原來類文件的目的,關於ClassLoader的合併和加載參見Android熱修復之路(一)——ClassLoader
private static void install(ClassLoader loader, List<File> additionalClassPathEntries, File optimizedDirectory) {
Field pathListField = ShareReflectUtil.findField(loader, "pathList");
Object dexPathList = pathListField.get(loader); //獲取loader中的pathList
ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
//先反射調用dexPathList中的makePathElements()將List<File>解析成Elements數組
//將獲取到的解析成Elements數組合並原來的數組,而後設置爲dexPathList
ShareReflectUtil.expandFieldArray(dexPathList, "dexElements", makePathElements(dexPathList,
new ArrayList<File>(additionalClassPathEntries), optimizedDirectory,
suppressedExceptions));
}
複製代碼
整個Tinker的執行流程就到此結束了,也讓我從源碼的角度理解了Tinker的強大,但願本篇能對想深刻學習Tinker的同窗有所幫助。