Hot patching, which began in 2015 after the introduction of plug-ins, has become a very popular Android development technology. One of the more famous taobao’s Dexposed, Alipay AndFix and QZone’s super hot patch program. Wechat’s research on hot patch technology is not early, starting around June 2015. After studying and trying various existing schemes, we found that they all have their own limitations. Wechat finally adopted a different technical solution and walked out of its own way of practice evolution.

Technology, on the other hand, should be just one piece of a hot patch solution. With many attempts and applications of hot patches, wechat has established its own process specifications and constantly tried to expand its application scenarios. Through this article, I hope that you will not only have a comprehensive understanding of the advantages and disadvantages of each hot patch technology, but also have a more comprehensive understanding of its application scenarios. Based on this, it may be easier for you to decide whether and how to use hot patching in your own projects.

Why do we need hot patches

Hot patch: Enables applications to be updated without reinstallation, helping applications quickly build dynamic repair capabilities.

From the above definition, hot patches save time for a large Android market release. At the same time, users do not need to reinstall, as long as the online can be unaware of the update. It looks great, but does that mean we can try to use patches instead of releases? In fact, hot patch technology still has its limitations, which are mainly reflected in the following points:

  1. The patch can be used only for a single client version. The patch size increases with the version difference.

  2. The patch does not support all changes, such as AndroidManifest;

  3. Patches are not 100% successful in updating either code or resources.

Since patching can’t completely replace upgrading, where does it fit in?

2. Remote debugging

Another pain in Android development is fragmentation. We may all encounter the “local do not reappear”, “log can not check”, “contact users do not bird you” trouble. So the patching mechanism is ideal for remote debugging. We needed the ability to send patches only to specific users, which was very helpful in finding problems.

Review images

Using patch technology, we avoid harassing users and quietly solve problems for users. Of course, this also requires very strict permission management, to prevent malicious or arbitrary use.

Other four.

In fact, Android officially uses hot patch technology to implement Instant Run. It is divided into Hot Swap, Warm Swap and Cold Swap three ways, you can refer to the English introduction, you can also see the translation of the reference article. The latest Instant App should adopt a similar principle, but Google Play is not allowed to send code, this overseas App needs to pay attention to.

A AndFix.

AndFix uses native hook. This solution directly replaces the implementation of the method in class with dalvik_replaceMethod. Since it does not replace the class as a whole, and the relative address of field in the class is determined when the class is loaded, AndFix cannot support adding or deleting filed cases (replacing init and Clinit can only change the field value).

Review images

As such, Andfix can support a relatively limited number of patch scenarios, and can only be used to fix specific problems. Given the previous release process, we prefer that the patch be insensitive to the developer, i.e. he doesn’t need to know whether the change is for a patch or a release (in fact, we also use Git branch management +cherry-pick). On the other hand, there are complex compatibility issues with native replacements.

Review images

The biggest advantage of AndFix over other solutions is that it works immediately. In fact, The implementation of AndFix is somewhat similar to the hot plug of Instant Run, but due to the limitations of usage scenarios, wechat has ruled out the use of this solution at the very beginning.

Iii. Wechat hot patch scheme

Is there a solution that makes development transparent but doesn’t have the drawbacks of the QZone solution? Instant Run’s Cold plug and Buck’s Exopackage might give us some inspiration, both of which are full Dex replacements. That is, we completely used the new Dex, so that there was no problem of Art address confusion, and there was no need to pile in Dalvik. Of course, given the size of the patch pack, we couldn’t just put the new Dex in it. However, we can put the differences between the old and new Dex into the patch package, and at the simplest, we can adopt the BsDiff algorithm.

Review images

In simple terms, a difference between the old and new Dex is generated at compile time, path.dex. At run time, restore the difference patch.dex with the old dex of the original installation package to the new dex. This process may be time consuming and memory consuming, so we put it in a separate background process: Patch. In order to keep the patch package as small as possible, wechat developed the DexDiff algorithm, which makes deep use of the format of Dex to reduce the size of the difference. Its granularity is each item in Dex format, which can make full use of the original Dex information, while the granularity of BsDiff is a file, AndFix/QZone is class.

Review images

I hope this part will be covered in a separate article. Here is a preparation. The general effect is shown in the picture below. In the most extreme case, our patch size was only 6.6m because we completely replaced a 13M DEX with the information of the original DEX.

Review images

But the scheme is not without its drawbacks, and it raises two problems:

  1. Occupying Rom volume; This side is about 1.5 times the size of the Dex you modified (dexopt and Dex compressed into JAR).

  2. An additional synthesis process; Although we put it in a separate process, the length of synthesis time and memory consumption also affect the final success rate.

Wechat’s hot patch solution is called Tinker, a nod to the goblin Tinker in Dota, which hopes to be unlimited.

Review images

The more technical details of Dex, Library, and resources are not covered here due to space limitations, but will be covered in a separate article later. Let’s finally compare these schemes as a whole:

Review images

The QZone scheme is the simplest and most successful without taking care of performance loss and patch pack size (there is no separate composition process). It also has a smaller Rom footprint than Tinker. On the other hand, the difference between QZone’s success rate and Tinker’s is about 3%.

In fact, a complete framework should also be one that is easy to use. Tinker has good support for patch version management, process management, security verification and so on. We also support gradle and named lines. Hopefully, it will be available soon.

I. Status quo of network channels

The network channel is responsible for delivering the fix pack to the user, both for specific users and for full users. In fact, wechat currently has the following three channel updates for hot patches:

  • Pull the channel; At the time of login /24 hours, we use pull to check whether there is a corresponding patch package update in the background, which is also the most common way.

  • Specify the push channel for the version; For version-specific channels, in case of emergency, we can deliver patch pack updates to all users within an hour.

  • Specify a push channel for a specific user; Remote debugging for specific users or groups of users.

In fact, for most applications, CDN+pull channels are relatively easy to implement, assuming that push channels are not implemented.

In fact, wechat hot patch release is very cautious. The entire release process is consistent with the upgraded version, and the version number must be modified, and the complete testing process must be rigorous, etc. We will also go online by grayscale, and monitor the patch version of each indicator. Here’s what we did to fully monitor the patch situation:

  • 1 minute granularity of accumulative users of each version per hour/day, timely monitor the number of patch version and active;

  • The Crash statistics of 3-minute granularity are compared in the two dimensions of hourly/daily Crash of benchmark version and patch version.

  • Patch monitoring information is reported within 10 minutes.

The future work

With the evolution of wechat department from “single APP” to “multiple apps”, wechat is also stepping into the development practice of open source. We want to componentize each function so that it can be copied and applied quickly. “Tinker”, the hot patch framework of wechat, is also undergoing a process of separation from wechat and integration into wechat. Hopefully, in the near future, we can also open source “Tinker” and some other components in wechat.

We also hope to find an App for internal testing to provide us with valuable opinions. Users who are interested in wechat’s Tinker solution can send separate messages or leave comments at the end of articles. Please indicate your name, company and responsible App. We hope to select some products for internal test.

If you think our content is good, please scan the QR code to reward the author and send to your moments, and share with your friends

Review images


This article is the exclusive content of Tencent Bugly, please reprint at the beginning of the articleNote the author and source “Tencent Bugly(http://bugly.qq.com)”

Review images