美文网首页
Tinker使用

Tinker使用

作者: pray_daidai | 来源:发表于2019-07-05 16:51 被阅读0次

热修复越来越普及,同时也为开发者们提供了便利,tinker早有耳闻,却一直没接触过,借这次项目中的需求来记录一下我的使用

准备

tinker

参考的文章
Android热修复Tinker集成

我自己集成的是tinkerpatch,更符合我们的开发流程,tinkerpatch官网
这个网站会帮助我们监听补丁的下发,合成等情况,不用每次在把生成的补丁包放在根目录。

注意事项
需要打开文件存储的权限

步骤

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.boying.secondtest"
        minSdkVersion 15
        multiDexEnabled true  //step3
    }

4 添加tinker.patch的配置

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);
    }

7 测试

在主页的布局中写一个textview,修改前显示的内容为 这是测试程序
运行到手机上后,查看build/bakApk/


微信图片_20190705163016.png

修改tinkerpatch.gradle中的参数


微信图片_20190705163335.png
修改textview的内容,这是测试tinker
点击右侧的gradle
gradle.png
apk.png

8 发布到tinkerpatch的官网

fabu.png

9

重新打开之前安装的应用.打开后关闭并清空后台程序,再次重新发开,可以发现更改成功了

demo地址

相关文章

网友评论

      本文标题:Tinker使用

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