Tinker hot repair use summary

Tinker usage record and summary

1. Usage scenario (why use hot fix?)

In the development process, we often encounter some small online bugs that must be resolved in time. The common method is to update a version and push it to the user to force the update.

Example: After the company's project version 3.0.0 was launched, we encountered a bug that users could not use data to download. We could only release version 3.0.1 to fix this bug.

If hot fix technology is used, we can solve this problem without upgrading the version.

 

2. Why use Tinker?

 

Tinker

QZone

AndFix

Robust

Class replacement

yes

yes

no

no

So replace

yes

no

no

no

Resource replacement

yes

yes

no

no

Full platform support

yes

yes

no

yes

Immediate effect

no

no

yes

yes

Performance loss

Smaller

Larger

Smaller

Smaller

Patch package size

Smaller

Larger

general

general

Development transparency

yes

yes

no

no

the complexity

Lower

Lower

complex

complex

Rom volume

Dalvik is larger

Smaller

Smaller

Smaller

Success rate

Higher

Higher

general

highest

Strong R&D strength; Tinker has been verified by hundreds of millions of WeChat users, and its stability and performance are trustworthy.

There are many third-party support platforms: TinkerPatchBugly , Tinker-Manager , tinker-dex-dump, etc. to facilitate the use and debugging of Tinker.

Take TinkerPatch as an example:

Attach the link: http://www.tinkerpatch.com/

 

Apply for appKey in the background of TinkerPatch (the application steps are skipped):

 

3. Use of TinkerPatch

1) Add Gradle plugin dependency (build.gradle under project)

dependencies { classpath ("com.tinkerpatch.sdk:tinkerpatch-gradle-plugin:${TINKERPATCH_VERSION}") { changing = true } }

2) Add denpendencies dependency of TinkerPatch SDK library (build.gradle under app)

annotationProcessor("com.tinkerpatch.tinker:tinker-android-anno:${TINKER_VERSION}") { changing = true }
compileOnly("com.tinkerpatch.tinker:tinker-android-anno:${TINKER_VERSION}") { changing = true } 
implementation("com.tinkerpatch.sdk:tinkerpatch-android-sdk:${TINKERPATCH_VERSION}") { changing = true }

For simplicity and convenience, we put the TinkerPatch related configuration in  tinkerpatch.gradle  , we need to import it

apply from: 'tinkerpatch.gradle'

 

3) Add version information in gradle.properties for easy version update

TINKERPATCH_VERSION=1.2.2 
TINKER_VERSION=1.9.2

4) Configure tinkerpatchSupport parameters and open the imported  tinkerpatch.gradle , as shown in the figure:

apply plugin: 'tinkerpatch-support'

def bakPath = file("${buildDir}/bakApk/")
def baseInfo = "app-3.1.0-1203-16-00-22"
def variantName = "primary-release"

/**
 * 对于插件各参数的详细解析请参考
 * http://tinkerpatch.com/Docs/SDK
 */
