Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair – dexpatch

Time:2021-9-4

Introduction:Introduction and instructions on Android hot fix – dexpatch

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

Scheme introduction

In order to solve the problem after the native module goes online, mpaas provides the hot repair function to realize the hot repair in the scenario of not publishing the client APK. At present, Android hot fixes mainly include andfix and dexpatch. Considering the version compatibility of andfix, it is mainly recommendedDexPatch

The principle of dexpatch repair is relatively simple, that is, after startup, pull the jar package address that needs to be distributed through RPC, then download the jar package file through an independent process, and save it after downloading. During the second startup, the classloader of the hook system modifies the dexpathlist and adds a DEX file with a modified class at the front of the array to block the loading of classes with the same name in the DEX file behind the array.

As shown in the following figure, the classloader will preferentially load ding.class in patch.dex and ignore ding.class in classes.dex to achieve the effect of replacement.

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

Based on this principle, dexpatch has the following characteristics:

  1. Support scope: it is a class level replacement, so it only supports patches of Java modules, not non Java modules, such as so modules;
  2. Compatibility: because it is the classloader acting on the system and uses less black technology, the overall scheme has good compatibility;
  3. Effectiveness timeliness: it can only take effect after the patch is downloaded and restarted. Real time effectiveness is not supported;
  4. In terms of success rate: since the download is an independent process, the impact of the main process flash back in the startup phase on patch download is reduced, and the success rate of download is increased.

Operating instructions

The following are the main steps and troubleshooting ideas for using the dexpatch module under mpaas to facilitate developers’ daily development.

  1. Trigger patch pull

===============

The startup phase calls mphhotpatch. Init(), which mainly triggers the RPC request for patch information. If the patch publishing rule is hit, the RPC will return the jar package download address of the patch. The client will trigger the download and save it in the client private directory / data / user / 0 / package name / dexpatch / patch /.

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

  1. Code operation demonstration

==========

Taking component-based access mode as an example, this paper introduces the main process of patch release.

(1) Before code change

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

You need to save the build product before the change to facilitate subsequent patch generation. The address is: build / intermediates / bundle / xxxx-raw.jar

(2) After code change

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

Recompile and save the build product. The product address is build / intermediates / bundle / xxxx-raw.jar

(3) Generate whitelist configuration

It is mainly used to specify the repair class in the hot repair package. The configuration file is in. TXT format. The configuration file should contain and contain the following information in order:
A class that requires a patch. Start with L, followed by the real class name after confusion. If there are multiple classes, only one can be written per line. Example: lxxx.xxx.clazzx sets the patch type to dexpatch. Example: patchtype: dexpatch

Set whether it is a static bundle. The default is false. If it is a statically linked bundle, it needs to be explicitly set to true. Example: hostdex: true (* at present, the modules of the mpaas client are generally in the static link, and generally write true)

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

(4) View signature

To generate a patch, you need to use the project’s packaging key, which needs to be prepared in advance. You can find the corresponding configuration under the packaging step

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

(5) Generate patch

① Through the IDE tool provided by mpaas, click hot repair to enter the repair page.

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

② According to the prompts on the page, fill in the jar package addresses prepared before and after repair, as well as the white list configuration file, and check dexpatch to enter the next step

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

③ The next step is to select the packaged configuration file. Recently, click Finish to generate the patch file

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

(6) Generate patch products

The generated patch products are as follows:

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

To view the products, you can use the dex2jar tool to inverse the diff.dex file, and use the JD GUI file to check whether the inverse products meet the expectations

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

After the inverse solution, you can see the modified module:

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

(7) Upload and publish

① Select the product jar package from the previous step to upload

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

② After uploading, it can be published through the white list to verify the stability of the patch

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

(8) Verify Download

After the whitelist is released, start the client and search for the keyword: dynamic release. You can see that there is a log that triggers the download in the tool process.
It should be noted here that the download of the patch triggered here is in the tool process, and the main reason why it is not in the main process is that the patch cannot be downloaded successfully because the main process will flash back repeatedly due to startup. Download the patch separately in the tool process to maximize the success rate of patch download.

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

Then go to the download directory to check whether the download is saved successfully. The download directory is: / data / user / 0 / package name / dexpatch / patch/ [email protected]

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

(9) Kill process start

After confirming that the download is saved successfully, kill the app and restart to see whether it is effective. Restart can search the keyword: dexpatchmanager to view the log of patch effectiveness. The log will print the log of whether there is a patch and whether the patch is loaded.

Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

At the same time, we can also verify the actual business scenario to see whether it is effective.

common problem

  1. Patch does not take effect after AAR mode integration

=======================

During AAR mode integration, you need to inherit the quinoxlessapplication of the framework and specify the application as the implementation class of the framework to load dexpatch. Quinoxlessapplication mainly encapsulates the initialization and loading of dexpatch module.

  1. It will not take effect after reinforcement

============

You need to use the APK before reinforcement to generate a patch, but you can’t use the package after reinforcement to generate a patch. Then it is necessary to verify the compatibility performance under different reinforcement manufacturers.

  1. After hot repair, Apache HTTP related crash occurs for RPC related calls.

===============================================

Please use the method on the official Android website to import Apache HTTP client. It is forbidden to import HTTP client by importing jar package or gradle implementation / compile. Otherwise, it will cause confusion in classloader loading classes.

Suggested method:

<uses-library android:name=”org.apache.http.legacy” android:required=”false”/>

E · N · D
Author: Rong Yang
Original link![ Upload failed, undefined] ()
Technical dry goods | deep deconstruction of the Savior of Android applications in the face of emergency release: mpaas hot repair - dexpatch

This article is the original content of Alibaba cloud and cannot be reproduced without permission

Recommended Today

Java Engineer Interview Questions

The content covers: Java, mybatis, zookeeper, Dubbo, elasticsearch, memcached, redis, mysql, spring, spring boot, springcloud, rabbitmq, Kafka, Linux, etcMybatis interview questions1. What is mybatis?1. Mybatis is a semi ORM (object relational mapping) framework. It encapsulates JDBC internally. During development, you only need to pay attention to the SQL statement itself, and you don’t need to […]