Since 2015, all kinds of thermal updates and restorations have mushroomed and blossomed until ali’s Sophia X. For the previous variety is a summary of it; I’ve been studying the principles of hotfix for a while now, and I’m afraid I’ll forget, so I’ll write a summary.

 

First, the evolution history of hot repair technology, from PC to mobile Internet, from native development to hybrid development, all the technologies evolved in the PC era, exactly copied to the mobile Internet; Hot repair of the evolution process: hand tao from the AndroidDalvik virtual machine operation of Java Method hook technology -Dexposed(due to the Davilk virtual machine structure of the underlying Davilk scheme is too dependent on the ART virtual machine compatible with Android 5.0); Then go to Alipay Andfix(also a replacement of the underlying structure, compatible with ART); Then Alibaichuan launched Hotfix(it only provides fixes at the code level, and does not support fixes for resources and SO). And finally Sophix; Tencent’s qzone super patch, wechat’s Tinker, Ele. me’s Amigo, Meituan’s Robust, etc., have their own limitations, or are unstable, or have too large patches, or are inefficient, or are too cumbersome to use, so the experience is not very good. The SophiX is the industry leader in the three major areas of Android hot fixes: code fixes, resource fixes, and SO fixes.

The only thing that SophiX doesn’t support is the fix for the four major components, because to fix the four major components, you have to pre-insert proxy components into the AndroidManifest and declare all permissions as much as possible, which leads to the original APP adding a lot of bloatcode that is very intrusive to the running process.

Sophix design philosophy: non-invasive, no need to invade the BUILD process of APK, just produce old and new APK; All that is required is two lines of code to initialize and request a patch.

 

1. Code repair:

Code repair two main schemes: Ali system replacement scheme, Tencent system class loading scheme

Advantages and disadvantages:

The underlying replacement has a lot of limitations, but is the most time-sensitive, fast to load, and immediately effective

The class loading scheme has poor timeliness and requires cold restart to take effect, but it has a wide repair range and few restrictions.

The underlying replacement scheme: directly replace the original method in the loaded class, is modified on the basis of the original class; Because it is impossible to add or subtract methods and fields from the original class; Because this will destroy the original class structure (once the increase or decrease of methods in the patched class, it will lead to the change of method number in this class and the whole Dex, and the change of method number will lead to the change of method index, so that the correct method cannot be indexed normally when accessing methods; The addition and subtraction of fields also cause index changes.

Like Dexposed, Andfix or other Hook schemes, are dependent on modifying the virtual machine method entity of the specific field, such as change Dalvik method jNI function pointer, change class or method access rights, because the manufacturers can transform the source code, replacement mechanism is likely to go wrong. Sophix takes the substitution approach regardless of the underlying structure, as long as the ArtMethod array remains linear.

Class loading solution: The principle is to let the Classloader load the new class after the app restarts

Sophix uses the full dex technology to quickly synthesize a new full dex by using the original Android class search and composition mechanism. The order of dex in the package is rearranged. When the virtual machine searches for classes, the classes in class. dex will be found first, then classes2.dex and classes3.dex. It does this by breaking and reorganizing the order of classes.dex in the old package and the fix package.

 

The two methods are combined and selected according to code changes. For minor modifications, the low-level replacement fixes are directly used within the limits of low-level replacement solutions, and the rest are replaced by class loading.