前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android 热修复】运行 Tinker 官方示例 ( 处理 TINKER_ID 问题 | 编译 debug 包 | 修改 Gradle 脚本 | 生成 patch 包 | 热修复 )

【Android 热修复】运行 Tinker 官方示例 ( 处理 TINKER_ID 问题 | 编译 debug 包 | 修改 Gradle 脚本 | 生成 patch 包 | 热修复 )

作者头像
韩曙亮
发布2023-03-29 10:23:24
4870
发布2023-03-29 10:23:24
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、下载官方示例源码


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 参数 ,

代码语言:javascript
复制
TINKER_ID=1.0
TINKER_ENABLE=true

方案二 : 修改 https://github.com/Tencent/tinker/blob/dev/tinker-sample-android/app/build.gradle 构建脚本代码 , 使 gitSha 方法返回非空字符串 ;

代码语言:javascript
复制
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 " ,

代码语言:javascript
复制
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 apk
    tinkerOldApkPath = "${bakPath}/app-debug-0424-15-02-56.apk"
    //proguard mapping file to build patch apk
    tinkerApplyMappingPath = "${bakPath}/app-debug-1018-17-32-47-mapping.txt"
    //resource R.txt to build patch apk, must input if there is resource changed
    tinkerApplyResourcePath = "${bakPath}/app-debug-0424-15-02-56-R.txt"

    //only use for build all flavor, if not, just ignore this field
    tinkerBuildFlavorDirectory = "${bakPath}/app-1018-17-32-47"
}

修改后的效果 :

代码语言:javascript
复制
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 apk
    tinkerOldApkPath = "${bakPath}/app-debug-0423-21-38-18.apk"
    //proguard mapping file to build patch apk
    tinkerApplyMappingPath = "${bakPath}/app-debug-0423-21-38-18-mapping.txt"
    //resource R.txt to build patch apk, must input if there is resource changed
    tinkerApplyResourcePath = "${bakPath}/app-debug-0423-21-38-18-R.txt"

    //only use for build all flavor, if not, just ignore this field
    tinkerBuildFlavorDirectory = "${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 " , 热修复成功 ;

九、 源码资源


参考资料 :

源码资源 :

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-04-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、下载官方示例源码
  • 二、处理 TINKER_ID 问题
  • 三、编译 debug 包
  • 四、安装 APK 并运行
  • 五、修改 Gradle 构建脚本中的文件名称
  • 六、修改程序逻辑代码
  • 七、生成 patch 包
  • 八、热修复
  • 九、 源码资源
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档