Android(安卓)Bugly 热修复 快速接入
介绍
1、Bugly目前采用微信Tinker
2、Bugly已经搭建了管理后台,对每个版本补丁进行管理。
3、实现自动下载补丁包、合成、并应用补丁。接入Bugly
1、添加插件依赖,工程根目录下“build.gradle”文件中添加:
buildscript { repositories { jcenter() } dependencies { // tinker gradle插件 classpath ('com.tencent.tinker:tinker-patch-gradle-plugin:1.7.5') // tinkersupport插件 classpath "com.tencent.bugly:tinker-support:latest.release" }}
dependencies { // 多dex配置 compile "com.android.support:multidex:1.0.1" compile 'com.tencent.bugly:crashreport_upgrade:latest.release'//其中latest.release指代最新版本号,也可以指定明确的版本号,例如1.2.0 }
2、集成SDK gradle配置
在app module的“build.gradle”文件中添加(示例配置):
3、在app module的“build.gradle”文件中添加
apply plugin: 'com.tencent.bugly.tinker-support'tinkerSupport { // 开启tinker-support插件,默认值true enable = true // 是否编译完成后,归档apk到指定目录,默认值false autoBackupApk = false // 指定归档目录,默认值当前module的子目录tinker // backupApkDir = 'tinker' // 是否启用覆盖tinkerPatch配置功能,默认值false // 开启后tinkerPatch配置不生效,即无需添加tinkerPatch overrideTinkerPatchConfiguration = true // 编译补丁包时,必需指定基线版本的apk,默认值为空 // 如果为空,则表示不是进行补丁包的编译 // @{link tinkerPatch.oldApk } baseApk = "YOUR_OLD_APK" // 对应tinker插件applyMapping baseApkProguardMapping = "YOUR_OLD_APK_PROGUARD_FILE" // 对应tinker插件applyResourceMapping baseApkResourceMapping = "YOUR_OLD_APK_RES_MAPPING" tinkerId = "YOUR_TINKER_ID"}
更详细的配置项参考 tinker-support配置说明
4、自定义Application
public class SampleApplication extends TinkerApplication { public SampleApplication() { super(ShareConstants.TINKER_ENABLE_ALL, "xxx.xxx.SampleApplicationLike", "com.tencent.tinker.loader.TinkerLoader", false); }}
将定义的Application 配置进AndroidManifest.
5、自定义ApplicationLike
注:此自定义的ApplicationLike 就是前面第四部Application 构造函数中”xxx.xxx.SampleApplicationLike”
public class SampleApplicationLike extends DefaultApplicationLike { public static final String TAG = "Tinker.SampleApplicationLike"; public SampleApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent, Resources[] resources, ClassLoader[] classLoader, AssetManager[] assetManager) { super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent, resources, classLoader, assetManager); } @Override public void onCreate() { super.onCreate(); // 这里实现SDK初始化,appId替换成你的在Bugly平台申请的appId Bugly.init(getApplication(), "appid", true); } @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public void onBaseContextAttached(Context base) { super.onBaseContextAttached(base); // you must install multiDex whatever tinker is installed! MultiDex.install(base); // 安装tinker // TinkerManager.installTinker(this); 替换成下面Bugly提供的方法 Beta.installTinker(this); } @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public void registerActivityLifecycleCallback(Application.ActivityLifecycleCallbacks callbacks) { getApplication().registerActivityLifecycleCallbacks(callbacks); }}
注: 以前所有在Application的实现必须要全部拷贝到这里,在onCreate方法调用SDK的初始化方法,在onBaseContextAttached中调用Beta.installTinker(this);。
6、配置相关
权限配置
<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" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Activity配置
<activity android:name="com.tencent.bugly.beta.ui.BetaActivity" android:theme="@android:style/Theme.Translucent" />
配置FileProvider(Android N之后配置)
<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>
${applicationId}请替换为您的包名,例如com.bugly.upgrade.demo。这里要注意一下,FileProvider类是在support-v4包中的,检查你的工程是否引入该类库。
在res目录新建xml文件夹,创建provider_paths.xml文件如下:
<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="beta_external_path" path="Download/"/> <external-path name="beta_external_files_path" path="Android/data/"/>paths>
这里配置的两个外部存储路径是升级SDK下载的文件可能存在的路径,一定要按照上面格式配置,不然可能会出现错误。
混淆配置
为了避免混淆SDK,在Proguard混淆文件中增加以下配置:
-dontwarn com.tencent.bugly.**-keep public class com.tencent.bugly.**{*;}
如果你使用了support-v4包,你还需要配置以下混淆规则:
-keep class android.support.**{*;}
- 使用
1、编译基准包
执行assembleRelease编译生成基准包:
这个会在build/outputs/bakApk路径下生成每次编译的基准包、混淆配置文件、资源Id文件,如下图所示:
启动apk,上报联网数据
我们每次冷启动都会请求补丁策略,会上报当前版本号和tinkerId,这样我们后台就能将这个唯一的tinkerId对应到一个版本,大家测试的时候可以打开logcat查看我们的日志,如下图所示:
以上 是 基准包 也就是我们正常上线的包, 如果 遇到bug 需要发布补丁包 需要单独打出pacth 包。
修复完代码 执行构建补丁包的task
生成的补丁包在build/outputs/patch目录下:
上传补丁包到平台
以下是官方地址以及文档:
Bugly Android热更新使用指南
Bugly Android热更新详解
官方Demo
Tinker WIKI
原生 Tinker
更多相关文章
- Android探索:全面分析Activity的生命周期&IntentFilter匹配规则
- Walle —— Android多渠道打包神器
- Android模拟器的属性配置介绍
- 如何让手机横竖屏切换不重走Activity生命周期
- 安卓编译出错:jre\bin\java.exe'' finished with non-zero exit
- android版本7.0以上无法抓取https
- android 编译java.util.zip.ZipException: error in opening zip
- Android(安卓)ndk报错error: format not a string literal and n
- Android(安卓)混淆 以及 java代码方式实现混淆配置