- 微信tinker的介绍:Tinker是微信前段时间开源的Android热补丁方案,它支持动态下发代码、So库以及资源,让应用能够在不需要重新安装的情况下实现更新。当然,你也可以使用Tinker来更新你的插件。
同时,还有许多其他公司使用的热补丁方案,如比如QZone,AndFix,Dexposed等等; - 微信tinker的优缺点:
/ | 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有以下已知问题:
1.Tinker不支持修改AndroidManifest.xml,Tinker不支持新增四大组件;
2.由于Google Play的开发者条款限制,不建议在GP渠道动态更新代码;
3.在Android N上,补丁对应用启动时间有轻微的影响;
4.不支持部分三星android-21机型,加载补丁时会主动抛出”TinkerRuntimeException:checkDexInstall failed”;
5.对于资源替换,不支持修改remoteView。例如transition动画,notification icon以及桌面图标。 - 如何集成:
- 在项目的build.gradle中添加 tinker-patch-gradle-plugin 的依赖:
dependencies {
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
}
- 然后在app的gradle文件app/build.gradle,我们需要添加tinker的库依赖以及apply tinker的gradle插件.
dependencies {
// 依赖multiDex库
compile 'com.android.support:multidex:1.0.1'
//可选,用于生成application类
provided ("com.tencent.tinker:tinker-android-anno:${TINKER_VERSION}"){changing = true}
//Tinker的核心库
compile ("com.tencent.tinker:tinker-android-lib:${TINKER_VERSION}"){changing = true}
}
apply plugin: 'com.tencent.tinker.patch'
- 在app/builde.gradle文件的dependencies外面添加一些配置 :
tinkerPatch {
//有问题的apk的地址
oldApk = "F://app_bug.apk"
ignoreWarning = false
useSign = true
buildConfig{
tinkerId = "1.0"
}
packageConfig{
configField("TINKER_ID", "1.0")
}
dex{
dexMode = "jar"
pattern = ["classes*.dex", "assets/secondary-dex-?.jar"]
loader = ["com.tencent.tinker.loader.*", "com.tinkertest.Application"]
}
lib{
pattern = ["lib/armeabi/*.so","lib/arm64-v8a/*.so","lib/armeabi-v7a/*.so","lib/mips/*.so","lib/mips64/*.so","lib/x86/*.so","lib/x86_64/*.so"]
}
res{
pattern = ["res/*", "assets/*", "resources.arsc", "AndroidManifest.xml"]
largeModSize = 100
}
sevenZip{
zipArtifact = "com.tencent.mm:SevenZip:1.1.10"
}
- 自定义Application类
1.写一个AppContextLike类继承自DefaultApplicationLike,并添加注解。使用注解生成application类AppContext,并且可以在这个类里面编写你原来Application需要实现的逻辑代码。
@DefaultLifeCycle(
application = ".AppContext",
flags = ShareConstants.TINKER_ENABLE_ALL,
loadVerifyFlag = false
)
public class AppContextLike extends DefaultApplicationLike {
public AppContextLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
}
@Override
public void onBaseContextAttached(Context base) {
super.onBaseContextAttached(base);
//必须安装MultiDex的更新!
MultiDex.install(base);
//安装Tinker后加载multiDex
TinkerManager.installTinker(this);
ApplicationContext.application = getApplication();
ApplicationContext.context = getApplication();
TinkerManager.setTinkerApplicationLike(this);
TinkerManager.initFastCrashProtect();
TinkerManager.setUpgradeRetryEnable(true);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
getApplication().registerActivityLifecycleCallbacks(callback);
}
}
2.添加到manifest中
**android:name=".tinker.AppContext"**
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
3.先将你的app运行到手机上并修改gradle里面oldapk的路径
4.修改app的代码,例如修改文字,Toast等,在实际中即修复bug。
5.在android-studio右边点开gradle窗口,选择tinkerPatchDebug,并运行生成补丁包
6.将补丁包放到手机的SD中
好啦,到这里微信热修复的集成就完成啦,如果有疑问,可以在下方留言,或者加我的QQ844974121 ,注明:微信热修复!
最后提醒大家不要忘记添加权限哦!(7.0的系统需要手动设置权限!)