All Products
Search
Document Center

Use hotpatch in an Android application

Last Updated: Mar 24, 2021

This topic describes how to create an Android project and use the hotpatch function.

Note:The hotpatch function supports access Native AAR, access mPaaS Inside and access componentization. For more information about access methods, see Access methods.

This topic is divided into two parts to describe how to use the hotpatch function:

Access a hotpatch

The process of accessing a hotpatch is as follows:

  1. Configure a development environment
  2. Create an App in the console
  3. Create a project on the client
  4. Create a signature
  5. Configure encryption information
  6. Compile code
  7. Release a client version that supports the hotpatch function

Configure a development environment

For more information, see Configure a Development Environment.

Create an App in the console

For more information, see Create an mPaaS App in the Console. There is no signed APK on the local host. Therefore, you do not need to upload the APK when downloading the configuration file.

Example of the name of a downloaded configuration file: Ant-mpaas-4111111111005-default-Android.config.

Create a project on the client

Create a project based on an mPaaS framework, and ensure that you use mPaaS > Build to successfully create a project. For more information, see the Introduction to access procedure.

Note: When adding the SDK, ensure that you select HOTFIX and RPC, as shown in the following figure.

Create a signature

A signature is used to configure encryption information in the next step.

Open the Portal project in Android Studio, then sign the app and generate a signed APK. For more information, see the document on the Android website.

The process is as follows:

  1. Generate a key and a key store. If a key store exists, skip this step.

    1. Open the Portal project in Android Studio, choose Build > Generate Signed APK, and then click Create new.
      new_keystoe_entry

    2. Complete relevant configurations and click OK.

      Note: You need to remember the entered information for use in the subsequent steps.
      new_keystore

  2. Generate a signed APK.

    1. Select a key store, complete relevant configurations, and then click Next.
      gen_signed_apk_ok

    2. Complete relevant configurations as shown in the following figure, and then click Finish. The signed APK is generated after a while.

      Note:
      • V1 (Jar Signature) in Signature Versions must be checked while V2 (Full APK Signature) is optional.
      • The signed APK is stored in the directory {APK Destination Folder}/{Build Type}. In the example shown in the following figure, the directory is /Users/archer.zb/AndroidStudioProjects/Tutorials/Hotpatch/app/debug/.
      gen_signed_apk_f

  3. Add the signature configuration in the code. For more information, see the document on the Android website. After the configuration is completed, build.gradle is shown as follows:

    1. signingConfigs {
    2. release {
    3. keyAlias 'keyAlias'
    4. keyPassword 'keyPassword'
    5. storeFile file('/Users/archer.zb/Desktop/hotpatch/keystore.jks')
    6. storePassword 'storePassword'
    7. }
    8. debug {
    9. keyAlias 'keyAlias'
    10. keyPassword 'keyPassword'
    11. storeFile file('/Users/archer.zb/Desktop/hotpatch/keystore.jks')
    12. storePassword 'storePassword'
    13. }
    14. }

Configure encryption information

To ensure the safety when the client obtains the hotpatch package, you need to encrypt the network content. The procedure for configuring the encryption information is as follows:

  1. Download the configuration file again from the console.

    Log in to the console and choose Manage codes > Code configurations > Android, upload the signed APK generated in the previous step, and then download the configuration file again. The configuration file is downloaded as a .zip file.

  2. Decompress the .zip file, and use the Ant-mpaas-xxx-xxx-Android.config file to replace the file with the same name in the main module of the Portal project.

    Note: After the replacement, base64Code must have a non-null value.
     config_base64

  3. In the main module of the Portal project, delete the yw_1222.jpg picture in the src/main/res/drawable directory.

  4. Reconstruct the Bundle and Portal projects respectively.

Compile code

Now, the hotpatch is accessed. You can compile codes based on business needs.

Button example

Note: The following code sample allows you to experience the hotpatch function before it is released.

To use the hotpatch function, you can add two buttons in the interface of the Bundle project:

add_buttons

  • Toast: The code for this button has a bug. If you click this button, the app crashes.
  • Hotfix: After releasing a hotpatch package in the console, you can click this button to trigger the hotpatch function. After you restart the app, the bug in the code of the Toast button is fixed.

Code sample

