In recent years, there has been more and more discussion and sharing of hot repair technology in the field of Android development. At the same time, some different solutions have emerged, such as Qzone patch solution, Ali AndFix and wechat Tinker. They have different principles and different application scenarios. Through the introduction of QQ space patch, Tinker and AndFix based alibaichuan HotFix technology principle analysis and horizontal comparison

First, the development history of thermal repair technology

1. Origin of thermal repair technology

From the traditional development process, there are many disadvantages:

  • The cost of reissuing the version is too high
  • The cost of download and installation is too high
  • Bug fixes are not timely and user experience is poor

2. Hot repair development process

Online version — user install — Bug found — emergency fix — patch printed, push to users — automatic pull patch fix

The development process of hot repair is more flexible and has many advantages:

  • No need to reissue version, real-time and efficient heat repair
  • No user perception repair, no need to download new applications, low cost
  • High repair success rate, minimize the loss

Two, three mainstream thermal repair technology

1. Qzone super patch technology

Super patch technology is based on DEX subcontracting scheme and uses the principle of multi-dex loading. The general process is as follows:

  • After fixing the BUG method, place it in a separate DEX, insert it at the top of the dexElements array, and let the virtual machine load the fixed method. When patch.dex contains test. class, it will be loaded preferentially. If the test. class is encountered in the subsequent dex, it will be directly returned without loading, thus achieving the purpose of repair.
  • One problem, however, is that an exception is generated when two classes calling the relationship are not in the same DEX. As we know, when APK is installed, the virtual machine needs to optimize classes.dex into an odex file before it executes. During this process, the verify operation of the class is performed. If all the classes calling the relationship are in the same DEX, the CLASS_ISPREVERIFIED flag is marked and then the Odex file is written.
  • Therefore, in order to make patch repair normal, it is necessary to avoid the class being marked with CLASS_ISPREVERIFIED. Specifically, a class is separately placed in another DEX for other classes to call.

The main steps of repair:

  • It can be seen by getting the Classloader of the current application
  • Convert patch.dex to Element by calling dexElements of pathList by reflection []
  • The two elements [] are merged, putting patch.dex first
  • Load Element[] for repair purposes


Advantage:

  • There is no integration package (this is compared to the following wechat Tinker), the product table is smaller and more flexible
  • Can achieve class replacement, high compatibility. (Certain Samsung phones don’t work)

Inadequate:

  • It does not take effect in a timely manner and must be restarted to take effect.
  • Is serious, the startup time will be increased, resulting in a significantly higher probability of ANR

2. Wechat Tinker

  • Wechat proposed a DEX differential package to replace DEX as a whole in view of the deficiency of qzone super patch technology. The main principle is basically the same as qzone super patch technology, except that patch.dex is no longer added to elements array, patch.dex is given in the way of difference, and then patch.dex is merged with classes.dex, and the old dex is replaced as a whole. To achieve the purpose of restoration.


Advantage:

  • Synthesize the entire package without inserting code in the constructor, preventing verify, verify, and opt from being completed at compile time and not at run time
  • Improved performance. High compatibility and stability
  • Developer transparency, no additional processing of packages is required

Disadvantage:

  • Like the super patch technology, it does not take effect immediately and must be restarted for efficiency
  • You need to start a new process for the application to merge, and it is easy to fail due to memory consumption and other reasons.
  • The merge takes up extra disk space. For an application with multiple DEX, if multiple DEX files are modified, multiple patch. DEX and corresponding classes. DEX need to be delivered to merge the patch. DEX and corresponding classes. DEX, which is more serious, and the merge process has a higher failure rate

3. Alibaba HotFix

  • Compared with qzone super patch technology and wechat Tinker, the HotFix service launched by Alibaichuan is positioned at the scene of urgent bug repair and can fix bugs in the most timely way. The pull-down patch takes effect immediately without waiting. Principle of implementation:
  • Different from qzone super patch technology and wechat Tinker, which adds or replaces the whole DEX, AndFix provides a way to modify the Filed pointer in Native during the runtime to realize the change of the method, so as to achieve immediate effect without restart and no performance consumption for the application

Implementation process steps:

  • Open the link library operation handle, get the native layer internal function, and get the ClassObject object
  • Change the access attribute to public
  • Get a pointer to the old and new methods, and the new method points to the target method to achieve method replacement



Advantage:

  • Timeliness of bug fixes
  • The patch package also adopts the differential technology to generate the smallest patch volume
  • No intrusion to applications, almost no performance loss

Inadequate:

  • New fields are not supported, the

    method is modified, and resource replacement is not supported.
  • Due to the manufacturer’s custom ROM, a few models are not supported.

Iii. Summary:

  • Qzone super patch technology and wechat Tinker support the replacement of new classes and resources, which is more powerful in some functional updates, but will have a certain impact on the performance and stability of the application; Although Alibaichuan HotFix does not support the replacement of new classes and resources for the time being, and has some restrictions on the release of new functions, as a HotFix service positioned as an online emergency BUG, it can truly fix the BUG instantly without users’ awareness, and at the same time ensure that there is no unnecessary loss to the application performance. It is a good choice in thermal repair