[Android][热修复]Android应用热修复之实战Tinker使用

前言

本文接上篇 [Android][热修复]Android应用热修复之基础原理篇

步骤

1 在build.gradle(project)中添加依赖

dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.tinkerpatch.sdk:tinkerpatch-gradle-plugin:1.2.2'//step 1

2 在build.gradle(module)中添加依赖

dependencies {
    //无需引入tinker的任何库,使用tinkerpatch sdk即可
    implementation 'com.tinkerpatch.sdk:tinkerpatch-android-sdk:1.2.2' //step2
    implementation "com.android.support:multidex:1.0.2"//step2
}

3 设置多分包支持

    defaultConfig {
        applicationId "com.example.richard.test"
        minSdkVersion 15
        multiDexEnabled true  //step3
    }

4 添加tinkerpatch.gradle的配置文件

apply plugin: 'tinkerpatch-support'

/**
 * TODO: 请按自己的需求修改为适应自己工程的参数
 */
def bakPath = file("${buildDir}/bakApk/")
def baseInfo = "app-1.0.0-0705-15-22-04"
def variantName = "debug"

/**
 * 对于插件各参数的详细解析请参考
 * http://tinkerpatch.com/Docs/SDK
 */
tinkerpatchSupport {
    /** 可以在debug的时候关闭 tinkerPatch **/
    /** 当disable tinker的时候需要添加multiDexKeepProguard和proguardFiles,
        这些配置文件本身由tinkerPatch的插件自动添加,当你disable后需要手动添加
        你可以copy本示例中的proguardRules.pro和tinkerMultidexKeep.pro,
        需要你手动修改'tinker.sample.android.app'本示例的包名为你自己的包名, com.xxx前缀的包名不用修改
     **/
    tinkerEnable = true
    reflectApplication = true  //使用自己的Applicaion 时应为true
    /**
     * 是否开启加固模式,只能在APK将要进行加固时使用,否则会patch失败。
     * 如果只在某个渠道使用了加固,可使用多flavors配置
     **/
    protectedApp = false
    /**
     * 实验功能
     * 补丁是否支持新增 Activity (新增Activity的exported属性必须为false)
     **/
    supportComponent = true

    autoBackupApkPath = "${bakPath}"

    appKey = "38518b564f8fc281"   //修改为自己app 的key

    /** 注意: 若发布新的全量包, appVersion一定要更新 **/
    appVersion = "1.0.0"

    def pathPrefix = "${bakPath}/${baseInfo}/${variantName}/"
    def name = "${project.name}-${variantName}"

    baseApkFile = "${pathPrefix}/${name}.apk"
    baseProguardMappingFile = "${pathPrefix}/${name}-mapping.txt"
    baseResourceRFile = "${pathPrefix}/${name}-R.txt"

    /**
     *  若有编译多flavors需求, 可以参照: https://github.com/TinkerPatch/tinkerpatch-flavors-sample
     *  注意: 除非你不同的flavor代码是不一样的,不然建议采用zip comment或者文件方式生成渠道信息(相关工具:walle 或者 packer-ng)
     **/
}

/**
 * 用于用户在代码中判断tinkerPatch是否被使能
 */
android {
    defaultConfig {
        buildConfigField "boolean", "TINKER_ENABLE", "${tinkerpatchSupport.tinkerEnable}"
    }
}

/**
 * 一般来说,我们无需对下面的参数做任何的修改
 * 对于各参数的详细介绍请参考:
 * https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
 */
tinkerPatch {
    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
    }
}

5在build.gradle中引入tinker.patch的配置

apply from: 'tinkerpatch.gradle'//step4

6 初始化tinker

在application中进行初始化

 @Override
    public void onCreate() {
        super.onCreate();
        // 我们可以从这里获得Tinker加载过程的信息
        if (BuildConfig.TINKER_ENABLE) {

            tinkerApplicationLike = TinkerPatchApplicationLike.getTinkerPatchApplicationLike();

            if(tinkerApplicationLike==null){
                tinkerApplicationLike = TinkerPatchApplicationLike.getTinkerPatchApplicationLike();
            }

            // 初始化TinkerPatch SDK
            TinkerPatch.init(tinkerApplicationLike)
                    .reflectPatchLibrary()
                    .setPatchRollbackOnScreenOff(true)
                    .setPatchRestartOnSrceenOff(true);
            TinkerPatch.with().fetchPatchUpdate(true);//每次打开应用都更新

        }
    }
  @Override
    public void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //you must install multiDex whatever tinker is installed!
        MultiDex.install(base);
    }

 没有完成,当前官网不能用

参考
https://blog.csdn.net/lbj_demo/article/details/78558353
https://www.jianshu.com/p/ee1a7a893054

https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97

猜你喜欢

转载自blog.csdn.net/xfb1989/article/details/110234260