Tinker 官方代码示例 : https://github.com/Tencent/tinker/tree/dev/tinker-sample-android
下载该 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'")
}
}
运行 Gradle 面板中的 " tinker-sample-android / app / Tasks / other / assembleDebug " 任务 , 双击即可 ;
运行 assembleDebug 任务完毕后 , 在 " tinker-master\tinker-sample-android\app\build\bakApk " 目录下 , 会生成
两个文件 ;
还有一种方法 , 在 Terminal 中断面板中 , 执行 gradlew assembleDebug
命令 , 也可以运行 assembleDebug gradle 任务 ;
将上述编译好的 app-debug-0423-21-38-18.apk 安装文件 , 部署到应用中 , 运行应用 :
点击 " SHOW INFO " 按钮 , 即可查看运行效果 ;
根据上一步生成的两个文件
修改 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 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"
}
修改后的效果 :
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 程序 , 放开该行注释代码 ,
在 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 " , 热修复成功 ;
参考资料 :
源码资源 :