tinker热修复, 一步一步集成, 特别简单

本文介绍如何通过Gradle配置实现Tinker热更新功能。包括旧版APK生成、补丁制作及应用流程,并提供代码示例。适用于希望快速迭代Android应用的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

gradle 的配置:
tinker官方demo: 传送门
tinker官方demo
可以直接用as导入, 运行.
(运行会出现一个错误tinker_id没获取到, 只需将下面写死或者获取git version即可)
这里写图片描述

运行步骤:
1, 先使用gradle中的build生成old apk
2, 在项目工程app->build->bakapk找到生成的old apk
3, 将生成old.apk名字复制替换掉gradle中
ext>tinkerOldApkPath/tinkerApplyMappingPath/tinkerApplyResourcePath
这里写图片描述
4, 修改项目工程代码,或者布局
5, 使用gradle中的tinker中的tinkerPatchDebug/tinkerPatchRelease生成补丁
6, 在项目工程中app->build->outputs->tinkerpatch中找到生成的补丁
(先安装old apk,然后将补丁放入手机根目录, 点击加载补丁, 重启app即可)

project.gralde:

buildscript {
    repositories {
        mavenLocal()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.2'
        classpath "com.tencent.tinker:tinker-patch-gradle-plugin:${TINKER_VERSION}"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app.gradle:
将官方gradle 复制到自己项目里面(所有)
(TINKER_VERSION: 在gradle.properties中TINKER_VERSION=1.8.0)

java代码:
除app包可以不复制以外, 其它包全部copy到自己工程里面. 如:
这里写图片描述

Application:
1. 新建ApplicationLike继承DefaultApplicationLike, 并且添加注解
这里写图片描述
(红框代码为自己生成的application, 代码重新构建后, 可以在manifast添加)
2. 重写方法onBaseContextAttached, 可以在里面实现我们初始化方法, 一定要初始化tinker

SampleApplicationContext.application = getApplication();
SampleApplicationContext.context = getApplication();
TinkerManager.setTinkerApplicationLike(this);

TinkerManager.initFastCrashProtect();
//should set before tinker is installed
TinkerManager.setUpgradeRetryEnable(true);

//optional set logIml, or you can use default debug log
TinkerInstaller.setLogIml(new MyLogImp());

//installTinker after load multiDex
//or you can put com.tencent.tinker.** to main dex
TinkerManager.installTinker(this);
Tinker tinker = Tinker.with(getApplication());
  1. 加载补丁
TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(),Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip.apk");
//patch_signed_7zip: 补丁名称

demo传送门

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值