Exclusive inventory of Android hot repair solutions (including Alibaba, Meituan, Tencent, etc.)

The last large series of articles is called "Hand-in-hand explanation" , which lasted 10 months and produced more than 20 blog posts, with detailed explanations. Almost every article provides detailed explanations of principles, provides workability githubDemo, and focuses on the key points in the Demo. The place has carried out key dismantling. I believe that every colleague who reads the article in detail will gain something. However, although the explanation is detailed, it lacks in-depth exploration of technology.

Starting from today to open up a new topic:, 移动架构师专业技能深入浅出with the goal of becoming an architect step by step , detail the most direct use value of an architect skill , horizontal peripheral knowledge and in- depth expertise .

The most direct use value: I am most afraid of seeing a kind of article on the Internet. The full text opens up tall and makes people feel unreachable. The whole article does not show how the technology is implemented and what effect it will be after. When an article is written, it is necessary to bring readers into the author's world in the most acceptable way , rather than pretending to be aloof and overlooking sentient beings. Therefore, the beginning of the article must be the most direct demonstration of the landing effect of the technology. Providing a runnable Demo allows readers to try it themselves.

Horizontal peripheral knowledge: A core technology must not exist independently. Technology is a system. However, the technology that can be detailed in an article is limited. It must be centered on one technology and assisted by other technologies. The core technology needs to be detailed, but the surrounding technology also needs to be explained. The towering trees will not need soil as a dependency. Use concise language to explain the surrounding knowledge and provide the correct research direction for this knowledge. It is also a step that a responsible blogger cannot ignore.

In-depth professional technology: The most taboo thing to do with technology is to simply taste it. A little deeper and then exit, it is not conducive to understanding the underlying implementation, in the long run, it will always be a technical novice, unable to become a master; secondly, it is not conducive to long-term memory, no matter how strong the memory is, the technical details will inevitably be blurred. But if you go deep into the core and understand the principles, you will never deviate in the general direction of the technology. As a man who wants to become an architect, even if he can't remember so many details, he can never be wrong about the general direction. Therefore, technical depth is necessary.

Recap

In the previous article  , the principle of the Android Muitldex hot update repair program mentioned one of the Muitidex hot repair solutions, and gave a demo. But it is far from enough to show the real open source framework of hot fixes completely.

Body outline

  • Ali AndFix solution

  • Meituan Robust Plan

  • Tencent Tinker dexdiff / DexMerge solution

  • Tencent QZone Muitidex solution

text

Hot repair solutions are divided into two categories according to whether restarting is necessary:  restart effective/immediate effective . According to the way of implementation, it can be divided into three categories:  implementation of java layer / implementation of native layer / mixed implementation of java native

Ali AndFix solution (deprecated)

AndFix is   an implementation  of the  native layer without restarting . However, AndFix has not been maintained and updated for more than 3 years. Because Ali already has a new alternative, no maintenance is required. In addition, the compatibility of this purely native implementation is very worrying. Since it has been deprecated, it is okay to understand the implementation a little bit.

Interpretation of the above figure : There is a program in A=>B=>Cthe figure to be executed in  the order, but when it is found that there is a bug when it is executed to B, AndFix's repair method is: when the native layer executes to the B method, the method pointer points to the  patch The B method in the package bypasses the original B method . So as to achieve the purpose of fixing bugs.

How to use :

Add the @MethodReplace annotation to the method that needs to be repaired. Given parameters, class and method, indicate which method of which class the method is replaced with.

Defect : Repair granularity: method. AndFix can only fix bugs with method granularity. The effect is limited, and there is nothing you can do for a wide range of class replacement and resource replacement.

Meituan Robust Plan

Robust is  a hot fix implementation solution implemented by the Java layer that takes effect immediately .

Meituan’s Robust solution was designed with reference to Google’s InstantRun solution (previously there was this option in androidStudio, you can choose to open instant run to run the app).

The main design idea is one sentence: when compiling and packaging, in some methods of the program, insert a piece of code (automatic operation):

