Android热修复之Tinker接入流程
16lz
2021-01-26
在上一篇文章中我们已经介绍了各大热修复框架的优缺点以及Hotfix的接入流程,如果有兴趣可以先阅读Android热修复之Hotfix接入流程。这次主要简单介绍一下Tinker的接入流程。
Tinker最大的优势就如官方所说的:Tinker 已运行在微信的数亿 Android 设备上,那么为什么你不使用 Tinker 呢?因此Tinker相对其他热修复框架,在兼容性和稳定性方面会更加突出。
1. 接入前准备
1.1 注册 TinkerPatch 平台账号 前往注册
1.2 添加APP
添加成功后我们可以得到对应的appKey
2. 集成Tinker
2.1 在TinkerTest下的build.gradle中添加插件依赖
buildscript { repositories { jcenter() } dependencies { // TinkerPatch 插件 classpath "com.tinkerpatch.sdk:tinkerpatch-gradle-plugin:1.1.7" }}
2.2 在app/build.gradle中添加Tinker库依赖
dependencies { provided("com.tinkerpatch.tinker:tinker-android-anno:1.7.11") compile("com.tinkerpatch.sdk:tinkerpatch-android-sdk:1.1.7")}
2.3 在app目录下创建tinkerpatch.gradle文件并添加以下内容
注意需要将appKey属性修改为我们之前得到对应的appKey,appVersion应与当前版本号相同。
apply plugin: 'tinkerpatch-support'/** * TODO: 请按自己的需求修改为适应自己工程的参数 */def bakPath = file("${buildDir}/bakApk/")def baseInfo = "app-1.1-0621-14-28-43"def variantName = "release"/** * 对于插件各参数的详细解析请参考 * http://tinkerpatch.com/Docs/SDK */tinkerpatchSupport { /** 可以在debug的时候关闭 tinkerPatch **/ /** 当disable tinker的时候需要添加multiDexKeepProguard和proguardFiles, 这些配置文件本身由tinkerPatch的插件自动添加,当你disable后需要手动添加 你可以copy本示例中的proguardRules.pro和tinkerMultidexKeep.pro, 需要你手动修改'tinker.sample.android.app'本示例的包名为你自己的包名, com.xxx前缀的包名不用修改 **/ tinkerEnable = true reflectApplication = false autoBackupApkPath = "${bakPath}" appKey = "1333019bd1ab7081" /** 注意: 若发布新的全量包, appVersion一定要更新 **/ appVersion = "1.1" def pathPrefix = "${bakPath}/${baseInfo}/${variantName}/" def name = "${project.name}-${variantName}" baseApkFile = "${pathPrefix}/${name}.apk"// baseProguardMappingFile = "${pathPrefix}/${name}-mapping.txt" baseResourceRFile = "${pathPrefix}/${name}-R.txt"// baseApkFile = null baseProguardMappingFile = null// baseResourceRFile = null /** * 若有编译多flavors需求, 可以参照: https://github.com/TinkerPatch/tinkerpatch-flavors-sample * 注意: 除非你不同的flavor代码是不一样的,不然建议采用zip comment或者文件方式生成渠道信息(相关工具:walle 或者 packer-ng) **/}/** * 用于用户在代码中判断tinkerPatch是否被使能 */android { defaultConfig { buildConfigField "boolean", "TINKER_ENABLE", "${tinkerpatchSupport.tinkerEnable}" }}/** * 一般来说,我们无需对下面的参数做任何的修改 * 对于各参数的详细介绍请参考: * https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97 */tinkerPatch { ignoreWarning = false useSign = true dex { dexMode = "jar" pattern = ["classes*.dex"] loader = [] } lib { pattern = ["lib/*/*.so"] } res { pattern = ["res/*", "r/*", "assets/*", "resources.arsc", "AndroidManifest.xml"] ignoreChange = [] largeModSize = 100 } packageConfig { } sevenZip { zipArtifact = "com.tencent.mm:SevenZip:1.1.10"// path = "/usr/local/bin/7za" } buildConfig { keepDexApply = false }}
2.4 在app/build.gradle中引入tinkerpatch.gradle
apply from: 'tinkerpatch.gradle'
2.5 初始化TInker SDK
初始化TInker SDK的方式主要根据tinkerpatch.gradle中的reflectApplication属性值来决定
2.5.1 当reflectApplication = true时,我们只需要将初始化代码放在我们创建的Application中即可
public class SampleApplication extends Application { ... @Override public void onCreate() { super.onCreate(); // 我们可以从这里获得Tinker加载过程的信息 tinkerApplicationLike = TinkerPatchApplicationLike.getTinkerPatchApplicationLike(); // 初始化TinkerPatch SDK, 更多配置可参照API章节中的,初始化SDK TinkerPatch.init(tinkerApplicationLike) .reflectPatchLibrary() .setPatchRollbackOnScreenOff(true) .setPatchRestartOnSrceenOff(true) .setFetchPatchIntervalByHours(3); // 每隔3个小时(通过setFetchPatchIntervalByHours设置)去访问后台时候有更新,通过handler实现轮训的效果 TinkerPatch.with().fetchPatchUpdateAndPollWithInterval(); } ... }
2.5.2 当reflectApplication = false时,我们只需要创建一个类继承DefaultApplicationLike,如下:
@SuppressWarnings("unused")@DefaultLifeCycle(application = ".MainApplication", flags = ShareConstants.TINKER_ENABLE_ALL, loadVerifyFlag = false)public class MainApplicationLike extends DefaultApplicationLike { public MainApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) { super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent); } @Override public void onCreate() { super.onCreate(); // 初始化TinkerPatch SDK, 更多配置可参照API章节中的,初始化 SDK TinkerPatch.init(this) .reflectPatchLibrary() .setPatchRollbackOnScreenOff(true) .setPatchRestartOnSrceenOff(true) .setFetchPatchIntervalByHours(3); // 每隔3个小时(通过setFetchPatchIntervalByHours设置)去访问后台时候有更新,通过handler实现轮训的效果 TinkerPatch.with().fetchPatchUpdateAndPollWithInterval(); }}
头部注解中的.MainApplication就是编译后生成的真正的Application,我们需要将其写在AndroidManifest.xml中,需要编译后才不会报错。
@DefaultLifeCycle(application = ".MainApplication", flags = ShareConstants.TINKER_ENABLE_ALL, loadVerifyFlag = false)
".MainApplication" android:allowBackup="false" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> ...
到此Tinker集成完毕
3. 创建补丁和修复示例
这里的修复示例和上篇一样,改变一下文本内容即可,将TextView中的内容Hello World!热修复为Hello Tinker!
3.1 构建基准包
此时TextView的内容为 Hello World!
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" />
在Terminal命令面板中执行gradlew assembleDebug命令 如果打正式包,则使用gradlew assembleRelease(需要在app/build.gradle配置密钥信息)
执行成功后可以看到生成的文件
3.2 构建补丁包
此时TextView的内容为 Hello Tinker!
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Tinker!" />
3.2.1 配置tinkerpatch.gradle
将baseInfo的值修改为我们上一步中生成的基准包的名字,variantName修改为debug 如果是正式打包这修改为release。
/** * TODO: 请按自己的需求修改为适应自己工程的参数 */def bakPath = file("${buildDir}/bakApk/")def baseInfo = "app-1.1-0706-10-47-42"def variantName = "debug"
3.2.2 执行gradlew tinkerPatchDebug/gradlew tinkerPatchRelease命令
执行成功后会生成的补丁会放置于如下目录,其中patch_signed_7zip.apk就是我们需要的补丁文件。
3.3上传补丁
添加版本
上传补丁
上传完毕后装上基准包的apk进行测试
可以发现第一次进入应用时TextView文本为Hello World!,退出应用重新进入时TextView文本为Hello Tinker! 说明补丁生效,热修复成功。
下载Demo
更多相关文章
- Android(安卓)Studio 1.5
- android 为桌面图标添加数字角标
- 自己实现的android树控件,android TreeView
- 一分钟帮你提升Android(安卓)studio 编译速度
- 34、Android编写应用-从模板添加代码
- Android(安卓)在Launcher桌面添加应用快捷图标(适用于Android(安
- android fragment详细介绍
- 从零开始--系统深入学习android(实践-让我们开始写代码-Android框
- Android(安卓)Ble从模式(Peripheral)开发