DEV Community

zhonghua
zhonghua

Posted on • Edited on

HarmonyOS Development in Practice: Easily Configure Multi-Environment Directories for Efficient App Deployment

HarmonyOS Development in Practice: Easily Configure Multi-Environment Directories for Efficient App Deployment

Introduction

During the development of HarmonyOS applications, it is often necessary to configure different parameters and resources for different environments (such as development and production environments). This article will delve into how to flexibly configure multi-environment directories in HarmonyOS, share practical experience, and make your app deployment more efficient and convenient.

  1. Configuring Environment Parameters

First, we need to configure parameters for different environments. These parameters can be stored in a JSON file. For example, we can create a file named config.json with the following content:

{
  "sslkey": "11==",
  "qrkey": "122="
}
Enter fullscreen mode Exit fullscreen mode

Note that this is just an example. You should configure the actual parameters according to your application's requirements.

  1. Establishing Environment Directories

In the AppScope of HarmonyOS, we can create a resource directory for each environment. For example, we can create resources_default and resources_pro directories for the development and production environments, respectively. Under each directory, we can create a rawfile subdirectory to store the config.json files for different environments.

The directory structure is as follows:

AppScope/
├── resources_default/
│   └── rawfile/
│       └── config.json  # Configuration file for the development environment
└── resources_pro/
    └── rawfile/
        └── config.json  # Configuration file for the production environment
Enter fullscreen mode Exit fullscreen mode
  1. Configuring the Project Build File

Next, we need to specify the resource directories for different environments in the project's build file. Here is an example configuration:

[
  {
    // Default environment (development environment)
    "name": "default",
    "signingConfig": "default",
    "compileSdkVersion": "4.1.0(11)",
    "compatibleSdkVersion": "4.1.0(11)",
    "runtimeOS": "HarmonyOS",
    "output": {
      "artifactName": "default_app"
    },
    "resource": {
      "directories": [
        "./AppScope/resources_default"
      ]
    }
  },
  {
    // Production environment
    "name": "pro",
    "signingConfig": "default",
    "compileSdkVersion": "4.1.0(11)",
    "compatibleSdkVersion": "4.1.0(11)",
    "runtimeOS": "HarmonyOS",
    "output": {
      "artifactName": "pro_app"
    },
    "resource": {
      "directories": [
        "./AppScope/resources_pro"
      ]
    }
  }
]
Enter fullscreen mode Exit fullscreen mode
  1. Calling Configuration Parameters

Where needed, we can obtain the configuration parameters through code. Here is an example code snippet:

public localRes() {
  // Get the configuration file
  try {
    let rawFile = this.resourceManager.getRawFileContentSync("config.json");
    let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true });
    let jsonString = textDecoder.decodeWithStream(rawFile, { stream: false });
    let configObj = JSON.parse(jsonString) as Config;
    Log.debug('config print: ' + jsonString);
    // Use parameters in configObj...
  } catch (e) {
    console.error("config error: " + e);
  }
}
Enter fullscreen mode Exit fullscreen mode

Note that when calling the configuration parameters, since we have already specified the resource directories for different environments, the getRawFileContentSync method will automatically load the config.json file from the resource directory of the current environment.

  1. Conclusion

Through the introduction in this article, you should now have mastered how to configure multi-environment directories in HarmonyOS. This method can make your app deployment more flexible and efficient, allowing you to use different parameters and resources in different environments. If you have any questions or suggestions, please feel free to leave a comment.

Top comments (0)