流行的热修复技术有哪些
时下比较流行的热修复技术有Tinker、QZone、AndFix、Robust。 前面两个是腾讯开发的,AndFix源于阿里,Robust是美国一家公司开发的。那么我们应该选用哪一款呢?网上有个它们之间的对比
Tinker | QZone | AndFix | Robust | |
---|---|---|---|---|
类替换 | yes | yes | no | no |
So替换 | yes | no | no | no |
资源替换 | yes | yes | no | no |
全平台支持 | yes | yes | yes | yes |
即时生效 | no | no | yes | yes |
性能损耗 | 较小 | 较大 | 较小 | 较小 |
补丁包大小 | 较小 | 较大 | 一般 | 一般 |
开发透明 | yes | yes | no | no |
复杂度 | 较低 | 较低 | 复杂 | 复杂 |
gradle支持 | yes | no | no | no |
Rom体积 | 较大 | 较小 | 较小 | 较小 |
成功率 | 较高 | 较高 | 一般 | 最高 |
可以看出,几种产品都有自己的侧重点,大家可根据自己的需求自由选择。本篇文章主要介绍一下Tinker和AndFix
强大的Tinker
tinker的功能非常强大,基本除了AndroidManifest.xml文件和tinker本身少数几个类之外,其他内容都能替换,包括布局、资源。不足之处在于其首次配置稍有点复杂,上手难度较AndFix稍高一些。废话不多说,上教程
接入步骤
在项目build.gradle文件中加入tinker插件配置
buildscript { dependencies { classpath ('com.tencent.tinker:tinker-patch-gradle-plugin:1.7.7') } }
在具体moudle的build.gradle文件中添加tinker相关依赖
dependencies { //optional, help to generate the final application provided('com.tencent.tinker:tinker-android-anno:1.7.7') //tinker's main Android lib compile('com.tencent.tinker:tinker-android-lib:1.7.7') compile "com.android.support:multidex:1.0.1" }
将以下内容拷贝到你moudle下面的build.gradle文件末尾
def gitSha() { try { String gitRev = 'git rev-parse --short HEAD'.execute(null, project.rootDir).text.trim() 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'") } } def bakPath = file("${buildDir}/bakApk/") /** * you can use assembleRelease to build you base apk * use tinkerPatchRelease -POLD_APK= -PAPPLY_MAPPING= -PAPPLY_RESOURCE= to build patch * add apk from the build/bakApk */ 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}/tinkerdemo-debug-0420-17-03-27.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}/tinkerdemo-debug-0420-17-03-27-R.txt" //only use for build all flavor, if not, just ignore this field tinkerBuildFlavorDirectory = "${bakPath}/app-1018-17-32-47" } def getOldApkPath() { return hasProperty("OLD_APK") ? OLD_APK