Android热修复 - Tinker

官网

Tinker接入方式有两种,一种gradle方法接入(推荐),还有另外一种命令行接入,这里只使用gradle方式接入

gradle接入

这里参考了官方的demo

按照官方接入文档

(1)首先添加gradle 远程仓库依赖 jcenter

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        // TinkerPatch 插件
        classpath "com.tinkerpatch.sdk:tinkerpatch-gradle-plugin:1.2.6"
    }
}

(2)添加依赖

dependencies {
    compile("com.tinkerpatch.sdk:tinkerpatch-android-sdk:1.2.6")
}

为了方便,创建一个tinkerpatch.gradle,我在这里直接把demo里面的tinkerpatch.gradle直接复制过来,和app/build.gradle放同一级目录


然后再app/build.gradle里面在添加一些配置



(3)代码接入

新建一个SampleApplication 初始化Tinker

public class SampleApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // 我们可以从这里获得Tinker加载过程的信息
        ApplicationLike tinkerApplicationLike = TinkerPatchApplicationLike.getTinkerPatchApplicationLike();

        // 初始化TinkerPatch SDK, 更多配置可参照API章节中的,初始化SDK
        TinkerPatch.init(tinkerApplicationLike)
                .reflectPatchLibrary()
                .setPatchRollbackOnScreenOff(true)
                .setPatchRestartOnSrceenOff(true)
                .setFetchPatchIntervalByHours(3);

        // 每隔3个小时(通过setFetchPatchIntervalByHours设置)去访问后台时候有更新,通过handler实现轮训的效果
        TinkerPatch.with().fetchPatchUpdateAndPollWithInterval();
    }
}

然后build一下,在AndroidManifest.xml中引用


(4)测试使用

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.tinker3.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是一个BUG" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="go"
        android:text="修复" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void go(View view) {
        TinkerInstaller.onReceiveUpgradePatch(this, Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip.apk");

    }
}

然后我们点击assembleRelease进行打包


完成之后在build文件夹下多出一个bakApk,里面有打包好后的apk文件,先把这个apk安装到手机上


现在构建热修复的patch

打开tinkerpatch.gradle,按照下图就行修改,保持一致


然后再点击tinkerPatchRelease生成patch


这样就基本完成了,最后测试一下,把patch_signed_7zip.apk拷贝到手机根目录(放哪无所谓,这里只为方便测试,保持和MainActivity里面写的路径一致就行)

由于这里直接读取SD卡里面文件,这里记得添加权限

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


然后我们打开刚才安装的apk,点击修复,然后重启app,就可以看到结果了


遇到的坑

1、点击run报错

Error:org.gradle.internal.event.ListenerNotificationException: Failed to notify project evaluation listener.
Error:org.gradle.api.GradleException: Tinker does not support instant run mode, please trigger build by assembleDebug or disable instant run in 'File->Settings...'.


解决方法:因为不支持instant run mode,需要关闭才行,打开Settings,找到Instant Run,关闭右侧打勾选项



2、编译没通过

java.lang.RuntimeException: Unable to instantiate application com.tinkerpatch.sdk.loader.TinkerPatchReflectApplication: java.lang.IllegalStateException: java.lang.ClassNotFoundException: null

解决方法:检查一下是不是在Application中忘记初始化Tinker,或者在AndroidManifest.xml中没有引用Application



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值