使用Tinker进行热更新

2017-07-13  本文已影响48人  AJI大侠

为什么使用Tinker

Tinker的介绍

  1. 在项目的[build.gradle]中,添加tinker-patch-gradle-plugin的依赖
buildscript {
    dependencies {
        classpath ('com.tencent.tinker:tinker-patch-gradle-plugin:1.7.11')
    }
}
  1. 然后在app的gradle文件[app/build.gradle]我们需要添加tinker的库依赖以apply tinker的gradle插件
dependencies {
    //可选,用于生成application类 
    provided('com.tencent.tinker:tinker-android-anno:1.7.11')
    //tinker的核心库
    compile('com.tencent.tinker:tinker-android-lib:1.7.11') 
}
...
...
//apply tinker插件
apply plugin: 'com.tencent.tinker.patch'
  1. 配置好这些之后我们可以下载官方的demo找到 build.gradle文件,从build.gradle 的第127行开始复制粘贴到自己的app/build.gradle文件中
  def bakPath = file("${buildDir}/bakApk/")......

4.注意配置apk路径

  ext {
    //for some reason, you may want to ignore tinkerBuild, such as instant run debug build?
    tinkerEnabled = true
    //for normal build
    //旧版本apk路径配置
    tinkerOldApkPath = "${bakPath}/app-debug-xxxx-14-05-21.apk"
    //用于混淆,没有混淆可以不管
    tinkerApplyMappingPath = "${bakPath}/app-debug-xxxx-14-05-21-mapping.txt"
    //旧版本apk R文件
    tinkerApplyResourcePath = "${bakPath}/app-debug-xxxx-14-05-21-R.txt"

    //only use for build all flavor, if not, just ignore this field
    tinkerBuildFlavorDirectory = "${bakPath}/app-debug-xxxx-14-05-21"
}

5.新建自己的Application

Tinker目前存在的问题

1.Tinker不支持修改AndroidManifest.xml,Tinker不支持新增四大组件;
2.由于Google Play的开发者条款限制,不建议在GP渠道动态更新代码;
3.在Android N上,补丁对应用启动时间有轻微的影响;
4.不支持部分三星android-21机型,加载补丁时会主动抛出"TinkerRuntimeException:checkDexInstall failed";
5.对于资源替换,不支持修改remoteView。例如transition动画,notification icon以及桌面图标。

上一篇 下一篇

猜你喜欢

热点阅读