Android 腾讯bugly Tinker 热修复
Bugly热更新是腾讯推出的热更新框架,热更新是指无需到应用市场重新下载安装app,只需要在app内下载补丁包即可实现app的更新,主要用于app的bug修复或者少量改动。
大家在使用app(特别是游戏app比较常见)的时候应该都有过类似经历:打开app时,一个弹框显示:有新的更新包,点击下载,只需要下载几百KB或几兆的补丁包,app就实现了更新,这就是使用的热更新技术。
Bugly热更新官方接入文档:[https://bugly.qq.com/docs/user-guide/instruction-manual-android-hotfix/?v=20180709165613](https://bugly.qq.com/docs/user-guide/instruction-manual-android-hotfix/?v=20180709165613)
- 简介
- 一、添加插件依赖
- 二、gradle配置
- 三、新建tinker-support.gradle
- 四、初始化SDK
- 五、AndroidManifest.xml配置
- 六、混淆配置:
- 七、生成基准包
- 八、生成补丁包
- 九、上传补丁包,见证热更新
一、添加插件依赖
工程根目录下“build.gradle”文件中添加:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.tencent.bugly:tinker-support:1.1.5" }
}</pre>
# 二、gradle配置
先生成一个jks密钥,然后在app module的“build.gradle”文件中添加(示例配置):
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">android {
defaultConfig {
ndk {
abiFilters 'armeabi' }
signingConfigs {
release {
storeFile file(jks密钥库路径)
storePassword 密钥库密码
keyAlias 密钥名
keyPassword 密钥密码
}
}
buildTypes {
release { //应用签名信息
signingConfig signingConfigs.release
minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }
}
}
}
dependencies {
implementation 'com.android.support:design:27.1.1' implementation 'com.android.support:multidex:1.0.3' implementation 'com.tencent.bugly:crashreport_upgrade:1.3.5' implementation 'com.tencent.tinker:tinker-android-lib:1.9.6' implementation 'com.tencent.bugly:nativecrashreport:2.2.0' }</pre>
# 三、新建tinker-support.gradle
在app module下新建tinker-support.gradle,内容如下:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">apply plugin: 'com.tencent.bugly.tinker-support' def bakPath = file("${buildDir}/bakApk/") /**
- 此处填写每次构建生成的基准包目录 */ def baseApkDir = "app-0719-23-18-50"
/**
-
对于插件各参数的详细解析请参考 */ tinkerSupport { // 开启tinker-support插件,默认值true
enable = true// 指定归档目录,默认值当前module的子目录tinker
autoBackupApkDir = "${bakPath}"// 是否启用覆盖tinkerPatch配置功能,默认值false // 开启后tinkerPatch配置不生效,即无需添加tinkerPatch
overrideTinkerPatchConfiguration = true// 编译补丁包时,必需指定基线版本的apk,默认值为空 // 如果为空,则表示不是进行补丁包的编译 // @{link tinkerPatch.oldApk }
baseApk = "{baseApkDir}/app-release.apk"// 对应tinker插件applyMapping
baseApkProguardMapping = "{baseApkDir}/app-release-mapping.txt"// 对应tinker插件applyResourceMapping
baseApkResourceMapping = "{baseApkDir}/app-release-R.txt"// 构建基准包和补丁包都要指定不同的tinkerId,并且必须保证唯一性
tinkerId = "base-1.3"// 构建多渠道补丁时使用 // buildAllFlavorsDir = "{baseApkDir}" // 是否启用加固模式,默认为false.(tinker-spport 1.0.7起支持) // isProtectedApp = true // 是否开启反射Application模式
enableProxyApplication = false// 是否支持新增非export的Activity(注意:设置为true才能修改AndroidManifest文件)
supportHotplugComponent = true } /** 一般来说,我们无需对下面的参数做任何的修改
对于各参数的详细介绍请参考:
-
https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
/ tinkerPatch { //oldApk ="{appName}/app-release.apk"
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
//tinkerId = "1.0.1-base" //applyMapping = "{appName}/app-release-mapping.txt" // 可选,设置mapping文件,建议保持旧apk的proguard混淆方式 //applyResourceMapping = "{appName}/app-release-R.txt" // 可选,设置R.txt文件,通过旧apk文件保持ResId的分配
}
}</pre>
建立完成后,在app module中的build.gradle文件中添加:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">apply from: 'tinker-support.gradle'</pre>
# 四、初始化SDK
先在[Bugly管理平台:https://bugly.qq.com/v2/](https://bugly.qq.com/v2/)上申请appId,注册账户后登录,点击新建产品:
![image](https://upload-images.jianshu.io/upload_images/4416412-7c2a08b85d21938e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后在我的产品–>设置中可以看到appId
![image](https://upload-images.jianshu.io/upload_images/4416412-c1017e33cec2e1e9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://upload-images.jianshu.io/upload_images/4416412-3f05a2b0f9233ea2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
# 五、AndroidManifest.xml配置
在AndroidMainfest.xml中进行以下配置:
1. 权限配置
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /></pre>
2. Activity配置
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">
<activity
android:name="com.tencent.bugly.beta.ui.BetaActivity" android:configChanges="keyboardHidden|orientation|screenSize|locale" android:theme="@android:style/Theme.Translucent"/></pre>
3.配置FileProvider
先在res目录新建xml文件夹,创建provider_paths.xml文件如下:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"><?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="beta_external_path" path="Download/" />
<external-path
name="beta_external_files_path" path="Android/data/" />
</paths></pre>
在AndroidManifest中配置FileProvider:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"><provider
android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileProvider" android:exported="false" android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
</provider></pre>
# 六、混淆配置:
如果开启了混淆,在proguard-rules.pro文件中添加以下混淆规则:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"># bugly混淆规则 -dontwarn com.tencent.bugly.**
-keep public class com.tencent.bugly.*{;}
tinker混淆规则 -dontwarn com.tencent.tinker.**
-keep class com.tencent.tinker.** { *; }
v4包混淆规则 -keep class android.support.*{;} </pre>
# 七、生成基准包
我们对基准包和补丁包做一个简单的测试,在基准包的MainActivity中显示一个TextView,文字为“基准包”,补丁包中将其显示文字修改为“补丁包”。
基准包:运行app,显示如下:
![image](https://upload-images.jianshu.io/upload_images/4416412-32334340bc28d81a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
运行成功后,10分钟左右、在[Bugly管理后台](https://bugly.qq.com/v2/)的我的产品–>版本管理界面可以看到如下显示:
![image](https://upload-images.jianshu.io/upload_images/4416412-f02e1be3bf734e02.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
生成基准包:
![image](https://upload-images.jianshu.io/upload_images/4416412-5afe005754c5c7a4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
点击图示位置的assembleRelease,生成基准包,完成后在下图位置可以看到app_release.apk
![image](https://upload-images.jianshu.io/upload_images/4416412-e696168c2a66eea6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在手机联网状态下,安装此基准包,否则上传补丁包时会报错:“未匹配到可应用补丁包的App版本,请确认补丁包的基线版本是否配置正确”
# 八、生成补丁包
修改MainActivity中的代码:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv_test.text = "补丁包" }
} </pre>
生成补丁包:
1.先将tinker-support.gradle中的baseApkDir修改为第七步生成的apk文件夹名
2.再将tinkerId修改为patch-1.0.0
3.然后点击下图位置的buildTinkerPatchRelease生成补丁包
![image](https://upload-images.jianshu.io/upload_images/4416412-3dc16c7b3f1affb2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
生成的补丁包在这里:
![image](https://upload-images.jianshu.io/upload_images/4416412-47a33ffde458116b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
这里注意路径不要找错了,是patch/release文件夹下的patch_signed_7zip.apk,而不是apk/tinkerPatch/release文件夹下的patch_signed_7zip.apk,如果上传apk文件夹下的补丁包,Bugly管理平台会报错:“上传失败!补丁文件缺失必需字段:Created-Time、Created-By、YaPatchType、VersionName、VersionCode、From、To,请检查补丁文件后重试!
# 九、上传补丁包,见证热更新
在[Bugly管理后台](https://bugly.qq.com/v2/)的 应用升级 --> 热更新 中,点击发布新补丁:
![image](https://upload-images.jianshu.io/upload_images/4416412-a85cb35b564fa4e9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
上传patch/release文件夹下的patch_signed_7zip.apk:
![image](https://upload-images.jianshu.io/upload_images/4416412-c229430ad4f412f3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
上传成功后,显示如下:
![image](https://upload-images.jianshu.io/upload_images/4416412-70aacdf219529870.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后我们静静等待热更新生效,生效时间大概十分钟,生效之后显示如下:
![image](https://upload-images.jianshu.io/upload_images/4416412-7f4be3c8d6eeb407.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
源码地址:[https://gitee.com/anan9303/TinkerFix](https://gitee.com/anan9303/TinkerFix)
参考地址:[https://blog.csdn.net/AlpinistWang/article/details/84248026#_8](https://blog.csdn.net/AlpinistWang/article/details/84248026#_8)