Tinker热修复实战|8月更文挑战

558 阅读5分钟

Tinker热修复实战

2016/3/10,Tinker项目正式启动,来自于微信官方,这对开发者来说确实是福音,不再需要嵌套webview的方式进行动态更新了,解决一些线上的小bug,也不用发布版本了。但对于实际维护来说,包管理也是一个比较麻烦的事。而它的实现原理就是通过dex差量,将差量包下发到客户端进行更新

一、效果图

1.gif

二、参考链接

实现原理:www.cnblogs.com/popfisher/p…

官方教程:www.tinkerpatch.com/

为什么在有官方教程的情况下,还要写这篇博客呢?

是为了记录在引入Tinker过程中的坑。

三、集成步骤

1. 第一步 添加 gradle 插件依赖


buildscript {

repositories {

jcenter()

}

dependencies {

// TinkerPatch 插件

classpath "com.tinkerpatch.sdk:tinkerpatch-gradle-plugin:1.2.9"

}

}

2. 第二步 集成 TinkerPatch SDK


dependencies {

// 若使用annotation需要单独引用,对于tinker的其他库都无需再引用

provided("com.tinkerpatch.tinker:tinker-android-anno:1.9.9")

compile("com.tinkerpatch.sdk:tinkerpatch-android-sdk:1.2.9")

}

该步骤出现错误提示

Configuration 'provided' is obsolete and has been replaced with 'compileOnly'.

很明显,直接修改即可


dependencies {

// 若使用annotation需要单独引用,对于tinker的其他库都无需再引用

compileOnly("com.tinkerpatch.tinker:tinker-android-anno:1.9.9")

implementation("com.tinkerpatch.sdk:tinkerpatch-android-sdk:1.2.9")

}

3. 第三步 新建tinkerpatch.gradle 并配置 tinkerpatchSupport 参数

(1) 在app根目录下新建tinkerpatch.gradle文件,填入以下信息,该信息来自官方的github


apply plugin: 'tinkerpatch-support'

/**

* TODO: 请按自己的需求修改为适应自己工程的参数

*/

def bakPath = file("${buildDir}/bakApk/")

def baseInfo = "app-1.0.0-1112-12-49-34"

def variantName = "debug"

/**

* 对于插件各参数的详细解析请参考

* 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 = true

/**

* 是否开启加固模式,只能在APK将要进行加固时使用,否则会patch失败。

* 如果只在某个渠道使用了加固,可使用多flavors配置

**/

protectedApp = false

/**

* 实验功能

* 补丁是否支持新增 Activity (新增Activity的exported属性必须为false)

**/

supportComponent = true

autoBackupApkPath = "${bakPath}"

appKey = "f938475486f91936"

/** 注意: 若发布新的全量包, appVersion一定要更新 **/

appVersion = "1.0.0"

def pathPrefix = "${bakPath}/${baseInfo}/${variantName}/"

def name = "${project.name}-${variantName}"

baseApkFile = "${pathPrefix}/${name}.apk"

baseProguardMappingFile = "${pathPrefix}/${name}-mapping.txt"

baseResourceRFile = "${pathPrefix}/${name}-R.txt"

/**

* 若有编译多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

}

}

如果出现错误Tinker Exception:applicationLike must not be null.

是因为 tinkerPatch.gradle中配置 reflectApplication = false,但是你又没有相应的改造你的Application类。本文介绍的是不改造我们的 Application 类接入Tinker,所以 配置应该为:reflectApplication = ture。

(2) 为了简单方便,我们将 TinkerPatch 相关的配置都放于 tinkerpatch.gradle 中, 在app/build.gradle的我们需要将其引入:


apply from: 'tinkerpatch.gradle'

四、初始化 TinkerPatch SDK

(1) 新建SampleApplication自定义的Application类,并在AndroidManifest.xml文件中name=""引入它


android:name=".SampleApplication"

(2) 在SampleApplication中接入以下代码


public class SampleApplication extends Application {

private ApplicationLike tinkerApplicationLike;

@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();

}

}

五、更改Key,并开始使用,使用步骤

(1)将在tinkerpatch申请的key填入

tinkerpatch.gradle中更改


appKey = "your key"

(2) 开始使用-官方介绍


1.运行 assembleRelease task 构建基准包(请在发布前确保更新tinkerpatchSupport中的appVersion),tinkerPatch会基于你填入的autoBackupApkPath自动备份基础包信息到相应的文件夹,包含:apk文件、R.txt文件和mapping.txt文件 (注:mapping.txt是proguard的产物,如果你没有开启proguard则不会有这个文件)

2.若想发布补丁包,只需将自动保存下来的文件分别填到tinkerpatchSupport中的baseApkFile、baseProguardMappingFile和baseResourceRFile 参数中;

3.运行 tinkerPatchRelease task 构建补丁包,补丁包将位于 build/outputs/tinkerPatch下

(3) 开始使用-自己实践

先理解一些概念。

assembleRelease就是打包

tinkerPatchRelease是为了生成差异包

  1. 先手动模拟一个bug

public class MainActivity extends Activity {

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView = findViewById(R.id.tv_test);

textView.setText("我是一个bug");

}

}

  1. 如果想及时生效,就下载官方的一个demo,并打开测试开关

地址:www.tinkerpatch.com/Docs/dev

  1. 打开AndroidStudio右侧的Gradle

2.jpg

  1. 点击assembleRelease,发现该错误

* What went wrong:

Execution failed for task ':app:javaPreCompileRelease'.

> Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor. Please add them to the annotationProcessor configuration.

- tinker-android-anno-1.9.9.jar (com.tinkerpatch.tinker:tinker-android-anno:1.9.9)

Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true to continue with previous behavior. Note that this option is deprecated and will be removed in the future.

See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.

解决

在app的build中添加


android {

...

defaultConfig {

...

//添加如下配置就OK了

javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }

}

...

}

运行成功后,会发现在app/build下生成了很多文件

3.jpg

  1. 手动模拟修复了这个bug

public class MainActivity extends Activity {

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView = findViewById(R.id.tv_test);

textView.setText("bug被修复");

}

}

  1. 修改tinkerpatch.gradle文件

该文件中很多属性指引的都是本地的目录,按照这个格式,需要修改以下参数。


//与上个步骤生成的文件对应

def baseInfo = "app-1.0.0-0416-20-10-21"

def variantName = "release"

  1. 然后就是运行tinkerPatchRelease

4.jpg

发现以下错误


* What went wrong:

Execution failed for task ':app:tinkerPatchRelease'.

> can't the get signConfig for this build

在build中不能获取签名配置信息,那我们配置下签名信息吧。

这一步不清楚怎么配置的请移步到 Google

  1. 继续执行tinkerPatchRelease生成差异包

如果不行,clean项目,从4步骤开始

成功之后发现生成了差异包,将差异包(补丁文件)上传至tinkerpatch平台

5.jpg

  1. 为了方便快捷,我们选择开发预览,并打开了官方提供的demo中的测试环境。
  • 先安装有bug的安装包

  • 打开之后杀死进程,重新进入,发现界面提示了“bug已修复”; 不行的话多试几次,给它一点合成的时间

六、其它需要注意的点

  1. 需要添加权限

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

  1. 在tinkerpatch平台添加版本时是app的版本,对应的版本请求对应版本的补丁

  2. 全量更新时需要更新tinkerpatch.gradle中的appVersion