Android Tinker 基本使用教程

为什么使用Tinker

当前市面的热补丁方案有很多,其中比较出名的有阿里的AndFix、美团的Robust以及QZone的超级补丁方案。但它们都存在无法解决的问题,这也是正是我们推出Tinker的原因。

这个Tinker官方给出的一个图表:

- Tinker QZone AndFix Robust
类替换 yes yes no no
So替换 yes no no no
资源替换 yes yes no no
全平台支持 yes yes yes yes
即时生效 no no yes yes
性能损耗 较小 较大 较小 较小
补丁包大小 较小 较大 一般 一般
开发透明 yes yes no no
复杂度 较低 较低 复杂 复杂
gradle支持 yes no no no
Rom体积 较大 较小 较小 较小
成功率 较高 较高 一般 最高

1.怎么使用:

Tinker 的使用还是比较简单的,这是Tinker官方的github链接 : https://github.com/Tencent/tinker.

官方上面的文档很久没更新了,可以直接下载他最新的demo,按照demo上的来集成。

2. 导入依赖:

注意,这里有个坑,gradle版本不能用超过4.0.0版本的,不然构建会出错,tinker还不支持超过4.0.0版本,我这里用的3.5.3版本的

gradle依赖
allprojects {
   
    dependencies {
   
    
        classpath "com.android.tools.build:gradle:3.5.3"
        classpath "com.tencent.tinker:tinker-patch-gradle-plugin:1.9.1"
    }
}

dependencies {
   
    //可选,如果不用注解构建application可不用
//    annotationProcessor("com.tencent.tinker:tinker-android-anno:1.9.14.5") { changing = true }
//    compileOnly("com.tencent.tinker:tinker-android-anno:1.9.14.5") { changing = true }

    //tinker's main Android lib
    implementation('com.tencent.tinker:tinker-android-lib:1.9.14.5')
    
	//dex分包
	implementation "androidx.multidex:multidex:2.0.1"
}

3. build.gradle配置:

在你的build.gradle加上以下配置 。tinker.gradle是我新建的一个gradle,目的是将tinker配置独立出来

${TINKER_ID}是我在 gradle.properties里面配置的一个常量TINKER_ID=100
每次发布新基准包都需要修改此版本号 注意:(发布补丁包不需要修改)

apply from: 'tinker.gradle'
android {
   
    
    ........

    defaultConfig {
   
		.......

		javaCompileOptions {
    annotationProcessorOptions {
    includeCompileClasspath = true } }
        multiDexEnabled true
        buildConfigField "String", "MESSAGE", "\"I am the base apk\""
        buildConfigField "String", "TINKER_ID", "\"${TINKER_ID}\""
        buildConfigField "String", "PLATFORM", "\"all\""
	}

	dexOptions {
   
        jumboMode = true
    }
}

tinker.gradle配置信息:可直接复制, 后面只需要修改ext {}里面的内容,其他的无需要可不用修改




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

ext {
   
    //for some reason, you may want to ignore tinkerBuild, such as instant run debug build?
    tinkerEnabled = true
    //基准apk路径
    tinkerOldApkPath = "${bakPath}/app-debug-0828-10-39-58.apk"
    //未开启混淆,则不需要填写
    tinkerApplyMappingPath = "${bakPath}/"
    //基准apk中的R文件路径
    tinkerApplyResourcePath = "${bakPath}/app-debug-0828-10-39-58-R.txt"
    //如果你修复了res文件,需要指定你bug版本的R.txt文件
    tinkerBuildFlavorDirectory = "${bakPath}/"

}

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 buildWithTinker() {
   
    return hasProperty("TINKER_ENABLE") ? TINKER_ENABLE : ext.tinkerEnabled
}
def getTinkerBuildFlavorDirectory() {
   
    return ext.tinkerBuildFlavorDirectory
}



