移动app开发的一个痛点就是遇到了bug只能重新打包,上传各个市场,审核,客户使用app后还会检查更新下载,这样为了修复一个bug而进行的这一套繁琐的操作,成本很高,不管对企业还是对用户都很烦恼;热修复的需求是很迫切的,近几年热修复的发展也很快,各个龙头企业都推出自己的热修复框架,之前了解过阿里的Dexposed,Andfix,QQ空间团队的Nuwa等等,都有很多不成熟的地方;
什么是 Tinker
Tinker是微信官方的 Android 热补丁解决方案,它支持动态下发代码、So 库以及资源,让应用能够在不需要重新安装的情况下实现更新。Github地址
当前市面的热补丁方案有很多,其中比较出名的有阿里的 AndFix、美团的 Robust 以及 QZone 的超级补丁方案。但它们都存在无法解决的问题,这也是正是我们推出 Tinker 的原因;Tinker热补丁方案不仅支持类、So 以及资源的替换,它还是2.X-7.X的全平台支持。利用Tinker我们不仅可以用做 bugfix,甚至可以替代功能的发布。Tinker 已运行在微信的数亿 Android 设备上,那么为什么你不使用 Tinker 呢?---来自tinker官方介绍(牛**)
接下来是来自官方的截图:
看到这个对比,瞬间就像试一把。
Tinker的集成
添加 gradle 插件依赖
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath "com.tencent.tinker:tinker-patch-gradle-plugin:${TINKER_VERSION}"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
${TINKER_VERSION}在gradle.properties中配置
TINKER_VERSION=1.7.7
然后在模块中的gradle添加配置:
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:23.1.1"
compile("com.tencent.tinker:tinker-android-lib:${TINKER_VERSION}") { changing = true }
provided("com.tencent.tinker:tinker-android-anno:${TINKER_VERSION}") { changing = true }
compile "com.android.support:multidex:1.0.1"
//use to test multiDex
// compile group: 'com.google.guava', name: 'guava', version: '19.0'
// compile "org.scala-lang:scala-library:2.11.7"
//use for local maven test
// compile("com.tencent.tinker:tinker-android-loader:${TINKER_VERSION}") { changing = true }
// compile("com.tencent.tinker:aosp-dexutils:${TINKER_VERSION}") { changing = true }
// compile("com.tencent.tinker:bsdiff-util:${TINKER_VERSION}") { changing = true }
// compile("com.tencent.tinker:tinker-commons:${TINKER_VERSION}") { changing = true }
}
def gitSha() {
try {
// String gitRev = 'git rev-parse --short HEAD'.execute(null, project.rootDir).text.trim()
String gitRev ="1.0.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'")
}
}
def javaVersion = JavaVersion.VERSION_1_7
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileOptions {
sourceCompatibility javaVersion
targetCompatibility javaVersion
}
//recommend
dexOptions {
jumboMode = true
}
signingConfigs {
release {
try {
storeFile file("./keystore/release.keystore")
storePassword "testres"
keyAlias "testres"
keyPassword "testres"
} catch (ex) {
throw new InvalidUserDataException(ex.toString())
}
}
debug {
storeFile file("./keystore/debug.keystore")
}
}
defaultConfig {
applicationId "tinker.sample.android"
minSdkVersion 10
targetSdkVersion 22
versionCode 1
versionName "1.0.0"
/**
* you can use multiDex and install it in your ApplicationLifeCycle implement
*/
multiDexEnabled true
/**
* buildConfig can change during patch!
* we can use the newly value when patch
*/
buildConfigField "String", "MESSAGE", "\"I am the base apk\""
// buildConfigField "String", "MESSAGE", "\"I am the patch apk\""
/**
* client version would update with patch
* so we can get the newly git version easily!
*/
buildConfigField "String", "TINKER_ID", "\"${getTinkerIdValue()}\""
buildConfigF