All Products
Search
Document Center

Preset an Android mini program in the client

Last Updated: Mar 11, 2021

The traditional Mini Program technology can be easily affected by the network environment. Therefore, you may fail to pull the Mini Program package when the network condition is poor. However, you can avoid this problem by presetting the Mini Program. This topic introduces the principle and implementation process of presetting a Mini Program.

About presetting a Mini Program

Presetting a Mini Program is to pack the static resources of the Mini Program such as rendering, logic, and configuration into a compressed package. Then, the client downloads the Mini Program package to the local in advance and loads the resources locally. Presetting the Mini Program can minimize the impact of the network environment on the mPaaS Mini Program page. The benefits of using the preset package are as follows.

  • Improve the user experience
    By embedding the static resources of the page into the App through the preset package and releasing the resources with the App, when you open the App for the first time, you can directly use the resources without relying on the network environment to download them.
  • Implement dynamic update
    When a new version or emergency release is about to be launched for a Mini Program, you can conduct iterative development in the mini program IDE and release the new version in the mPaaS console. The Mini Program SDK integrated in the client will then automatically update the Mini Program to the latest version. This release method does not require App Store review, allowing users to receive updates in a timely manner.

The structure and usage

This topic describes how to preset a Mini Program from the following aspects:

The structure of the mini program preset package

The mini program preset package is a compression file with the .amr extension. By changing the extension from .amr to .zip and decompress the package, you can see HTML resources and JavaScript code. After the mini program container is loaded, these resources and code can be rendered in the UC kernel.

By taking the Android system as an example, the following figure shows the directory structure of a general resource package:

  • Level-1 directory: It is generally the ID of the resource package, such as 2020121620201216_1.0.1.0.zip.
  • Level-2 and lower-level directories are custom resource files. They also specify the entry file that is opened by the current preset package by default, such as /index.html.
    structure

The usage of the mini program preset package

The process of using the mini program preset package is as follows.

  1. Request the package information.
    This step is to request the mini program package from the server and store its information to the local database. The package information includes the download address and version number of the mini program package.
  2. Download the mini program package.
    Download the mini program package from the server to your mobile phone.
  3. Install the mini program package.
    Download directories and copy them to the installation directory on the mobile phone.

Prerequisites

Procedure

  1. Preset the mini program package.
    1. In the mPaaS console, release the Mini Program package and download the AMR and configuration files.
      amr
    2. Place the downloaded AMR and configuration files to the assets directory of the mPaaS project.
      asset
    3. Add preset code in the project to call the preset code for App installation when the App starts. The code sample of presetting is as follows:
           
      1. new Thread(new Runnable(){
      2. @Override
      3. public void run(){
      4. MPNebula.loadofflineNebula(jsonFileName: "h5_json.json",
      5. new MPNebulaOfflineInfo(offLineFileName:"2020121620201216_1.0.1.0.amr",
      6. addId:"2020121620201216",
      7. version:"1.0.1.0"));
      8. }
      9. }).start();
      Notes:
      • This method is a blocking call, and therefore do not call the built-in preset package method in the main thread.
      • This method can be called only once. If you call this method multiple times, only the first call is valid. For this reason, you need to pass in full preset package information at one time.
      • If you need to build in multiple AMR packages, make sure that they already exist. Otherwise, other built-in preset packages cannot be imported.
  2. Start the Mini Program. The code sample for starting the Mini Program is as follows:
       
    1. /**
    2. * Start the Mini Program.
    3. *
    4. * @param appId: The Mini Program ID.
    5. */
    6. public static void startApp(String appId);
  3. Update the Mini Program.
    By default, every time you open the App, the Mini Program SDK attempts to check if a newer version is available. The check interval is restricted to 30 minutes by default to minimize the stress on the server. To instantly check for the latest available version, call the following code to request an update. Generally, you can call the code after the App starts or the user logs in.
       
    1. MPNebula.updateAllApp(new MpaasNebulaUpdateCallback(){
    2. @Override
    3. public void onResult(final boolean success, final boolean isLimit) {
    4. super.onResult(success, isLimit);
    5. runOnUiThread(new Runnable() {
    6. @Override
    7. public void run() {
    8. AUToast.makeToast(NebulaAppActivity.this,
    9. success ? R.string.update_success : R.string.update_failure, 2000).show();
    10. }
    11. });
    12. }
    13. });
  4. Check the security signature.
    The Mini Program has a signature verification mechanism to prevent malicious programs from tampering with the mini program package downloaded to the device. To enable this mechanism, call the MPNebula API to configure verification parameters. If you are using the 10.1.60 baseline or later, you must enable container configuration additionally. For details, see Configure HTML5 Containers.
    Notes:
    • Be sure to call the MPNebula API before opening the mini program package for the first time. Otherwise, public key initialization will fail. For details about public and private keys, see Configure the mini program Package > Manage keys.
    • Signature verification is forced on mobile phones that are determined as rooted no matter signature verification is enabled or not on the client.
           
      1. /**
      2. * @param publicKey The public key for signature verification.
      3. */
      4. public static void enableAppVerification(final String publicKey)
  5. Delete the local Mini Program.
    Nebula provides the API for deleting local App information. After local App information is deleted, opening the App again requests the server to download and update local Mini Program information again.
       
    1. public class MPNebula {
    2. // appId indicates the mini program app ID.
    3. public static boolean deleteAppInfo(String appId);
    4. }
    Note: The minimum baselines versions that supports this API are 10.1.68.8 for 10.1.68 series and 10.1.60.14 for 10.1.60 series.