A Simple Example of Hot Fix Like Wechat

Introduction

Not long ago read a blog, about the micro-hot patch, and instantly been attracted, so decided to take the time to study, a very good article, recommend you look at the link will be attached at the end of this article.

principle

Let’s start by assuming that old.apk is the older version of apk (the version with the bug) and new.apk is the new one (bugfix version); assuming that they are both bug-related and code-independent Resource level replacement).

We get two apk classes.dex documents were named old.dex and new.dex, the ultimate goal is to replace the old.dex old.dex want to run the program in old.apk. To this end we have done the following steps:

By using the bsdiff tool, get the old.dex difference with new.dex classes.patch (done on the computer side).

In the mobile terminal to get to install the old.apk, extract access to classes.dex, that old.dex.

In the phone side through the bspatch tool (internal. So) old.dex and classes.patch combined into new.dex.

In the application’s attachBaseContext, construct a DexclassLoader, insert new.dex into the BaseClassLoader’s dexElements before the new.dex load in advance to achieve the effect of hot patch.

The problems encountered in the process

In the program-side access apk path getApplicationInfo (). SourceDir, originally thought I have apk path since the read and write permissions, I simply directly from this.zip file to read classes.dex file can be, there is no need to Copy out. But in the process of achieving the Debug found that, in fact, they did not read this. Zip file permissions to the contents of subdirectories (why?) Under investigation slowly).

In the translation dspatch.so encountered “dlopen failed can not locate symbol” signal “”, This really good pit, said the reasons for it, is seen in the stackoverflow, android-21 before, signal.so is a Inline functionality. But now is no longer. How to solve it?

  • I. Use app_PLATFORM: = android-15 in Application.mk when ndk is used (: = = followed by the lowest version of your compiled sdk).

  • II. Set your compile-time version of sdk to android-21 before.

Specific links are as follows: hateful can not locate symbol.

In the course of the experiment, the idea of ??direct loading replaced. Dex the apk, so that all resources can be obtained from the dex, but found is a special sb, Apk file structure analysis. Since I want to replace the entire resource file, why not do the old.apk and new.apk difference, access to the difference patch, and then through the old.apk and patch synthesis of the new apk, direct load. (This is just a vision, the follow-up will go to verify).

Deficiencies and problems

To verify the micro-hot patch is to write a very simple demo, to achieve the basic functions, but did not do in-depth verification of complex issues, such as the growth of the package with the memory consumption, time, success rate.

At this stage can be verified whether the confusion or non-confusing, this principle to achieve the hot patch is feasible. This should be easier to do than the NuWa hot fix before making a patch. After all, one is positive and the other is in reverse, just the difference between the old and new packages.

But also to study the next hot patch If you load the resource file to enable it to take effect, stands to reason is feasible.

ref:

WeChat Android hot patch practice of the road of evolution(Used in the article)

fake wechat hot fix

cannot locate symbol “signal”

Apk file structure analysis 1