【Android 热修复】运行 Tinker 官方示例 ( 处理 TINKER_ID 问题 | 编译 debug 包 | 修改 Gradle 脚本 | 生成 patch 包 | 热修复 )
文章目錄
- 一、下載官方示例源碼
- 二、處理 TINKER_ID 問題
- 三、編譯 debug 包
- 四、安裝 APK 并運行
- 五、修改 Gradle 構建腳本中的文件名稱
- 六、修改程序邏輯代碼
- 七、生成 patch 包
- 八、熱修復
- 九、 源碼資源
一、下載官方示例源碼
Tinker 官方代碼示例 : https://github.com/Tencent/tinker/tree/dev/tinker-sample-android
二、處理 TINKER_ID 問題
下載該 tinker-sample-android 代碼 , 先處理 TINKER_ID 問題 , 參考 【錯誤記錄】Tinker 熱修復示例運行報錯 ( Execution failed for task ‘:app:tinkerProcessD‘ . tinkerId is not set!!! )
解決 " tinkerId is not set!!! " 問題 , 有兩種處理方案 :
方案一 :
在 gradle.properties 配置中 , 設置 TINKER_ID 參數 ,
TINKER_ID=1.0 TINKER_ENABLE=true方案二 : 修改 https://github.com/Tencent/tinker/blob/dev/tinker-sample-android/app/build.gradle 構建腳本代碼 , 使 gitSha 方法返回非空字符串 ;
def gitSha() {try {String gitRev = "1.0"if (gitRev == null) {throw new GradleException("can't get git rev, you should add git to system path or just input test value, such as 'testTinkerId'")}return gitRev} catch (Exception e) {throw new GradleException("can't get git rev, you should add git to system path or just input test value, such as 'testTinkerId'")} }三、編譯 debug 包
運行 Gradle 面板中的 " tinker-sample-android / app / Tasks / other / assembleDebug " 任務 , 雙擊即可 ;
運行 assembleDebug 任務完畢后 , 在 " tinker-master\tinker-sample-android\app\build\bakApk " 目錄下 , 會生成
- app-debug-0423-21-38-18.apk
- app-debug-0423-21-38-18-R.txt
兩個文件 ;
還有一種方法 , 在 Terminal 中斷面板中 , 執行 gradlew assembleDebug 命令 , 也可以運行 assembleDebug gradle 任務 ;
四、安裝 APK 并運行
將上述編譯好的 app-debug-0423-21-38-18.apk 安裝文件 , 部署到應用中 , 運行應用 :
點擊 " SHOW INFO " 按鈕 , 即可查看運行效果 ;
五、修改 Gradle 構建腳本中的文件名稱
根據上一步生成的兩個文件
- app-debug-0423-21-38-18.apk
- app-debug-0423-21-38-18-R.txt
修改 app 下的 build.gradle 構建腳本 " tinker-sample-android\app\ build.gradle" , 主要使用 " 0423-21-38-18 " 這個時間參數 ;
在 Gradle 構建腳本中 , 修改如下四個變量中的時間相關值 , 將時間參數修改為 " 0423-21-38-18 " ,
ext {//for some reason, you may want to ignore tinkerBuild, such as instant run debug build?tinkerEnabled = true//for normal build//old apk file to build patch apktinkerOldApkPath = "${bakPath}/app-debug-0424-15-02-56.apk"//proguard mapping file to build patch apktinkerApplyMappingPath = "${bakPath}/app-debug-1018-17-32-47-mapping.txt"//resource R.txt to build patch apk, must input if there is resource changedtinkerApplyResourcePath = "${bakPath}/app-debug-0424-15-02-56-R.txt"//only use for build all flavor, if not, just ignore this fieldtinkerBuildFlavorDirectory = "${bakPath}/app-1018-17-32-47" }修改后的效果 :
ext {//for some reason, you may want to ignore tinkerBuild, such as instant run debug build?tinkerEnabled = true//for normal build//old apk file to build patch apktinkerOldApkPath = "${bakPath}/app-debug-0423-21-38-18.apk"//proguard mapping file to build patch apktinkerApplyMappingPath = "${bakPath}/app-debug-0423-21-38-18-mapping.txt"//resource R.txt to build patch apk, must input if there is resource changedtinkerApplyResourcePath = "${bakPath}/app-debug-0423-21-38-18-R.txt"//only use for build all flavor, if not, just ignore this fieldtinkerBuildFlavorDirectory = "${bakPath}/app-0423-21-38-18" }六、修改程序邏輯代碼
修改 MainActivity 程序 , 放開該行注釋代碼 ,
七、生成 patch 包
在 Gradle 面板中 , 運行 " tinker-sample-android / app / Tasks / tinker/ tinkerPatchDebug " 任務 , 雙擊即可 ;
運行完成后 , 在 " tinker-sample-android\app\build\outputs\apk\tinkerPatch\debug " 目錄下生成了 patch 包 ;
八、熱修復
將 app-debug-patch_signed_7zip.apk 文件 , 更名為 patch_signed_7zip.apk , 拷貝到手機 SD 卡根目錄 ;
點擊 " LOAD PATCH " 按鈕 , 會有 Toast 提示成功 " patch success, please restart process " , 熱修復成功 ;
九、 源碼資源
參考資料 :
-
官方主頁 : https://github.com/Tencent/tinker
-
Tinker 官方 Wiki 地址 : https://github.com/Tencent/tinker/wiki
-
Tinker 接入指南 : https://github.com/Tencent/tinker/wiki/Tinker-接入指南
-
Tinker 官方示例 : https://github.com/Tencent/tinker/tree/master/tinker-sample-android
源碼資源 :
- GitHub 地址 : https://github.com/Tencent/tinker/tree/dev/tinker-sample-android
- CSDN 源碼快照 : https://download.csdn.net/download/han1202012/17417498
( Tiinker 完整項目 )
總結
以上是生活随笔為你收集整理的【Android 热修复】运行 Tinker 官方示例 ( 处理 TINKER_ID 问题 | 编译 debug 包 | 修改 Gradle 脚本 | 生成 patch 包 | 热修复 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【错误记录】Tinker 热修复示例运行
- 下一篇: 【Android 安装包优化】WebP