tinkerpatchSupport {
    /** 可以在debug的时候关闭 tinkerPatch **/
    tinkerEnable = true
    reflectApplication = true

    autoBackupApkPath = "${bakPath}"

    /** 是否开启加固模式,只有在使用加固时才能开启此开关 **/
    protectedApp = true

    appKey = "这里替换成TinkerPatch后台的Appkey"
    appVersion = "3.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"

//    baseApkFile = "${bakPath}/app-1.0.0-0118-15-36-58/debug/app-debug.apk"
//    baseProguardMappingFile = "${bakPath}/app-1.0.0-0118-15-36-58/debug/app-debug-mapping.txt"
//    baseResourceRFile = "${bakPath}/app-1.0.0-0118-15-36-58/debug/app-debug-R.txt"

    /** 若有编译多flavors需求,可在flavors中覆盖以下参数
     *  你也可以直接通过tinkerPatchAllFlavorDebug/tinkerPatchAllFlavorRelease, 一次编译所有的flavor补丁包
     *  注意的是:除非你不同的flavor代码是不一样的,不然建议采用zip comment或者文件方式生成渠道信息
     **/
//    productFlavors {
//        flavor {
//            flavorName = "flavor1"
//            appVersion = "${tinkerpatchSupport.appVersion}_${flavorName}"
//
//            pathPrefix = "${bakPath}/${baseInfo}/${flavorName}${variantName}/"
//            name = "${project.name}-${flavorName}${variantName}"
//
//            baseApkFile = "${pathPrefix}/${name}.apk"
//            baseProguardMappingFile = "${pathPrefix}/${name}-mapping.txt"
//            baseResourceRFile = "${pathPrefix}/${name}-R.txt"
//        }
//
//        flavor {
//            flavorName = "flavor2"
//            appVersion = "${tinkerpatchSupport.appVersion}_${flavorName}"
//
//            pathPrefix = "${bakPath}/${baseInfo}/${flavorName}${variantName}/"
//            name = "${project.name}-${flavorName}${variantName}"
//
//            baseApkFile = "${pathPrefix}/${name}.apk"
//            baseProguardMappingFile = "${pathPrefix}/${name}-mapping.txt"
//            baseResourceRFile = "${pathPrefix}/${name}-R.txt"
//        }
//    }
}

/**
 * 用于用户在代码中判断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) Initialize TinkerPatch SDK.

 public class MyApplication extends MultiDexApplication{
   
   ...
     
    private ApplicationLike tinkerApplicationLike;
    @Override

    public void onCreate() {
        super.onCreate();
        if (BuildConfig.TINKER_ENABLE) {
            // 我们可以从这里获得Tinker加载过程的信息
            tinkerApplicationLike = TinkerPatchApplicationLike.getTinkerPatchApplicationLike();
            // 初始化TinkerPatch SDK, 更多配置可参照API章节中的,初始化SDK
            TinkerPatch.init(tinkerApplicationLike)
                    .reflectPatchLibrary()
                    .setPatchRollbackOnScreenOff(true)
                    .setPatchRestartOnSrceenOff(true)
                    .setFetchPatchIntervalByHours(1);
            Log.e("TinkerPatch", "Current patch version is " + TinkerPatch.with().getPatchVersion());
            // 每隔1个小时(通过setFetchPatchIntervalByHours设置)去访问后台时候有更新,通过handler实现轮训的效果
            TinkerPatch.with().fetchPatchUpdateAndPollWithInterval();
            ...
        }
        
        ...
        
    }

Note: The initialization code is recommended to follow super.onCreate(), and all processes need to be initialized, so that all processes can be patched

 

6) Generate base class package

After running, find the generation path of the base class package.

Modify the three file paths in tinkerpatch.gradle .

def bakPath = file("${buildDir}/bakApk/")
def baseInfo = "app-3.1.0-1203-16-00-22"
def variantName = "primary-release"

 

7) Generate a patch package. Add any Toast on the basis of the original code to test whether the patch is loaded successfully (the Toast is not included in the original package).

showToast("Upgrade ok!!!",false);

 

After running, find the location of the patch package as follows:

 

8) Publish the patch and enter the TinkerPatch background to configure:

Here we choose the development preview. The development preview is used for development testing and will not affect the online version.

 

9) Install the base package generated in step 6 first. At this time, there is no Toast code. Developers need to install the test tool on the phone.

Attach the link: http://ogmj51oem.bkt.clouddn.com/TinkerDebugTool.apk

Turn on,

Reopen our application again, you can find from the background that the patch has been downloaded, you need to reopen the application again to complete the merge operation, in the background we can see the number of patch downloads/synthesis number/application number, the patch just released Only 16.6k:

After the test is no problem, you can choose to release the full amount, you can unknowingly fix the online bug, and you can monitor the patch synthesis in real time in the background:

 

 

to sum up

Up to this point, with the help of the TinkerPatch third-party platform, I have initially mastered the use of hot fixes. The complicated principle knowledge still needs to be learned. When accessing the SDK with their official documentation, I encountered many pits, so I reorganized my access And the steps and methods when using Tinker, interested students can learn from it.

 

Guess you like

Origin blog.csdn.net/Json_Jerry/article/details/84792116