The corresponding layout code is as follows:

  1. <Button
  2. android:id="@+id/toast"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:text="Toast" />
  6. <Button
  7. android:id="@+id/hotfix"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="Hotfix" />

The corresponding Java code is as follows:

  1. findViewById(R.id.toast).setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View view) {
  4. // When the Toast button is clicked, the division operation is performed, and the result is displayed in a pop-up.
  5. int result = 1/0; // The divisor is 0, which is a bug and may cause app crash.
  6. Toast.makeText(getApplicationContext(), "result = " + result, Toast.LENGTH_SHORT).show();
  7. }
  8. });
  9. findViewById(R.id.hotfix).setOnClickListener(new View.OnClickListener() {
  10. @Override
  11. public void onClick(View view) {
  12. // When the Hotfix button is clicked, hotpatch is triggered.
  13. // When the SDK version is 10.1.32 or later, call the following API:
  14. MPHotpatch.init();
  15. // When the SDK version is earlier than 10.1.32, call the following API:
  16. // HotPatchUtils.trigDynamicRelease(getApplicationContext(), true);
  17. }
  18. });

MainActivity.java

After the code sample is added, you only need to reconstruct the APK generated by the Bundle and Portal projects. For more information about how to use the hotpatch function, see Demonstration of bug hotpatch.

Release a client version that supports the hotpatch function

After completing the client code, you can release the generated APK to the app platform for app users to download and update. For more information, see Android release management or iOS release management.

Demonstration of bug hotpatch

The following sample process shows how to use the hotpatch function to fix bugs:

  1. Back up the .jar package generated by the bug version.
  2. Modify the bug code and generate a hotpatch package.
  3. Add and release the hotpatch package in the console.
  4. The client calls the API that triggers hotpatch to obtain the hotpatch package.
  5. After the app is restarted, the hotpatch is triggered and the bug is fixed.

Back up the .jar package generated by the bug version

The packages generated by the old and new versions (bug version and fixed version) are needed to generate the hotpatch package. Therefore, you need to back up the .jar package generated by the bug version.

Directory of the .jar package:

  • If a debug package is generated, the directory is build/intermediates/bundle/xxxx-raw.jar under the main module of the Bundle project.

  • If a release package is generated, the .jar package name does not have the -raw suffix.

    Example:
    bundle_build_result

Modify the bug code

Modify the bug code and reconstruct the project. Based on the preceding example, you can change the divisor to 1:

fix_code

Generate a hotpatch package

In Android Studio, choose mPaaS > Generate Hotpatch to generate a hotpatch package.

gen_hotpatch_file

  • New Bundle: the .jar package re-generated by the fixed version.
  • Old Bundle: the .jar package generated by the backup bug version. For more information, see Back up the .jar package generated by the bug version.
  • Whitelist: the configuration file in .txt format that specifies the fixed classes. For the rules for editing a configuration file, see Rules for editing a whitelist configuration file. You are recommended to use Whitelist, otherwise the hotpatch package may fail to be generated.
  • Patch file dir: the directory where the hotpatch package is stored. Multiple files are generated in the directory, in which only the .jar file is useful in the subsequent steps:
  • DexPatch: We recommend that you select this check box.

For more information, see Hotpatch package functions.

Add and release the hotpatch package in the console

Add a hotpatch package

  1. Log in to the mPaaS console and choose Delivery Service > Manage hotpatches.
  2. Click Add hotpatches, complete relevant configurations, and then click OK:

Create a release

  1. Click Create release as shown in the following figure.
  2. Select a release type and then click OK. For more information, see Hotpatch management.

App-triggered hotpatch

  1. Click Logcat in Android Studio, enter DynamicRelease as a keyword, and select No Filters.

    logcat_settings

  2. Ensure that your cellphone is connected to Android Studio, open the bug app installed on your phone, and then click the Hotfix button. The following log is displayed:

    hotpatch_log

  3. After closing and restarting the app process, click the Toast button. If a pop-up is displayed, the bug is fixed.

Note: If the hotpatch does not work and a RPCException [7001] error exists in the log, the signature is wrong. Repeat the step Create a signature and ensure that:
  • base64Code in the Ant-mpaas-xxx-xxx-Android.config file of the main module of the Portal project has a non-null value.
  • signingConfigs in the build.gradle file of the main module of the Portal project is correctly configured.