if (buildWithTinker()) {
   
    apply plugin: 'com.tencent.tinker.patch'

    tinkerPatch {
   
        /**
         * 默认为null
         * 将旧的apk和新的apk建立关联
         * 从build / bakApk添加apk
         */
        oldApk = getOldApkPath()
        /**
         * 可选,默认'false'
         *有些情况下我们可能会收到一些警告
         *如果ignoreWarning为true,我们只是断言补丁过程
         * case 1:minSdkVersion低于14,但是你使用dexMode与raw。
         * case 2:在AndroidManifest.xml中新添加Android组件,
         * case 3:装载器类在dex.loader {}不保留在主要的dex,
         *          它必须让tinker不工作。
         * case 4:在dex.loader {}中的loader类改变,
         *          加载器类是加载补丁dex。改变它们是没有用的。
         *          它不会崩溃,但这些更改不会影响。你可以忽略它
         * case 5:resources.arsc已经改变,但是我们不使用applyResourceMapping来构建
         */
        ignoreWarning = true

        /**
         * 可选,默认为“true”
         * 是否签名补丁文件
         * 如果没有,你必须自己做。否则在补丁加载过程中无法检查成功
         * 我们将使用sign配置与您的构建类型
         */
        useSign = true

        /**
         * 可选,默认为“true”
         * 是否使用tinker构建
         */
        tinkerEnable = buildWithTinker()

        /**
         * 警告,applyMapping会影响正常的android build!
         */
        buildConfig {
   
            /**
             * 可选,默认为'null'
             * 如果我们使用tinkerPatch构建补丁apk,你最好应用旧的
             * apk映射文件如果minifyEnabled是启用!
             * 警告:你必须小心,它会影响正常的组装构建!
             */
            applyMapping = getApplyMappingPath()
            /**
             * 可选,默认为'null'
             * 最好保留R.txt文件中的资源id,以减少java更改
             */
            applyResourceMapping = getApplyResourceMappingPath()

            /**
             * 必需,默认'null'
             * 因为我们不想检查基地apk与md5在运行时(它是慢)
             * tinkerId用于在试图应用补丁时标识唯一的基本apk。
             * 我们可以使用git rev,svn rev或者简单的versionCode。
             * 我们将在您的清单中自动生成tinkerId
             */
            tinkerId = TINKER_ID.toInteger()

            /**
             * 如果keepDexApply为true,则表示dex指向旧apk的类。
             * 打开这可以减少dex diff文件大小。
             */
            keepDexApply = false

            /**
             * optional, default 'false'
             * Whether tinker should treat the base apk as the one being protected by app
             * protection tools.
             * If this attribute is true, the generated patch package will contain a
             * dex including all changed classes instead of any dexdiff patch-info files.
             */
            isProtectedApp = false

            /**
             * optional, default 'false'
             * Whether tinker should support component hotplug (add new component dynamically).
             * If this attribute is true, the component added in new apk will be available after
             * patch is successfully loaded. Otherwise an error would be announced when generating patch
             * on compile-time.
             *
             * <b>Notice that currently this feature is incubating and only support NON-EXPORTED Activity</b>
             */
            supportHotplugComponent = false
        }

        dex {
   
            /**
             * 可选,默认'jar'
             * 只能是'raw'或'jar'。对于原始,我们将保持其原始格式
             * 对于jar,我们将使用zip格式重新包装dexes。
             * 如果你想支持下面14,你必须使用jar
             * 或者你想保存rom或检查更快,你也可以使用原始模式
             */
            dexMode = "jar"

            /**
             * 必需,默认'[]'
             * apk中的dexes应该处理tinkerPatch
             * 它支持*或?模式。
             */
            pattern = ["classes*.dex",
                       "assets/secondary-dex-?.jar"]
            /**
             * 必需,默认'[]'
             * 警告,这是非常非常重要的,加载类不能随补丁改变。
             * 因此,它们将从补丁程序中删除。
             * 你必须把下面的类放到主要的dex。
             * 简单地说,你应该添加自己的应用程序{@code tinker.sample.android.SampleApplication}
             * 自己的tinkerLoader,和你使用的类
             */
            loader = [
                    //use sample, let BaseBuildInfo unchangeable with tinker
                    <
### VSCode Vue3 开发常用插件推荐 #### 编辑增强类插件 为了提高编辑效率,一些插件提供了诸如自动补全、语法高亮等功能。例如 `Auto Rename Tag` 插件能够在修改HTML/XML标签时同步更新其闭合标签[^2]。 #### 主题与界面优化 对于视觉体验有需求的开发者来说,可以选择像 `Atom One Light Theme` 或者 `Cobalt2 Theme Official` 这样的主题来美化工作环境[^1]。另外还有 `VSCode Great Icons` 提供更美观的图标支持[^1]。 #### 中文语言包 为了让国内用户更好地理解和操作VSCode,安装 `Chinese (Simplified) (简体中文) Language Pack for Visual Studio Code` 是很有必要的,它能够使整个IDE界面汉化。 #### 代码片段加速开发 `Vue VSCode Snippets` 和 `Vue 3 Snippets` 都是非常实用的选择,前者通过预设好的模板让开发者可以迅速构建起基本结构;后者则专注于为最新版本框架定制专属片段集合[^3]。 #### 导航辅助工具 当项目规模逐渐增大之后,利用 `Vue Peek` 实现快速定位组件定义位置变得尤为重要。该功能允许使用者仅需简单点击就能直达目标源码所在之处。 #### 路径处理解决方案 针对模块间相互引用频繁的情况,`Path Intellisense` 的存在无疑大大简化了这一过程——无论是相对还是绝对路径都能得到智能提示。而 `file-jump` 功能同样实现了别名路径下的便捷跳转[^4]。 #### 类型感知能力加强 考虑到TypeScript日益普及的趋势,在编写基于TSX/JSX语法糖封装后的单文件组件(SFCs)时,借助于 `TypeScript Vue Plugin (Volar)` 及 `Vue Language Features (Volar)` 来获得更好的类型推断效果显得尤为关键。 #### 图片资源管理 如果涉及到大量图像素材,则不可错过 `Image preview` ,它可以即时显示图片内容而不必离开当前窗口去寻找原图。 #### 版本控制集成 最后但并非最不重要的是,保持良好的Git实践习惯始终是软件工程领域内不可或缺的一环。因此建议加入 `SVN` 或其他形式的SCM客户端以便随时追踪变更记录并协同作业。 ```json { "editor.codeActionsOnSave": { "source.fixAll.eslint": true, "source.organizeImports": true }, "[vue]": { "editor.defaultFormatter": "Vue.vscode-vue-languageservice" } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值