微信Tinker热修复
        Tinker刚出来的时候倒腾过一次,今天心血来潮重新倒腾了一次,这篇博客就当做是笔记了O(∩_∩)O~
        Tinker是微信开源的Android热补丁方案,它支持动态下发代码、So库以及资源,让应用能够在不需要重新安装的情况下实现更新。当然,你也可以使用Tinker来更新你的插件。
        同时,还有许多其他公司使用的热补丁方案,如比如QZone,AndFix,Dexposed等等;网上有许多他们之间的比较文章,大家可以参考后选择适合自己的。本文就简单介绍下Tinker的使用,不涉及原理分析。

第一步:

    在项目build.gradle文件配置tinker-patch-gradle-plugin 依赖:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath ('com.tencent.tinker:tinker-patch-gradle-plugin:1.7.1')
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

然后在app的gradle文件中添加tinker的库依懒:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    //optional, help to generate the final application
    compile('com.tencent.tinker:tinker-android-anno:1.7.1')
    //tinker's main Android lib
    compile('com.tencent.tinker:tinker-android-lib:1.7.1')
    compile "com.android.support:multidex:1.0.1"


}
添加tinker插件:

apply plugin: 'com.tencent.tinker.patch'
在app/builde.gradle文件的dependencies外面添加一些配置 :

tinkerPatch {

    oldApk = "/Users/mac/eel/tinker/app-debug.apk"
    ignoreWarning = true
    useSign = true
    buildConfig {
        applyMapping = null
        applyResourceMapping = null
        tinkerId = "tinkerId"
    }

    dex {
        dexMode = "jar"
        usePreGeneratedPatchDex = false
        pattern = ["classes*.dex",
                   "assets/secondary-dex-?.jar"]
        loader = ["com.tencent.tinker.loader.*",
                  "demo.com.tinkerdemo.SimpleApp",
                  "demo.com.tinkerdemo.BaseBuildInfo"
        ]
    }

    lib {
        pattern = ["lib/armeabi/*.so"]
    }

    res {
        pattern = ["res/*", "assets/*", "resources.arsc", "AndroidManifest.xml"]
        ignoreChange = ["assets/sample_meta.txt"]
        largeModSize = 100
    }

    packageConfig {
        configField("patchMessage", "tinker is sample to use")
        configField("platform", "all")
        configField("patchVersion", "1.0")
    }

    sevenZip {
        zipArtifact = "com.tencent.mm:SevenZip:1.1.10"
    }

oldApk:这里的路径填写你自己存apk的路径,其它都不变。

第二步:

   写一个SimpleAppLike类继承自DefaultApplicationLike,并添加注解。使用注解生成application类*demo.com.tinkerdemo.SimpleApp*,并将其添加到manifest中
package demo.com.tinkerdemo;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Build;
import android.support.multidex.MultiDex;

import com.tencent.tinker.anno.DefaultLifeCycle;
import com.tencent.tinker.lib.tinker.TinkerInstaller;
import com.tencent.tinker.loader.app.DefaultApplicationLike;
import com.tencent.tinker.loader.shareutil.ShareConstants;

/*
 * FileName: SimpleApp
 * Author  : it-009
 * Date    : 16-10-26
 * Desc    :
 */

@DefaultLifeCycle(
        application = "demo.com.tinkerdemo.SimpleApp",
        flags = ShareConstants.TINKER_ENABLE_ALL,
        loadVerifyFlag = false
)
public class SimpleAppLike extends DefaultApplicationLike {

    public SimpleAppLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag,
                         long applicationStartElapsedTime, long applicationStartMillisTime,
                         Intent tinkerResultIntent, Resources[] resources, ClassLoader[] classLoader,
                         AssetManager[] assetManager) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag,
                applicationStartElapsedTime, applicationStartMillisTime,
                tinkerResultIntent, resources, classLoader, assetManager);

    }

    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        MultiDex.install(base);
        TinkerInstaller.install(this);
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
        getApplication().registerActivityLifecycleCallbacks(callback);
    }
}
配置manifest要注意:app的name要和SimpleAppLike类中的注释(application = "demo.com.tinkerdemo.SimpleApp")保持一致:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="demo.com.tinkerdemo"
          xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:name=".SimpleApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

第三步:

如何运行:

1.先将你的app运行到手机上,并复制一份到第一步中的oldApk目录
2.修改app的代码,就比如你修改了bug。
3.在android-studio右边点开gradle窗口,选择tinkerPatchDebug,并运行生成补丁包


4.在app->build->outputs->tinkerPatch目录下选择patch_signed_7zip.apk;并拷到指定手机目录(指定的运行目录,见步骤6
333.png

5.在app中执行更新,退出后再进入app,就能看到修改后的效果。
6.图中path为指定目录,在执行TinkerInstaller.onReceiverUpgradePatch()修复时需要传入的path即为存放生成的补丁包的位置。
 

OK,这样流程这样就完成了。

项目地址:https://github.com/seaeel/TinkerDemo.git

博主技术交流qq群:239025382

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qw65317152/article/details/80351836
文章标签: tinker热修复
个人分类: android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

微信Tinker热修复

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