一、概述
1.uni-app打包成apk或wgt文件,使用plus.runtime.getProperty方法獲取本地應用資源版本號。
2.調用后端接口,拿到與后端規定好的版本號,與前面獲取的版本號進行對比,如果不一致(也可以判斷當前版本號是否小於線上版本號,也就是最新版本號),進行下一步操作。
3.uni.showModal或其他彈出框彈出下載提示。
4.使用plus.downloader.createDownload下載安裝包。下載完可以使用plus.runtime.openFile(path),打開文件安裝。
5.另一種方法是去瀏覽器下載,使用plus.runtime.openURL(url); 打開一個網址去下載。
6.推薦的方法是使用wgt熱更新,這樣就不用每次更改都需要對ios包進行加簽。這種方法着重記一下。
以上方法詳情詳見:plus文檔地址
二、apk整包更新
HbuilderX App選擇發行=>原生App-雲打包
HbuilderX App雲打包,證書可以由公司安卓開發人員提供,打安卓包也可以使用公共測試證書進行測試。打包完成會返回一個下載地址,在瀏覽器下載即可。將下載后的安卓包上傳到公司的服務器下載地址。
前端代碼
前端代碼(APP.vue)創建下載資源的下載任務並彈出相應提示信息的彈窗,這里使用plus的api進行操作。
// #ifdef APP-PLUS const dtask = plus.downloader.createDownload(this.$store.state.url,{force : true},function(d,status){ // 下載完成 if(status == 200){ var path = (d.filename);//文件安裝路徑 plus.runtime.install(path,{},function(){ plus.nativeUI.closeWaiting();//關閉系統等待對話框 if(name == 'wgt'){ console.log("安裝wgt文件成功!"); }else{ console.log("安裝apk文件成功!") } plus.nativeUI.alert("引用資源更新完成!",function(){ plus.runtime.restart(); }) },function(e){ plus.nativeUI.closeWaiting();//關閉系統等待對話框 if(name == 'wgt'){ console.log("安裝wgt文件失敗["+e.code+"]:"+e.message); plus.nativeUI.alert("安裝wgt文件失敗["+e.code+"]:"+e.message); }else{ console.log("安裝apk文件失敗["+e.code+"]:"+e.message); plus.nativeUI.alert("安裝apk文件失敗["+e.code+"]:"+e.message); } }) }else{ alert("Download failed:"+status); } }) // #endif
三、wgt熱更新
HbuilderX App選擇發行=>原生App-制作應用wgt包
前端代碼
getVersionNumber(){//獲取版本號 var _self = this; this.$http('/wangge/systemManage/getLastVersion',{citycode:this.citycode},'GET').then(res=>{ console.log(res) console.log(this.$store.state) if(res.data.result && res.data.result.length>0){ //保存版本號 this.AppVersion(res.data.result[0]); //獲取下載地址 let url = res.data.result[0].url; let arr = url.split('.'); let name = arr[arr.length-1]; console.log(arr,name); if( Number(res.data.result[0].versioncode) > Number(this.$store.state.versionCode) ){//是否有新的版本 //下載熱更新資源包 不比較版本 const dtask = plus.downloader.createDownload(this.$store.state.url,{force : true},function(d,status){ // 下載完成 if(status == 200){ var path = (d.filename);//文件安裝路徑 plus.runtime.install(path,{},function(){ plus.nativeUI.closeWaiting();//關閉系統等待對話框 if(name == 'wgt'){ console.log("安裝wgt文件成功!"); }else{ console.log("安裝apk文件成功!") } plus.nativeUI.alert("引用資源更新完成!",function(){ plus.runtime.restart(); }) },function(e){ plus.nativeUI.closeWaiting();//關閉系統等待對話框 if(name == 'wgt'){ console.log("安裝wgt文件失敗["+e.code+"]:"+e.message); plus.nativeUI.alert("安裝wgt文件失敗["+e.code+"]:"+e.message); }else{ console.log("安裝apk文件失敗["+e.code+"]:"+e.message); plus.nativeUI.alert("安裝apk文件失敗["+e.code+"]:"+e.message); } }) }else{ alert("Download failed:"+status); } }) //彈出框是否下載最新版本 this.$showModal({ title:'更新', concent:'有新版本,是否更新~', cancelVal:'下次再說', confirmVal:'立即更新' }).then(res=>{ dtask.start(); let prg = 0; let showLoading = plus.nativeUI.showWaiting("正在下載"); dtask.addEventListener('statechanged', function(task, status) { // 給下載任務設置一個監聽 並根據狀態 做操作 switch (task.state) { case 1: showLoading.setTitle("正在下載"); //顯示loading提示框,title為提示的文字內容,顯示在loading的下方 break; case 3: prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100); showLoading.setTitle(" 正在下載" + prg + "% "); break; case 4: plus.nativeUI.closeWaiting(); //下載完成 break; } }) /* plus.runtime.openURL(this.$store.state.url); */ //確認 }).catch(res=>{ //取消 // 不更新 // console.log(res); }) }else{ } } }) },
不支持的情況
SDK 部分有調整,比如新增了 Maps 模塊等,不可通過此方式升級,必須通過整包的方式升級。
如果是老的非自定義組件編譯模式,之前沒有 nvue 文件,但更新中新增了 nvue
文件,不能使用此方式。因為非自定義組件編譯模式如果沒有nvue文件是不會打包weex引擎進去的,原生引擎無法動態添加。自定義組件模式默認就含着weex引擎,不管工程下有沒有nvue文件。
原生插件的增改,同樣不能使用此方式。
注意事項
條件編譯,僅在 App 平台執行此升級邏輯。
appid 以及版本信息等,在 HBuilderX 真機運行開發期間,均為 HBuilder
這個應用的信息,因此需要打包自定義基座或正式包測試升級功能。
plus.runtime.version 或者 uni.getSystemInfo() 讀取到的是 apk/ipa 包的版本號,而非
manifest.json 資源中的版本信息,所以這里用 plus.runtime.getProperty() 來獲取相關信息。
安裝 wgt 資源包成功后,必須執行 plus.runtime.restart(),否則新的內容並不會生效。
如果App的原生引擎不升級,只升級wgt包時需要注意測試wgt資源和原生基座的兼容性。平台默認會對不匹配的版本進行提醒,如果自測沒問題,可以在manifest中配置忽略提示,詳見https://ask.dcloud.net.cn/article/35627
www.example.com 是一個僅用做示例說明的地址,實際應用中應該是真實的 IP 或有效域名,請勿直接復制粘貼使用。
部分參考:https://blog.csdn.net/Bruce_byan/article/details/120197989