Hotfixes have been hot in the Android world for the last year or so, and I’m sure you’ve read some of the team’s hotfixes blog posts about framework selection and so on. Framework selection is important, but it’s much easier to have a set of hotfixes and then change the framework.

Andfix

Andfix is an Ali solution for fixing bugs online, so today we’re going to take a look at Andfix and how you can apply it to your project. It claims that AndFix supports Android 2.3 to 7.0, both ARM and X86 architectures, as well as Dalvik and ART virtual machines, and both 32-bit and 64-bit models.

The implementation principle of AndFix is as follows:

Method substitution, Andfix implements method substitution through custom annotations, because there is a native method art_replaceMethod inside ART or a dalvik_replaceMethod in Dalvik.

How to use

It’s easy to use:

  1. Import the aar package: compile ‘com. Alipay. Euler: andfix: 0.5.0 @ aar’.

  2. Initialize PatchManager in your own Application.

  3. Loading past patch files.

  4. Add a new patch file.

Dependencies {the compile 'com. Alipay. Euler: andfix: 0.5.0 @ aar'} PatchManager PatchManager = new PatchManager (context); patchManager.init(appversion); //current version patchManager.loadPatch(); patchManager.addPatch(path); //path of the patch file that was downloadedCopy the code

The principle is introduced

Apkpatch compares the two APKs once and finds the different parts. You can see the generated Apatch file, change the suffix to zip and unzip, there is a dex file. Look at the source code through Jadx, which is the class file where the repaired code is located. The changed classes are added with a _CF suffix, and the changed methods are annotated with an @methodreplace annotation, which specifies the method to be replaced by clazz and method.

Then the client SDK gets the patch file and looks for the replacement method based on the annotations. Finally, the JNI layer completes the method replacement.

If multiple patches are saved locally, AndFix loads the patches in the order they were generated. This is based on the patch. MF field in the. Apatch file.

The patch package

There are a lot of issues on AndFix, so using this hotfix prematurely for the enterprise can cause a lot of problems. Then the whole solution to the patch is the key to the hot fix. For example, how to perform grayscale testing, how to control whitelist and blacklist, how to perform patch version management, how to automate patch installation, and how to perform patch rollback.

Gray level test

The use of whitelisting is necessary if grayscale testing is required to advance the patch solution. What is gray testing, that is, for some specific people, the release of specific patches, stage testing. This specific group can be employees within the company, deep users of the company, etc. How to ensure that patches are delivered to specific groups of people requires code control. For example, when the application is started, the user’s mobile phone information or corresponding APP version number is uploaded. The backend checks whether the user is in the whitelist based on the information, and then checks whether the corresponding patch exists in the patch library.

Patch Version Management

Patch files follow the app version number, so it is recommended to use a four-paragraph format, such as 3.1.0.1, indicating that the corresponding APP version is 3.1.0. A patch release platform is required to upload patches, submit version information at the front end, and analyze whether corresponding patches exist at the back end. Attention should also be paid to the addition of a patch control switch, which can be used to block the release of patches in an emergency.

Automatic patching

In learning to use AndFix, you will use the Apatch tool, and manual patching will cause some problems. Most companies use Jenkins to build projects, and the corresponding signature files are placed on the server. For example, after a successful construction, the corresponding APK will be renamed and released to the official website of the company and other channels, and the APK will be saved. If the version has problems, the code will be repaired, and the APK will be built again. The APK will be saved, and the ORIGINAL APK will be used for diff, and the patch file will be generated and pushed to the patch release platform. This clears up the whole process, and then the developer just needs to verify that the patch works in the pre-release environment. If it works, the patch release switch can be directly turned on and pushed by the user.

Flash back patch

After a user downloads the patch, due to some reasons, such as domestic ROM, the latest Android version, etc., the application will crash when initializing the patch, or it may be due to patch release errors. In these cases, if there is no effective means to deal with it, then you will lose this segment of users. How to roll back a patch is an important part of the patch solution. For example, determine whether the patch crash flag exists before executing the hot fix code, set a flag bit if it does not exist, and then clear the flag after executing the hot fix code. If it exists, skip the hot fix code.

Then how to manage the tag? If the user uses a patch and crashes abnormally, if the tag is not managed, that is, the user cannot use the patch file. There are also many solutions, such as tag following the version number.

Of course, you can also roll back the patch. For example, if the patch is detected, you can clear the patch file.