11

Android热修复框架Tinker初体验

 4 years ago
source link: http://www.androidchina.net/7729.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Android热修复框架Tinker初体验 – Android开发中文站
你的位置:Android开发中文站 > Android开发 > 新手入门 > Android热修复框架Tinker初体验

1.Tinker简介

Tinker是微信官方的Android热补丁解决方案,它支持动态下发代码、So库以及资源,让应用能够在不需要重新安装的情况下实现更新。当然也可以使用Tinker来更新你的插件。

详细说明:Tinker介绍

2.Tinker快速集成

1.在项目的build.gradle中,添加tinker-patch-gradle-plugin的依赖

Center

2.然后在app的gradle文件app/build.gradle,我们需要添加tinker sdk的库依赖

Center

注意在app/build.gradle中 要在 apply plugin:‘com.android.application’,后面加上使用  apply from:‘tinkerpatch.gradle’

tinkerpatch.gradle文件放在和app/build.gradle 相同的目录下

tinkerpatch.gradle详细如下:

apply plugin: 'tinkerpatch-support'  

/**  
 * TODO: 请按自己的需求修改为适应自己工程的参数  
 */  
def bakPath = file("${buildDir}/bakApk/")  
def baseInfo = "app-1.0-1023-17-32-02"  
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  

    autoBackupApkPath = "${bakPath}"  

    appKey = "04e5313d45561aa6"  

    /** 注意: 若发布新的全量包, appVersion一定要更新 **/  
    appVersion = "1.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  
    }  
}

3.添加集成Tinker的代码

public class SampleApplication extends Application {  
    private static final String TAG = "SampleApplication";  
    private ApplicationLike tinkerApplicationLike;  

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

        // 初始化TinkerPatch SDK, 更多配置可参照API章节中的,初始化SDK  
        TinkerPatch.init(tinkerApplicationLike)  
                .reflectPatchLibrary()  
                .setPatchRollbackOnScreenOff(true)  
                .setPatchRestartOnSrceenOff(true)  
                .setFetchPatchIntervalByHours(3);  
        // 获取当前的补丁版本  
        Log.d(TAG, "Current patch version is " + TinkerPatch.with().getPatchVersion());  
        // 每隔3个小时(通过setFetchPatchIntervalByHours设置)去访问后台时候有更新,通过handler实现轮训的效果  
        TinkerPatch.with().fetchPatchUpdateAndPollWithInterval();  
    }  
}

这一部分详细可以查看官网文档:http://www.tinkerpatch.com/Docs/SDK
本例子中MainActivty中主要内容如下:

textView的点击事件是注释的,要加入检测热修复的一句代码

TinkerPatch.with().fetchPatchUpdate(true);

运行项目在app/build/bakApk中生成文件如下:

将文件夹名称 app-1.0-1023-17-32-02 替换tinkerpatch.gradle中def baseInfo = “app-1.0-1023-17-32-02″

def baseInfo = “app-1.0-1023-17-32-02″

将MainActivity中textView的点击事件打开,然后打开Android Studio 右侧 Gradle 找到Tasks打开tinkerGradle

运行 tinkerPatchRelease或者tinkerPatchDebug(根据自己app/gradle中设置选择) task 构建补丁包,补丁包将位于 build/outputs/tinkerPatch下。

下一步就是 将生成的补丁包(patch_signed_7zip.apk)上传Tinker后台管理http://www.tinkerpatch.com/Index/login,提前注册好登录账号,注意在tinkerpatch.gradle中需要用到注册好的项目appKey,然后可以全量下发,也可以按照官网说明安装 tinkerpatch-debug-tool进行开发预览,它的 Github 地址为 tinkerpatch-debug-tool,也可以通过点击此链接下载

注意的地方有几个:

1、应用程序版本号versionName 填入Tinker管理后台作为Tinker后台项目版本号,两者保持一致

2、手机上安装的应该是注释textView点击事件的app,然后根据热修复技术,重新打开该app后textView点击事件有效


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK