Bugly实现热修复采用的是微信Tinker技术。对Tinker进行了简化封装。
Bugly热修复官方文档
添加插件依赖
工程根目录下“build.gradle”文件中添加:
buildscript { repositories { jcenter() } dependencies { // tinkersupport插件,其中latest.release指代最新版本号,也可以指定明确的版本号,例如1.0.3 classpath "com.tencent.bugly:tinker-support:latest.release" }}gradle配置
在app module的“build.gradle”文件中添加(示例配置):
dependencies { compile "com.android.support:multidex:1.0.1" compile 'com.tencent.bugly:crashreport_upgrade:latest.release'}在app module的“build.gradle”文件中添加:
// 依赖插件脚本apply from: 'tinker-support.gradle'tinker-support.gradle内容如下所示(示例配置):
需要在同级目录下创建tinker-support.gradle这个文件
apply plugin: 'com.tencent.bugly.tinker-support'def bakPath = file("${buildDir}/bakApk/")def appName = "app-0111-15-18-41"/*** 对于插件各参数的详细解析请参考*/tinkerSupport { // 开启tinker-support插件,默认值true enable = true // 指定归档目录,默认值当前module的子目录tinker autoBackupApkDir = "${bakPath}" // 是否启用覆盖tinkerPatch配置功能,默认值false // 开启后tinkerPatch配置不生效,即无需添加tinkerPatch overrideTinkerPatchConfiguration = true // 编译补丁包时,必需指定基线版本的apk,默认值为空 // 如果为空,则表示不是进行补丁包的编译 // @{link tinkerPatch.oldApk } baseApk = "${bakPath}/${appName}/app-release.apk" // 对应tinker插件applyMapping baseApkPRoguardMapping = "${bakPath}/${appName}/app-release-mapping.txt" // 对应tinker插件applyResourceMapping baseApkResourceMapping = "${bakPath}/${appName}/app-release-R.txt" // 唯一标识当前版本 tinkerId = "1.0.1-base" // 是否开启代理application,设置之后无须改造Application,默认为false enableProxyApplication = false}enableProxyApplication = false 的情况
自定义Applicationpublic class SampleApplication extends TinkerApplication { public SampleApplication() { super(ShareConstants.TINKER_ENABLE_ALL, "com.yiba.test.buglypatch.SampleApplicationLike", "com.tencent.tinker.loader.TinkerLoader", false); }}自定义ApplicationLikepublic 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(), "900029763", 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); }}配置FileProvider(Android N之后配置)
见文档
为了避免混淆SDK,在Proguard混淆文件中增加以下配置:
-dontwarn com.tencent.bugly.**-keep public class com.tencent.bugly.**{*;}如果你使用了support-v4包,你还需要配置以下混淆规则:
-keep class android.support.**{*;}基准包就是原先运行有bug的包。
点击Android Studio右上角的Gradle按钮,找到项目的assembleRelease任务,双击执行assembleRelease任务。
任务执行完成后,会在build的目录下生成如下文件:
修复前代码:
public class BugClass { public String bug() { String str = "This is a bug"; str = null; Log.e("zhang", "BugClass --> bug--> str length :" + str.length()); return str; }}修复后代码:
public class BugClass { public String bug() { String str = "This is a bug";// str = null; Log.e("zhang", "BugClass --> bug--> str length :" + str.length()); return str + " , fixed!!!"; }}调用代码:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { BugClass bugClass = new BugClass(); String bug = bugClass.bug(); Toast.makeText(MainActivity.this, bug, Toast.LENGTH_SHORT).show(); } }); }}执行生成补丁包的任务操作:
任务执行完之后,会生成3个文件,其中patch_signed_7zip.apk是我们需要的补丁包
具体见bugly的官方文档。
上传了补丁包可能不会立马生效,多试几次就好
Demo
新闻热点
疑难解答