美文网首页Android
Android热修复

Android热修复

作者: 节庆007 | 来源:发表于2020-03-05 09:30 被阅读0次

    Android中热修复的方案其实特别多,腾讯、阿里、美团等等诸多。

    本文主要介绍基于Thinker的腾讯bugly集成实现。

    热修复优点:
    • 紧急修复线上Bug
    • 方便频繁应用版本更新导致的不停上架
    热修复缺点:
    • 只能基于方法修复,而且兼容性不太好,许多机型都需要特殊处理(如小米高版本系统),不支持新增字段和修改方法,也不支持替换资源。

    具体实现:

    1. 依赖库
    'crashreport'          : 'com.tencent.bugly:crashreport_upgrade:1.3.6',                                  // bugly仓库
    'tinker'               : 'com.tencent.tinker:tinker-android-lib:1.9.13',                                 // 指定tinker依赖版本
    'bugly_natice'         : 'com.tencent.bugly:nativecrashreport:latest.release',                           // bugly_natice
    
    1. 集成方式:https://bugly.qq.com/docs/user-guide/instruction-manual-android-hotfix/?v=20200114181137

    2. Application设置

    // 安装tinker
    Beta.installTinker();
    
    // 这里实现SDK初始化,appId替换成你的在Bugly平台申请的appId
    // 调试时,将第三个参数改为true
    Bugly.init(this, "2979b36ce0", BuildConfig.ONLINE);
    
    1. tinker-support.gradle文件设置
    apply plugin: 'com.tencent.bugly.tinker-support'
    
    def bakPath = file("${buildDir}/bakApk/")
    
    /**
     * 此处填写每次构建生成的基准包目录
     */
    def baseApkDir = "app-1111-11-48-09"
    
    /**
     * 对于插件各参数的详细解析请参考,如果没有特殊需求下面的参数都可以不用更改;如果apk需要加固等可以参考具体描述设置参数
     */
    tinkerSupport {
    
        // 开启tinker-support插件,默认值true
        enable = true
        // 指定归档目录,默认值当前module的子目录tinker
        autoBackupApkDir = "${bakPath}"
        //建议设置true,用户就不用再自己管理tinkerId的命名,插件会为每一次构建的base包自动生成唯一的tinkerId,默认命名规则是versionname.versioncode_时间戳
        //具体参考https://github.com/BuglyDevTeam/Bugly-Android-Demo/wiki/Tinker-ID%E8%AF%A5%E6%80%8E%E4%B9%88%E8%AE%BE%E7%BD%AE
        autoGenerateTinkerId = true
        //tinkerId必须保证唯一性,如果两个base包的tinkerid是一样的,并且都联网激活了,那么后续补丁上传到后台的时候会出现匹配错误
        tinkerId = "base-1.7.0"
    
        // 是否启用覆盖tinkerPatch配置功能,默认值false
        // 开启后tinkerPatch配置不生效,即无需添加tinkerPatch
        overrideTinkerPatchConfiguration = true
    
        // 编译补丁包时,必需指定基线版本的apk,默认值为空
        // 如果为空,则表示不是进行补丁包的编译
        // @{link tinkerPatch.oldApk }
        baseApk =  "${bakPath}/${baseApkDir}/app-release.apk"
    
        // 对应tinker插件applyMapping
        baseApkProguardMapping = "${bakPath}/${baseApkDir}/app-release-mapping.txt"
    
        // 对应tinker插件applyResourceMapping
        baseApkResourceMapping = "${bakPath}/${baseApkDir}/app-release-R.txt"
    
    //    buildAllFlavorsDir = "${bakPath}/${baseApkDir}"
        // 是否开启加固模式,默认为false
        // isProtectedApp = true
    
        enableProxyApplication = true
    
        supportHotplugComponent = true
    
    }
    
    /**
     * 一般来说,我们无需对下面的参数做任何的修改
     * 对于各参数的详细介绍请参考:
     * https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
     */
    tinkerPatch {
        //oldApk ="${bakPath}/${appName}/app-debug.apk"
        ignoreWarning = false
        useSign = true
        dex {
            dexMode = "jar"
            pattern = ["classes*.dex"]
            loader = []
        }
        lib {
            pattern = ["lib/*/*.so"]
        }
    
        res {
            pattern = ["res/*", "r/*", "assets/*", "resources.arsc", "AndroidManifest.xml"]
            ignoreChange = []
            largeModSize = 100
        }
    
        packageConfig {
        }
        sevenZip {
            zipArtifact = "com.tencent.mm:SevenZip:1.1.10"
    //        path = "/usr/local/bin/7za"
        }
        buildConfig {
            keepDexApply = false
            //tinkerId = "1.0.1-patch"
            //applyMapping = "${bakPath}/${appName}/app-debug-mapping.txt" //  可选,设置mapping文件,建议保持旧apk的proguard混淆方式
            //applyResourceMapping = "${bakPath}/${appName}/app-debug-R.txt" // 可选,设置R.txt文件,通过旧apk文件保持ResId的分配
        }
    }
    

    打包步骤以及注意事项

    步骤

    打基准包:
    • 修改 base-1.3.1
    • 执行 android-app-Tasks-other-assembleRelease
    打修复包:
    • 设置baseApkDir
    • 修改patch-1.3.1
    • 执行 android-app-Tasks-thinker-thinkerPatchRelease

    注意事项

    1. 打基准包注意事项:
    • tinker-support.gradle下tinkerId变更。示例:base-1.3.1
    2.打修复包注意事项:
    • app/build/bakApk目录下导入基准包。
    • tinker-support.gradle下baseApkDir变更为基准包名称。示例:app-0904-14-08-41
    • tinker-support.gradle下tinkerId变更。示例:patch-1.3.1(首次修复)或者patch-1.3.1.x(多次修复)
    3.gradle版本要求:
    • classpath 'com.android.tools.build:gradle:3.4.0' gradle版本不能高于3.4.0,腾讯bugly的问题

    (后期如果有注意事项,及时更正)

    相关文章

      网友评论

        本文标题:Android热修复

        本文链接:https://www.haomeiwen.com/subject/yqftrhtx.html