When it is  changeQuickRedirectnot empty, the method will hit if(changeQuickRedirect!=null)and the repaired implementation code will be  executed. When it is empty, the original logic is executed normally.

Usually we code ourselves, and we only need to add an  @modifyannotation to mark that this method needs to be patched, that is, we need to insert the above  if(changeQuickRedirect!=null)code segment. 

Why can it take effect immediately?

Analysis of the above figure: Using ClassLoader, when the client mobile phone receives the patch.dex, it executes the patch and assigns the specified method  changeQuickRedirectby reflection to make it non-empty. So that the next time the program logic is here, the logic after the repair will be followed.

How does Robust insert so many **** if(changeQuickRedirect!=null)code segments into the code logic? :

The if(changeQuickRedirect!=null)code segment in the picture above  is not written manually, but automatically inserted by the Robust framework. This technique is called  bytecode instrumentation , which means: operate on the class and insert the logic you want according to the format of the class file. Currently Robust supports two bytecode instrumentation schemes  AspectJ and  Javasist.

Tencent Tinker dexdiff / DexMerge solution

Tinker Tencent research since the  restart to take effect the  implementation of the Java layer .

principle

Tinker uses the dexdiff algorithm to calculate the difference package dex based on a benchmark dex and the dex after the bug is fixed. Then push the differential package to the client. After receiving it, the client restarts the app to merge the differential package dex with the original dex to form a new dex. Then when ClassLoader creates the class object, it will create the class object after the bug is fixed. So as to achieve the purpose of fixing bugs. 

Dexdiff algorithm

Those who know incremental updates should know bsdiff. Bsdiff ignores the file format and generates the difference file of two binary files. dexdiff is based on bsdiff and optimized for the dex file format. In addition to supporting Dex repair, Tinker also supports so repair, but the difference package of so is generated by the directly used bsdiff algorithm.

Tencent QZone Muitidex solution

The Multidex solution is a hot repair solution based on the pure java implementation  of  ClassLoader that  restarts to take effect .

principle

When the Apk is packaged, multiple classes.dex files may be generated. The class loader ClassLoader in the JVM, when the program runs and uses a certain Class, is performed in a sequential search mode. Once found, it will be cached , and the next loadClass will not be searched, but directly used in the cache (so the Muitldex solution must restart the app). And, when we put the patch dex file at the top of the sequential search, then Once the class loader finds it, it will be used directly. The original same class is at the rear and will no longer take effect. As a result, the original class is completely replaced with the patch Class.

As for the more in-depth principle, the principle of the Android Muitldex hot update repair solution has been explained in detail in the previous article  . I won't go into details here.

Scheme comparison

Digression

With the continuous development of Internet companies, there are more and more modules in product projects, and user experience requirements are getting higher and higher. It is becoming more and more difficult to achieve the purpose of fast running in small steps and rapid iteration. There is also 65535. The application of plug-in technology is born for problems such as the mutual calling of each other. If there is no plug-in technology, the applications that integrate a large number of "app" such as Meituan and Taobao may be as big as a few g.

Therefore, today's Android mobile development will not be hot-fixed, plug-in, and componentized, and more than 80% of the interviews will not pass.

I have collected from the Internet that the Ali P8 boss spent nearly half a month on the Android hot repair framework, plug-in framework, component framework, image loading framework, network access framework, RxJava responsive programming framework, IOC dependency injection framework, and recently The architecture component Jetpack and other Android third-party open source frameworks are integrated into a set of system knowledge notes PDF, up to 1042 pages! I believe that after reading this document, you will have a deeper and more systematic understanding of these Android third-party frameworks.

Due to the excessive content of the document, in order to avoid affecting everyone's reading experience, only part of the content is shown in screenshots. The detailed and complete version of the 1042 [Android Design Idea Interpretation Open Source Framework] document collection method: like + follow, and then privately message keywords [1] You can get a free way to receive it!

Guess you like

Origin blog.csdn.net/weixin_43901866/article/details/110366150