热修复Tinker接入问题与解决2022年

本文详细介绍了如何在Android Studio北极狐版本的项目中接入微信Tinker热修复功能,包括解决AGP7.0不支持的问题、添加依赖、配置tinker.gradle文件、代码接入、权限处理以及打包流程。通过实例演示了如何打基准包和补丁包,以及加载补丁包的步骤,为开发者提供了一份完整的接入教程。

背景:

因为项目要接入热修复功能,最开始选择了腾讯Bugly,毕竟有微信那么大的用户体量作背书,而且提供了版本管理平台方便版本维护。照着官方文档和Github的Issues的帮助,费了一番功夫总算接入成功,并且打基准包安装和上传补丁包修复成功。

        本来挺高兴的,但......我们AndroidStudio用的是北极狐版本,而且项目使用的Gradle和AGP都是7.0版本。当时Tinker官方版本都不支持AGP7.0。好在等了一周,在2022年1月份微信更新了版本v1.9.14.19​​​​​​ 支持了AGP7.0和R8。但是Bugly上次更新还是2021年的上半年,更新支持已经不知道何年何月了。所以我转而只接入微信Tinker,本文也只讲述本次接入所遇到的问题和解决办法。在服务器上进行版本管理维护暂不处理。

开发环境:

  • Window10-64位(因为也用Mac M1开发,所以问题方面的解决也有涉及M1芯片相关的。)
  • AndroidStudio BumbleBee build February 2,2022
  • Gradle Version 7.2
  • AGP Version 7.1.0
  • 项目compileSdk和targetSdk都为31(涉及到的有加载补丁包没有权限问题)

正式接入:

一、SDK接入:

(1)插件依赖,项目的build.gradle文件中添加如下内容:

buildscript {
    dependencies {
        classpath ("com.tencent.tinker:tinker-patch-gradle-plugin:1.9.14.19")
    }
}

⚠️注意:

  • 插件版本为1.9.14.19,最新版本可以查看链接插件版本 ,下的最新版本,注意:排序并非按照版本顺序,注意发布时间。
  • 代码块添加在“plugins {}”代码块之前,不然会提示错误信息“all buildscript {} blocks must appear before any plugins {} blocks in the script”。

(2)app目录下的build.gradle引入如下依赖:

    //dex分包
    implementation "androidx.multidex:multidex:2.0.1"
    //可选,用于生成application类
    annotationProcessor('com.tencent.tinker:tinker-android-anno:1.9.14.19')
    compileOnly('com.tencent.tinker:tinker-android-anno:1.9.14.19')
    //tinker的核心库
    implementation('com.tencent.tinker:tinker-android-lib:1.9.14.19')

⚠️注意:依赖的版本通常是和第一步的插件版本一致的,具体版本可查看链接依赖版本

(3)引入插件:

apply plugin: 'com.tencent.tinker.patch'

(4)项目的gradle.properties文件添加如下属性:

TINKER_ID = 1.0

⚠️注意:tinkerId一般为每次发版的版本号,补丁包是根据tinkerId匹配基准包的。之后会在app的build.gradle当中引用该属性。 

(5)将官方demo中的文件【tinker_multidexkeep.pro】拷贝到自己项目的app目录下。

⚠️注意:该文件内有一处需要修改成自己内容的地方,需要将该文件内替换为自己在继承自“DefaultApplicationLike”类中声明的application。如下:

(6)在官方demo中,将打包的配置都写在了app下的build.gradle当中,这里将这些内容独立出来,app目录下创建tinker.gradle文件。并在app下的build.gradle中引入:

// 引入tinker配置
apply from: "tinker.gradle"

因为官方demo当中的gradle语法属性有些已经废弃了,所以【tinker.gradle】文件的具体内容如下:

def bakPath = file("${buildDir}/bakApk/")

/**
 * you can use assembleRelease to build you base apk
 * use tinkerPatchRelease -POLD_APK=  -PAPPLY_MAPPING=  -PAPPLY_RESOURCE= to build patch
 * add apk from the build/bakApk
 */
ext {
    //for some reason, you may want to ignore tinkerBuild, such as instant run debug build?
    tinkerEnabled = true

    //for normal build
    //打补丁包时候这里配置基准包的全称
    tinkerOldApkPath = "${bakPath}/app-debug-0211-11-33-02.apk"
    //打Release补丁包时候这里配置基准包时候生成的mapping文件proguard mapping file to build //patch apk
    tinkerApplyMappingPath = "${bakPath}/"
    //打补丁包时候这里配置基准包时候生成的文件全称resource R.txt to build patch apk, must input //if there is resource changed
    tinkerApplyResourcePath = "${bakPath}/app-debug-0211-11-33-02-R.txt"

    //多渠道包时候的输出目录only use for build all flavor, if not, just ignore this field
    tinkerBuildFlavorDirectory = "${bakPath}/app-1018-17-32-47"
}

def gitSha() {
    try {
        String gitRev = 'git rev-parse --short HEAD'.execute(null, project.rootDir).text.trim()
        if (gitRev == null) {
            throw new GradleException("can't get git rev, you should add git to system path or just input test value, such as 'testTinkerId'")
        }
        return gitRev
    } catch (Exception e) {
        throw new GradleException("can't get git rev, you should add git to system path or just input test value, such as 'testTinkerId'")
    }
}

def getOldApkPath() {
    return hasProperty("OLD_APK") ? OLD_APK : ext.tinkerOldApkPath
}

def getApplyMappingPath() {
    return hasProperty("APPLY_MAPPING") ? APPLY_MAPPING : ext.tinkerApplyMappingPath
}

def getApplyResourceMappingPath() {
    return hasProperty("APPLY_RESOURCE") ? APPLY_RESOURCE : ext.tinkerApplyResourcePath
}

def getTinkerIdValue() {
    return hasProperty("TINKER_ID") ? TINKER_ID : gitSha()
}

def buildWithTinker() {
    return hasProperty("TINKER_ENABLE") ? Boolean.parseBoolean(TINKER_ENABLE) : ext.tinkerEnabled
}

def getTinkerBuildFlavorDirectory() {
    return ext.tinkerBuildF
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值