之前公司項(xiàng)目使用的是JSPatch老的版本做的熱更(最新的要收費(fèi))嵌灰,每次發(fā)布補(bǔ)丁的時(shí)候都要做改動(dòng)方法或者類的OC代碼轉(zhuǎn)成JS代碼的修正,頭有點(diǎn)大羊初,改個(gè)小bug調(diào)試起來(lái)也比較麻煩犯助。于是想著能不能不通過(guò)JSCore下發(fā)的方式做熱更呢住诸,去github上面搜了一波,無(wú)意中找到了基于二進(jìn)制熱更的方案箱亿,配置好后不用再做代碼的轉(zhuǎn)換和翻譯跛锌,直接生成二進(jìn)制文件,當(dāng)補(bǔ)丁用就把bug給修復(fù)了届惋。
先放下二進(jìn)制熱更的原理圖
經(jīng)過(guò)實(shí)踐髓帽,我在本地和github下載的zip包都實(shí)現(xiàn)了代碼更新的效果,新手接入的時(shí)候有幾個(gè)注意的地方:
1.將OCRunner Pod下來(lái)
pod 'OCRunner' #支持所有架構(gòu)脑豹,包含libffi.a
# 或者
pod 'OCRunnerArm64' #僅支持 arm64和arm64e郑藏,沒(méi)有l(wèi)ibffi.a
2.在項(xiàng)目中添加需要熱更的文件夾HotPath,把熱更的資源放到這個(gè)文件夾下瘩欺。在demo中我將這個(gè)庫(kù)弄成一個(gè)模塊OCRunnerLib译秦,以AppDelegate+OCRunner分類的形式引入了,方便管理击碗。
3.添加HotPath目錄下資源生成binarypatch二進(jìn)制文件和jsonpatch的腳本筑悴,對(duì)應(yīng)改下項(xiàng)目名,并添加存放內(nèi)聯(lián)函數(shù)稍途、預(yù)編譯函數(shù)阁吝、C函數(shù)的轉(zhuǎn)義,否則會(huì)出現(xiàn)不支持OC的一些屬性設(shè)置和系統(tǒng)函數(shù)方法的Scripts.bundle械拍,具體可以看下GCDRefrences和UIKitRefrences突勇,對(duì)應(yīng)自己項(xiàng)目用到的少哪個(gè)加哪個(gè)就行。
PatchGeneratorBinary
$SRCROOT/OCRunner_Use/PatchGenerator -files $SRCROOT/OCRunner_Use/HotPath -refs $SRCROOT/OCRunner_Use/Scripts.bundle -output $SRCROOT/OCRunner_Use/binarypatch
PatchGeneratorJson
$SRCROOT/OCRunner_Use/PatchGenerator -files $SRCROOT/OCRunner_Use/HotPath -refs $SRCROOT/OCRunner_Use/Scripts.bundle -output $SRCROOT/OCRunner_Use/jsonpatch -type json
4.對(duì)項(xiàng)目熱更的VC做改動(dòng)坷虑,想改哪個(gè)類就把類名設(shè)置一樣即可甲馋,這里作者沒(méi)有說(shuō)明,我也是自己摸索好長(zhǎng)時(shí)間才知道規(guī)律的迄损。想改哪個(gè)方法重寫就可以了定躏,可新增類,也可修改原有類。注意痊远,新增類要把這個(gè).m文件加到build phases->compile sources中垮抗,否則識(shí)別不到類中的方法出現(xiàn)閃退的情況,而修改的類不用碧聪,否則編譯器會(huì)報(bào)錯(cuò)冒版。
5.build成功后把binarypatch二進(jìn)制上傳服務(wù)器,app加載二進(jìn)制熱更逞姿。以下是我在demo中做的本地和服務(wù)器(我沒(méi)有服務(wù)器就用上傳github的zip下載做了)辞嗡。測(cè)試一波完成了HotPath改動(dòng)的更新,不用再做代碼的轉(zhuǎn)換滞造,對(duì)coder更友好了续室。
- (void)loadOCRunner {
[ORSystemFunctionPointerTable reg:@"CGPointEqualToPoint" pointer:&CGPointEqualToPoint];
[ORSystemFunctionPointerTable reg:@"CGSizeEqualToSize" pointer:&CGSizeEqualToSize];
#if 0
NSString *finalPath = [[NSBundle mainBundle] pathForResource:@"binarypatch" ofType:nil];
[ORInterpreter excuteBinaryPatchFile:finalPath];
#else
//若補(bǔ)丁已經(jīng)下載好并解壓了,就讀取本地的補(bǔ)丁断部,避免每次都去下載新的補(bǔ)丁
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSString *finalPath = [[documentsDirectoryURL.absoluteString stringByAppendingPathComponent:@"binarypatch"] stringByReplacingOccurrencesOfString:@"file:" withString:@""];
if ([[NSFileManager defaultManager] fileExistsAtPath:finalPath]) {
[ORInterpreter excuteBinaryPatchFile:finalPath];
return;
}
//創(chuàng)建信號(hào)量猎贴,保證先把補(bǔ)丁包下載下來(lái)再進(jìn)入主頁(yè)面班缎,若補(bǔ)丁包大的話蝴光,可以考慮不阻塞主線程,給予等待loding顯示
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
manager.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSURL *URL = [NSURL URLWithString:@"https://github.com/Charles2016/OCRunner_Use/raw/master/OCRunner_Use/OCRunnerLib/ConfigPath/binarypatch_Use.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
NSString *filePathStr = filePath.relativePath;
NSString *toPath = [filePathStr stringByDeletingLastPathComponent];
NSString *finalPath = [NSString stringWithFormat:@"%@/binarypatch", toPath];
//先移除已存在的解壓包达址,以防解壓過(guò)后的文件干擾到蔑祟,取不到最新的二進(jìn)制文件
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:finalPath]) {
[fileManager removeItemAtPath:finalPath error:nil];
}
[SSZipArchive unzipFileAtPath:filePathStr toDestination:toPath progressHandler:nil completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
NSLog(@"File unzip to: %@", finalPath);
[ORInterpreter excuteBinaryPatchFile:finalPath];
dispatch_semaphore_signal(semaphore);
}];
}];
[downloadTask resume];
//信號(hào)量等待
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
#endif
}
以上就是二進(jìn)制熱更的實(shí)踐了,測(cè)試起來(lái)感覺(jué)還不錯(cuò)沉唠。服務(wù)器下載zip的話可以與后臺(tái)協(xié)商疆虚,覺(jué)得可以弄個(gè)灰度下發(fā)之類的操作,版本號(hào)相關(guān)配置判斷满葛,想讓哪臺(tái)手機(jī)哪個(gè)版本更新都可以了径簿,看作者說(shuō)明是可以過(guò)App Store的審核,沒(méi)有具體上傳過(guò)嘀韧,需要進(jìn)一步驗(yàn)證篇亭,希望能幫到你。本篇GitHubDemo傳送陣在此锄贷。
感謝作者@SilverFruity/OCRunner的開(kāi)源封裝译蒂。