Android热修复框架Tinker的使用(一)

    官网文档:http://tinkerpatch.com/Docs/SDK
    官网demo:https://github.com/TinkerPatch/tinkerpatch-sample

    本次使用demo:https://download.csdn.net/download/u010326875/12002859

                                             按照官网文档来配置tinker的使用

第一步、 添加 gradle 插件依赖:

第二步、 集成 TinkerPatch SDK:

第三步、 配置 tinkerpatchSupport 参数(tinkerpatch.gradle文件的内容):

第四步、 初始化 TinkerPatch SDK:

第五步、 编写android页面,操作触发补丁包下发

第六步、 使用步骤

 

具体说明:

第一步、 添加 gradle 插件依赖:

buildscript {
	repositories {
		mavenCentral() //配置上这个,不然有可能会加载插件不成功
		google()
		jcenter()
	}
	dependencies {
		// TinkerPatch 插件
		classpath "com.tinkerpatch.sdk:tinkerpatch-gradle-plugin:1.2.14"
	}
}

第二步、 集成 TinkerPatch SDK:

apply from: 'tinkerpatch.gradle' //为了简单方便,我们将 TinkerPatch 相关的配置都放于 tinkerpatch.gradle 中

dependencies {
	// 若使用annotation需要单独引用,对于tinker的其他库都无需再引用
	compileOnly("com.tinkerpatch.tinker:tinker-android-anno:1.9.14") { changing = true }
	implementation("com.tinkerpatch.sdk:tinkerpatch-android-sdk:${TINKER_VERSION}")
	implementation 'com.android.support:multidex:1.0.3'
}

              TINKER_VERSION 在 gradle.properties 文件中声明:TINKER_VERSION = 1.2.14

   自行配置gradle的签名信息等:

    signingConfigs {
        release {
            try {
                keyAlias 'bai'
                keyPassword '123456'
                storeFile file('E:/test/bai.jks')
                storePassword '123456'
            } catch (ex) {
                throw new InvalidUserDataException(ex.toString())
            }

        }
        debug {
            storeFile file('E:/test/bai.jks')
            keyAlias 'bai'
            keyPassword '123456'
            storePassword '123456'
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
            minifyEnabled false
            signingConfig signingConfigs.debug
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

第三步、 配置 tinkerpatchSupport 参数(tinkerpatch.gradle文件的内容):

apply plugin: 'tinkerpatch-support'

/**
 * TODO: 请按自己的需求修改为适应自己工程的参数
 */
def bakPath = file("${buildDir}/bakApk/")
def baseInfo = "app-1.0.0-1112-12-49-34"
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
	/**
	 * 是否开启加固模式,只能在APK将要进行加固时使用,否则会patch失败。
	 * 如果只在某个渠道使用了加固,可使用多flavors配置
	 **/
	protectedApp = false
	/**
	 * 实验功能
	 * 补丁是否支持新增 Activity (新增Activity的exported属性必须为false)
	 **/
	supportComponent = true

	autoBackupApkPath = "${bakPath}"

	appKey = "22a3f0085f80f30b"

	/** 注意: 若发布新的全量包, 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
	}
}

第四步、 初始化 TinkerPatch SDK:

          1. reflectApplication = true 的情况:

若我们使用 reflectApplication 模式,我们无需为接入 Tinker 而改造我们的 Application 类。初始化 SDK 可参考 tinkerpatch-easy-sample 中的 SampleApplication 类.

public class SampleApplication extends Application {

	...

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

		// 初始化TinkerPatch SDK, 更多配置可参照API章节中的,初始化SDK
		TinkerPatch.init(tinkerApplicationLike)
			.reflectPatchLibrary()
			.setPatchRollbackOnScreenOff(true)
			.setPatchRestartOnSrceenOff(true)
			.setFetchPatchIntervalByHours(3);

		// 每隔3个小时(通过setFetchPatchIntervalByHours设置)去访问后台时候有更新,通过handler实现轮训的效果
		TinkerPatch.with().fetchPatchUpdateAndPollWithInterval();
	}

	...
	
我们将 Tinker 加载补丁过程的结果存放在 TinkerPatchApplicationLike 中

          2. reflectApplication = false 的情况 :

若我们已经完成了应用的 Application 改造,即将 Application 的逻辑移动到 ApplicationLike类中。我们可以参考 tinkerpatch-sample 中的 SampleApplicationLike 类.

public class SampleApplicationLike extends DefaultApplicationLike {

	...

	@Override
	public void onCreate() {
		super.onCreate();
		// 初始化TinkerPatch SDK, 更多配置可参照API章节中的,初始化 SDK
		TinkerPatch.init(this)
			.reflectPatchLibrary()
			.setPatchRollbackOnScreenOff(true)
			.setPatchRestartOnSrceenOff(true)
			.setFetchPatchIntervalByHours(3);

		// 每隔3个小时(通过setFetchPatchIntervalByHours设置)去访问后台时候有更新,通过handler实现轮训的效果
		TinkerPatch.with().fetchPatchUpdateAndPollWithInterval();
	}

	...

}
注意:初始化的代码建议紧跟 super.onCreate(),并且所有进程都需要初始化,已达到所有进程都可以被 patch 的目的

如果你确定只想在主进程中初始化 tinkerPatch,那也请至少在 :patch 进程中初始化,否则会有造成 :patch 进程crash,无法使补丁生效

          使用reflectApplication = false 要自动生动生成application:

          对的

         Tips:OnBaseContextAttached 和 OnCreate上下两个方法里的内容时重复的,用一个就行;自己的逻辑也一样,放哪一个方法里都可以!

第五步、编写android页面,或者方法,去触发更新补丁包的操作:

          

第六步、 使用步骤

       1、 运行 assembleRelease task 构建基准包(请在发布前确保更新tinkerpatchSupport中的appVersion),
            tinkerPatch会基于你填入的autoBackupApkPath自动备份基础包信息到相应的文件夹,包含:apk文件、
            R.txt文件和mapping.txt文件 (注:mapping.txt是proguard的产物,如果你没有开启proguard则不会有这个文件)

            111

           执行生成基础包后的结果:

           基础包
           注意:测试机上要先安装基准包,为了测试
    
      2 、若想发布补丁包, 只需将自动保存下来的文件分别填到tinkerpatchSupport中的baseApkFile、baseProguardMappingFile 和baseResourceRFile 参数中;

           //这里配置对应的补丁包对应老的基础包文件,我这里为了方便,把路径和文件名称改成了和基础包相同的名字,用来测试。也可以自己指定文件路径,然后对应的文件信息在 tinkerpatch.gradle中做好对应的匹配就行
           def bakPath = file("${buildDir}/bakApk/")
           def baseInfo = "app-1.0.0-1128-10-44-32"
           def variantName = "release"
            结果

           tinkerpatchSupport中的三个路径配置:

            
      3、运行 tinkerPatchRelease task 构建补丁包,补丁包将位于 build/outputs/tinkerPatch下

           补丁包:patch_signed_7zip.apk

      4、 在tinker官网平台上,操作上传发布补丁包:

           tinker平台代理管理:http://www.tinkerpatch.com/Apps/index
           22
           在自己的APP管理页面,去添加要修复的版本号,然后上传补丁包,确定无误,对应发布操作。

           (上传补丁包成功后,在基础包的app 页面,点击文字去执行更新)

           
           333
           参考:https://blog.csdn.net/sw1995126/article/details/82683910

总结:第一次使用tinker有错误的地方请指出,谢谢!  其实官网已经写得很详细了,大家可以参考官网去操作,我这里是为了自己记录一下,方便以后操作有个自己的思路。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漠天515

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值