Android Hot Fix principle

A. AndFix

Replace principle is the method of AndFix, to replace the method as a method of bug patch file.

Note: In the alternative embodiment method bug Native layer pointer Alternatively, has the purpose to fix the bug.

AndFix using native hook manner, this embodiment directly dalvik_replaceMethod alternative implementation of class methods. Because it does not replace the whole class, and class field in the relative address is determined when the class is loaded, so AndFix can not add or delete support cases filed in (the value field can be modified only by replacing init and clinit). Andfix can support patch scene is relatively limited, only can use it to fix specific problems.

Two. QZone (instrumentation mode)

The program is based on the android dex subcontracting program, simply summarize, is to stuffing more dex files into the app classloader, but android dex unpacking program in class is not repeated, if classes.dex and classes1.dex have duplicate class, when the repeated use of the class, the system will choose which class to load it? Let's take a look at the code class loading:

Dex a ClassLoader can contain multiple files, each file is a dex Element, more dex files are arranged in an ordered array dexElements, when looking for class, will traverse in order dex file and dex files from the current traversal find the class, look for the class if it returns, if not found continue to find from a dex file.
Theoretically, if the presence of the same class in different dex, then select the priority class of the top surface of the dex file, as shown below:

On this basis, we conceived a hot patch program, there is the question of class packed into a dex (patch.dex) to go, and then inserted into the dex Elements of the front, as shown below:

III. Tinker micro-channel (differential package)

Cold swap and buck the exopackage Instant Run perhaps give us inspiration, their thoughts are replaced with new Dex full amount.

We can put the difference between the old and new Dex patch package, we can use BsDiff simplest algorithm.

In simple terms, the difference is generated at compile time path.dex by both old and new Dex. At run time, the difference with the original patch.dex again to restore the old Dex installation package for the new Dex. This process can be time-consuming and memory, so we are placed in a separate background process: patch in. To patches as small as possible, since the inquiry of the micro-channel DexDiff algorithms that use depth Dex format to reduce the magnitude of the difference.

Fourth, Ali Sophix

Principle (two-pronged):

1. Optimize Andfix (breakthrough underlying structural differences, solve the stability problem):

Andfix ArtMethod underlying structure using the internal variable eleven replaced, but added that all manufacturers will be modified, so compatibility is not good.

Sophix change his thoughts, the overall structure of alternative methods, ignoring the underlying implementation, in order to solve the problem of stability compatible.

2. The breakthrough of defects QQ and Tinker

QQ and Tinker defects

Sophix solutions of dex

  • Ali from research using the full amount under the Dalvik dex program: dex not consider all the patches into the front of dex (dex instrumentation), but think of ways to delete dex principle of (just delete the definition of the class) patch in dex the existence of class, so the system can not find the time to find the class in the original dex, then only the patch of dex loaded into the system, the system will naturally find the corresponding class from the patch package.

  • Under Art is essentially a virtual machine and supports multi-dex loading, Sophix is ​​done simply to patch as the main dex dex (classes.dex) only, equivalent to reorganize all the dex files: the dex patches renamed classes. dex, all previous dex apk sequentially changed classes2.dex, classes3.dex ... classesx.dex, as shown below.

3. Resource repair another way

Common program (Instant Run Technology): The problem with this approach is that compatible replacement AssetManager place of

Sophix resource restoration program

4.SO repair another way

For more information + interview Android development framework to share information click on the link to receive free

"Android Architect necessary to receive a free learning resources (architecture + video + interview thematic document study notes)"

Guess you like

Origin blog.csdn.net/Coo123_/article/details/93063636